KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > lookup > MetaInfServicesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.lookup;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.print.PrintServiceLookup JavaDoc;
27 import org.netbeans.Module;
28 import org.netbeans.ModuleManager;
29 import org.netbeans.core.startup.ModuleHistory;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.util.Lookup;
32 import org.openide.util.LookupEvent;
33 import org.openide.util.LookupListener;
34 import org.openide.util.Mutex;
35 import org.openide.util.MutexException;
36
37 /** Test whether modules can really register things in their META-INF/services/class.Name
38  * files, and whether this behaves correctly when the modules are disabled/enabled.
39  * Note that Plain loads its classpath modules as soon as you ask for it, so these
40  * tests do not check what happens on the NetBeans startup classpath.
41  * @author Jesse Glick
42  */

43 public class MetaInfServicesTest extends NbTestCase {
44
45     public MetaInfServicesTest(String JavaDoc name) {
46         super(name);
47     }
48     
49     private ModuleManager mgr;
50     private Module m1, m2;
51     protected void setUp() throws Exception JavaDoc {
52         //System.err.println("setUp");
53
//Thread.dumpStack();
54
clearWorkDir();
55         // Load Plain.
56
// Make a couple of modules.
57
mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager();
58         try {
59             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
60                 public Object JavaDoc run() throws Exception JavaDoc {
61                     File JavaDoc jar1 = InstanceDataObjectModuleTestHid.toFile(MetaInfServicesTest.class.getResource("data/services-jar-1.jar"));
62                     File JavaDoc jar2 = InstanceDataObjectModuleTestHid.toFile(MetaInfServicesTest.class.getResource("data/services-jar-2.jar"));
63                     m1 = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false);
64                     m2 = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false);
65                     return null;
66                 }
67             });
68         } catch (MutexException me) {
69             throw me.getException();
70         }
71         assertEquals(Collections.EMPTY_SET, m1.getProblems());
72     }
73     protected void tearDown() throws Exception JavaDoc {
74         try {
75             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
76                 public Object JavaDoc run() throws Exception JavaDoc {
77                     if (m2.isEnabled()) mgr.disable(m2);
78                     mgr.delete(m2);
79                     if (m1.isEnabled()) mgr.disable(m1);
80                     mgr.delete(m1);
81                     return null;
82                 }
83             });
84         } catch (MutexException me) {
85             throw me.getException();
86         }
87         m1 = null;
88         m2 = null;
89         mgr = null;
90     }
91     protected static final int TWIDDLE_ENABLE = 0;
92     protected static final int TWIDDLE_DISABLE = 1;
93     protected void twiddle(final Module m, final int action) throws Exception JavaDoc {
94         try {
95             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
96                 public Object JavaDoc run() throws Exception JavaDoc {
97                     switch (action) {
98                     case TWIDDLE_ENABLE:
99                         mgr.enable(m);
100                         break;
101                     case TWIDDLE_DISABLE:
102                         mgr.disable(m);
103                         break;
104                     default:
105                         throw new IllegalArgumentException JavaDoc("bad action: " + action);
106                     }
107                     return null;
108                 }
109             });
110         } catch (MutexException me) {
111             throw me.getException();
112         }
113     }
114     
115     /** Fails to work if you have >1 method per class, because setUp gets run more
116      * than once (XTest bug I suppose).
117      */

118     public void testEverything() throws Exception JavaDoc {
119         twiddle(m1, TWIDDLE_ENABLE);
120         ClassLoader JavaDoc systemClassLoader = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
121         Class JavaDoc xface = systemClassLoader.loadClass("org.foo.Interface");
122         Lookup.Result JavaDoc r = Lookup.getDefault().lookupResult(xface);
123         List JavaDoc instances = new ArrayList JavaDoc(r.allInstances());
124         // Expect to get Impl1 from first JAR.
125
assertEquals(1, instances.size());
126         Object JavaDoc instance1 = instances.get(0);
127         assertTrue(xface.isInstance(instance1));
128         assertEquals("org.foo.impl.Implementation1", instance1.getClass().getName());
129         // Expect to have (same) Impl1 + Impl2.
130
LookupL l = new LookupL();
131         r.addLookupListener(l);
132         twiddle(m2, TWIDDLE_ENABLE);
133         assertTrue("Turning on a second module with a manifest service fires a lookup change", l.gotSomething());
134         instances = new ArrayList JavaDoc(r.allInstances());
135         assertEquals(2, instances.size());
136         assertEquals(instance1, instances.get(0));
137         assertEquals("org.bar.Implementation2", instances.get(1).getClass().getName());
138         // Expect to lose Impl2.
139
l.count = 0;
140         twiddle(m2, TWIDDLE_DISABLE);
141         assertTrue(l.gotSomething());
142         instances = new ArrayList JavaDoc(r.allInstances());
143         assertEquals(1, instances.size());
144         assertEquals(instance1, instances.get(0));
145         // Expect to lose Impl1 too.
146
l.count = 0;
147         twiddle(m1, TWIDDLE_DISABLE);
148         assertTrue(l.gotSomething());
149         instances = new ArrayList JavaDoc(r.allInstances());
150         assertEquals(0, instances.size());
151         // Expect to not get anything: wrong xface version
152
l.count = 0;
153         twiddle(m1, TWIDDLE_ENABLE);
154         // not really important: assertFalse(l.gotSomething());
155
instances = new ArrayList JavaDoc(r.allInstances());
156         assertEquals(0, instances.size());
157         systemClassLoader = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
158         Class JavaDoc xface2 = systemClassLoader.loadClass("org.foo.Interface");
159         assertTrue(xface != xface2);
160         Lookup.Result JavaDoc r2 = Lookup.getDefault().lookupResult(xface2);
161         instances = new ArrayList JavaDoc(r2.allInstances());
162         assertEquals(1, instances.size());
163         // Let's also check up on some standard JDK services.
164
PrintServiceLookup JavaDoc psl = Lookup.getDefault().lookup(PrintServiceLookup JavaDoc.class);
165         assertNotNull("Some META-INF/services/javax.print.PrintServiceLookup was found in " + Lookup.getDefault(), psl);
166     }
167     
168     protected static final class LookupL implements LookupListener {
169         public int count = 0;
170         public synchronized void resultChanged(LookupEvent ev) {
171             count++;
172             notifyAll();
173         }
174         public synchronized boolean gotSomething() throws InterruptedException JavaDoc {
175             if (count > 0) return true;
176             wait(9999);
177             return count > 0;
178         }
179     }
180     
181 }
182
Popular Tags