Lines 24-35
Link Here
|
24 |
import java.io.IOException; |
24 |
import java.io.IOException; |
25 |
import java.io.InputStream; |
25 |
import java.io.InputStream; |
26 |
import java.io.InputStreamReader; |
26 |
import java.io.InputStreamReader; |
|
|
27 |
import java.lang.ref.Reference; |
28 |
import java.lang.ref.WeakReference; |
27 |
import java.net.URL; |
29 |
import java.net.URL; |
28 |
import java.net.URLClassLoader; |
30 |
import java.net.URLClassLoader; |
29 |
import java.util.ArrayList; |
31 |
import java.util.ArrayList; |
30 |
import java.util.Collections; |
32 |
import java.util.Collections; |
31 |
import java.util.Enumeration; |
33 |
import java.util.Enumeration; |
|
|
34 |
import java.util.HashMap; |
35 |
import java.util.HashSet; |
32 |
import java.util.List; |
36 |
import java.util.List; |
|
|
37 |
import java.util.Map; |
33 |
import java.util.Set; |
38 |
import java.util.Set; |
34 |
import java.util.Set; |
39 |
import java.util.Set; |
35 |
import java.util.TreeSet; |
40 |
import java.util.TreeSet; |
Lines 44-69
Link Here
|
44 |
import org.openide.util.LookupEvent; |
49 |
import org.openide.util.LookupEvent; |
45 |
import org.openide.util.LookupListener; |
50 |
import org.openide.util.LookupListener; |
46 |
import org.openide.util.RequestProcessor; |
51 |
import org.openide.util.RequestProcessor; |
|
|
52 |
import org.openide.util.WeakSet; |
47 |
|
53 |
|
48 |
/** Test finding services from manifest. |
54 |
/** Test finding services from manifest. |
49 |
* @author Jesse Glick |
55 |
* @author Jesse Glick |
50 |
*/ |
56 |
*/ |
51 |
public class MetaInfServicesLookupTest extends NbTestCase { |
57 |
public class MetaInfServicesLookupTest extends NbTestCase { |
52 |
private Logger LOG; |
58 |
private Logger LOG; |
|
|
59 |
private Map<ClassLoader,Lookup> lookups = new HashMap<ClassLoader,Lookup>(); |
53 |
|
60 |
|
54 |
public MetaInfServicesLookupTest(String name) { |
61 |
public MetaInfServicesLookupTest(String name) { |
55 |
super(name); |
62 |
super(name); |
56 |
LOG = Logger.getLogger("Test." + name); |
63 |
LOG = Logger.getLogger("Test." + name); |
57 |
} |
64 |
} |
58 |
|
65 |
|
59 |
private String prefix() { |
66 |
protected String prefix() { |
60 |
return "META-INF/services/"; |
67 |
return "META-INF/services/"; |
61 |
} |
68 |
} |
62 |
|
69 |
|
|
|
70 |
protected Lookup createLookup(ClassLoader c) { |
71 |
return Lookups.metaInfServices(c); |
72 |
} |
73 |
|
63 |
protected Level logLevel() { |
74 |
protected Level logLevel() { |
64 |
return Level.INFO; |
75 |
return Level.INFO; |
65 |
} |
76 |
} |
66 |
|
77 |
|
|
|
78 |
private Lookup getTestedLookup(ClassLoader c) { |
79 |
Lookup l = lookups.get(c); |
80 |
if (l == null) { |
81 |
l = createLookup(c); |
82 |
lookups.put(c, l); |
83 |
} |
84 |
return l; |
85 |
} |
86 |
|
67 |
private URL findJar(String n) throws IOException { |
87 |
private URL findJar(String n) throws IOException { |
68 |
LOG.info("Looking for " + n); |
88 |
LOG.info("Looking for " + n); |
69 |
File jarDir = new File(getWorkDir(), "jars"); |
89 |
File jarDir = new File(getWorkDir(), "jars"); |
Lines 154-161
Link Here
|
154 |
}, c0); |
174 |
}, c0); |
155 |
} |
175 |
} |
156 |
|
176 |
|
|
|
177 |
protected void tearDown() throws Exception { |
178 |
Set<Reference<Lookup>> weak = new HashSet<Reference<Lookup>>(); |
179 |
for (Lookup l : lookups.values()) { |
180 |
weak.add(new WeakReference<Lookup>(l)); |
181 |
} |
182 |
|
183 |
lookups = null; |
184 |
|
185 |
for(Reference<Lookup> ref : weak) { |
186 |
assertGC("Lookup can disappear", ref); |
187 |
} |
188 |
} |
189 |
|
157 |
public void testBasicUsage() throws Exception { |
190 |
public void testBasicUsage() throws Exception { |
158 |
Lookup l = Lookups.metaInfServices(c2); |
191 |
Lookup l = getTestedLookup(c2); |
159 |
Class xface = c1.loadClass("org.foo.Interface"); |
192 |
Class xface = c1.loadClass("org.foo.Interface"); |
160 |
List results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
193 |
List results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
161 |
assertEquals(2, results.size()); |
194 |
assertEquals(2, results.size()); |
Lines 169-179
Link Here
|
169 |
|
202 |
|
170 |
public void testLoaderSkew() throws Exception { |
203 |
public void testLoaderSkew() throws Exception { |
171 |
Class xface1 = c1.loadClass("org.foo.Interface"); |
204 |
Class xface1 = c1.loadClass("org.foo.Interface"); |
172 |
Lookup l3 = Lookups.metaInfServices(c3); |
205 |
Lookup l3 = getTestedLookup(c3); |
173 |
// If we cannot load Interface, there should be no impls of course... quietly! |
206 |
// If we cannot load Interface, there should be no impls of course... quietly! |
174 |
assertEquals(Collections.EMPTY_LIST, |
207 |
assertEquals(Collections.EMPTY_LIST, |
175 |
new ArrayList(l3.lookup(new Lookup.Template(xface1)).allInstances())); |
208 |
new ArrayList(l3.lookup(new Lookup.Template(xface1)).allInstances())); |
176 |
Lookup l4 = Lookups.metaInfServices(c4); |
209 |
Lookup l4 = getTestedLookup(c4); |
177 |
// If we can load Interface but it is the wrong one, ignore it. |
210 |
// If we can load Interface but it is the wrong one, ignore it. |
178 |
assertEquals(Collections.EMPTY_LIST, |
211 |
assertEquals(Collections.EMPTY_LIST, |
179 |
new ArrayList(l4.lookup(new Lookup.Template(xface1)).allInstances())); |
212 |
new ArrayList(l4.lookup(new Lookup.Template(xface1)).allInstances())); |
Lines 183-200
Link Here
|
183 |
} |
216 |
} |
184 |
|
217 |
|
185 |
public void testStability() throws Exception { |
218 |
public void testStability() throws Exception { |
186 |
Lookup l = Lookups.metaInfServices(c2); |
219 |
Lookup l = getTestedLookup(c2); |
187 |
Class xface = c1.loadClass("org.foo.Interface"); |
220 |
Class xface = c1.loadClass("org.foo.Interface"); |
188 |
Object first = l.lookup(new Lookup.Template(xface)).allInstances().iterator().next(); |
221 |
Object first = l.lookup(new Lookup.Template(xface)).allInstances().iterator().next(); |
189 |
l = Lookups.metaInfServices(c2a); |
222 |
l = getTestedLookup(c2a); |
190 |
Object second = l.lookup(new Lookup.Template(xface)).allInstances().iterator().next(); |
223 |
Object second = l.lookup(new Lookup.Template(xface)).allInstances().iterator().next(); |
191 |
assertEquals(first, second); |
224 |
assertEquals(first, second); |
192 |
} |
225 |
} |
193 |
|
226 |
|
194 |
public void testMaskingOfResources() throws Exception { |
227 |
public void testMaskingOfResources() throws Exception { |
195 |
Lookup l1 = Lookups.metaInfServices(c1); |
228 |
Lookup l1 = getTestedLookup(c1); |
196 |
Lookup l2 = Lookups.metaInfServices(c2); |
229 |
Lookup l2 = getTestedLookup(c2); |
197 |
Lookup l4 = Lookups.metaInfServices(c4); |
230 |
Lookup l4 = getTestedLookup(c4); |
198 |
|
231 |
|
199 |
assertNotNull("services1.jar defines a class that implements runnable", l1.lookup(Runnable.class)); |
232 |
assertNotNull("services1.jar defines a class that implements runnable", l1.lookup(Runnable.class)); |
200 |
assertNull("services2.jar does not defines a class that implements runnable", l2.lookup(Runnable.class)); |
233 |
assertNull("services2.jar does not defines a class that implements runnable", l2.lookup(Runnable.class)); |
Lines 202-213
Link Here
|
202 |
} |
235 |
} |
203 |
|
236 |
|
204 |
public void testOrdering() throws Exception { |
237 |
public void testOrdering() throws Exception { |
205 |
Lookup l = Lookups.metaInfServices(c1); |
238 |
Lookup l = getTestedLookup(c1); |
206 |
Class xface = c1.loadClass("java.util.Comparator"); |
239 |
Class xface = c1.loadClass("java.util.Comparator"); |
207 |
List results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
240 |
List results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
208 |
assertEquals(1, results.size()); |
241 |
assertEquals(1, results.size()); |
209 |
|
242 |
|
210 |
l = Lookups.metaInfServices(c2); |
243 |
l = getTestedLookup(c2); |
211 |
xface = c2.loadClass("java.util.Comparator"); |
244 |
xface = c2.loadClass("java.util.Comparator"); |
212 |
results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
245 |
results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
213 |
assertEquals(2, results.size()); |
246 |
assertEquals(2, results.size()); |
Lines 216-222
Link Here
|
216 |
assertEquals("org.foo.impl.Comparator1", results.get(1).getClass().getName()); |
249 |
assertEquals("org.foo.impl.Comparator1", results.get(1).getClass().getName()); |
217 |
|
250 |
|
218 |
// test that items without position are always at the end |
251 |
// test that items without position are always at the end |
219 |
l = Lookups.metaInfServices(c2); |
252 |
l = getTestedLookup(c2); |
220 |
xface = c2.loadClass("java.util.Iterator"); |
253 |
xface = c2.loadClass("java.util.Iterator"); |
221 |
results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
254 |
results = new ArrayList(l.lookup(new Lookup.Template(xface)).allInstances()); |
222 |
assertEquals(2, results.size()); |
255 |
assertEquals(2, results.size()); |
Lines 251-257
Link Here
|
251 |
} |
284 |
} |
252 |
} |
285 |
} |
253 |
Loader loader = new Loader(); |
286 |
Loader loader = new Loader(); |
254 |
Lookup l = Lookups.metaInfServices(loader); |
287 |
Lookup l = getTestedLookup(loader); |
255 |
|
288 |
|
256 |
Object no = l.lookup(String.class); |
289 |
Object no = l.lookup(String.class); |
257 |
assertNull("Not found of course", no); |
290 |
assertNull("Not found of course", no); |
Lines 259-265
Link Here
|
259 |
} |
292 |
} |
260 |
|
293 |
|
261 |
public void testListenersAreNotifiedWithoutHoldingALockIssue36035() throws Exception { |
294 |
public void testListenersAreNotifiedWithoutHoldingALockIssue36035() throws Exception { |
262 |
final Lookup l = Lookups.metaInfServices(c2); |
295 |
final Lookup l = getTestedLookup(c2); |
263 |
final Class xface = c1.loadClass("org.foo.Interface"); |
296 |
final Class xface = c1.loadClass("org.foo.Interface"); |
264 |
final Lookup.Result res = l.lookup(new Lookup.Template(Object.class)); |
297 |
final Lookup.Result res = l.lookup(new Lookup.Template(Object.class)); |
265 |
|
298 |
|