46 import com.sun.net.httpserver.HttpExchange; |
46 import com.sun.net.httpserver.HttpExchange; |
47 import com.sun.net.httpserver.HttpHandler; |
47 import com.sun.net.httpserver.HttpHandler; |
48 import com.sun.net.httpserver.HttpServer; |
48 import com.sun.net.httpserver.HttpServer; |
49 |
49 |
50 /** |
50 /** |
51 * Verifies the impact of sun.misc.JarIndex.metaInfFilenames on service loaders |
51 * Verifies the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader |
52 * (sun.misc.Service & java.util.ServiceLoader), as well as finding resources |
52 * and on finding resources via Class.getResource. |
53 * through Class.getResouce. |
|
54 * |
53 * |
55 * 1) Compile the test sources: |
54 * 1) Compile the test sources: |
56 * jarA: |
55 * jarA: |
57 * META-INF/services/my.happy.land |
56 * META-INF/services/my.happy.land |
58 * com/message/spi/MessageService.java |
57 * com/message/spi/MessageService.java |
212 static void doTest(InetSocketAddress serverAddress) throws IOException { |
211 static void doTest(InetSocketAddress serverAddress) throws IOException { |
213 URL baseURL = new URL("http://localhost:" + serverAddress.getPort() + "/"); |
212 URL baseURL = new URL("http://localhost:" + serverAddress.getPort() + "/"); |
214 |
213 |
215 int failed = 0; |
214 int failed = 0; |
216 |
215 |
217 // Tests using sun.misc.Service |
|
218 if (!sunMiscServiceTest(baseURL, messageService, true, false, true)) { |
|
219 System.out.println("Test: sun.misc.Service looking for " + messageService + ", failed"); |
|
220 failed++; |
|
221 } |
|
222 if (!sunMiscServiceTest(baseURL, unknownService, false, false, false)) { |
|
223 System.out.println("Test: sun.misc.Service looking for " + unknownService + " failed"); |
|
224 failed++; |
|
225 } |
|
226 |
|
227 // Tests using java.util.SerivceLoader |
216 // Tests using java.util.SerivceLoader |
228 if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) { |
217 if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) { |
229 System.out.println("Test: sun.misc.Service looking for " + messageService + ", failed"); |
218 System.out.println("Test: ServiceLoader looking for " + messageService + ", failed"); |
230 failed++; |
219 failed++; |
231 } |
220 } |
232 if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) { |
221 if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) { |
233 System.out.println("Test: sun.misc.Service looking for " + unknownService + " failed"); |
222 System.out.println("Test: ServiceLoader looking for " + unknownService + " failed"); |
234 failed++; |
223 failed++; |
235 } |
224 } |
236 |
225 |
237 // Tests using java.lang.Class (similar to the FontManager in javafx) |
226 // Tests using java.lang.Class (similar to the FontManager in javafx) |
238 if (!klassLoader(baseURL, "/META-INF/fonts.mf", true, false, true)) { |
227 if (!klassLoader(baseURL, "/META-INF/fonts.mf", true, false, true)) { |
244 failed++; |
233 failed++; |
245 } |
234 } |
246 |
235 |
247 if (failed > 0) |
236 if (failed > 0) |
248 throw new RuntimeException("Failed: " + failed + " tests"); |
237 throw new RuntimeException("Failed: " + failed + " tests"); |
249 } |
|
250 |
|
251 static boolean sunMiscServiceTest(URL baseURL, |
|
252 String serviceClass, |
|
253 boolean expectToFind, |
|
254 boolean expectbDotJar, |
|
255 boolean expectcDotJar) throws IOException { |
|
256 debug("----------------------------------"); |
|
257 debug("Running test with sun.misc.Service looking for " + serviceClass); |
|
258 URLClassLoader loader = getLoader(baseURL); |
|
259 httpServer.reset(); |
|
260 |
|
261 Class<?> messageServiceClass = null; |
|
262 try { |
|
263 messageServiceClass = loader.loadClass(serviceClass); |
|
264 } catch (ClassNotFoundException cnfe) { |
|
265 System.err.println(cnfe); |
|
266 throw new RuntimeException("Error in test: " + cnfe); |
|
267 } |
|
268 |
|
269 Iterator<?> iterator = sun.misc.Service.providers(messageServiceClass, loader); |
|
270 if (expectToFind && !iterator.hasNext()) { |
|
271 debug(messageServiceClass + " NOT found."); |
|
272 return false; |
|
273 } |
|
274 |
|
275 while (iterator.hasNext()) { |
|
276 debug("found " + iterator.next() + " " + messageService); |
|
277 } |
|
278 |
|
279 debug("HttpServer: " + httpServer); |
|
280 |
|
281 if (!expectbDotJar && httpServer.bDotJar > 0) { |
|
282 debug("Unexpeced request sent to the httpserver for b.jar"); |
|
283 return false; |
|
284 } |
|
285 if (!expectcDotJar && httpServer.cDotJar > 0) { |
|
286 debug("Unexpeced request sent to the httpserver for c.jar"); |
|
287 return false; |
|
288 } |
|
289 |
|
290 return true; |
|
291 } |
238 } |
292 |
239 |
293 static boolean javaUtilServiceLoaderTest(URL baseURL, |
240 static boolean javaUtilServiceLoaderTest(URL baseURL, |
294 String serviceClass, |
241 String serviceClass, |
295 boolean expectToFind, |
242 boolean expectToFind, |