privatevoid runTest() throws Exception { final String classes = System.getProperty("url.dir", "."); final URL curl = new File(classes).toURI().toURL();
URLClassLoader testLoader = new URLClassLoader(new URL[] {curl}, null);
WeakReference<URLClassLoader> weakRef = new WeakReference<>(testLoader); Thread.currentThread().setContextClassLoader(testLoader);
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, DUMMY_FACTORY);
testContextCalls(env);
// now test with another factory Thread.currentThread().setContextClassLoader(testLoader);
env.put(Context.INITIAL_CONTEXT_FACTORY, DUMMY_FACTORY2);
testContextCalls(env);
// one count is derived from a default constructor call (ignored for test) // class associated with this ClassLoader should have 2 counts if (counter != 2) { thrownew RuntimeException("wrong count: " + counter);
}
// a test for handling non-existent classes
env.put(Context.INITIAL_CONTEXT_FACTORY, MISSING_FACTORY);
testBadContextCall(env);
// test that loader gets GC'ed
testLoader = null;
System.gc(); while (weakRef.get() != null) { Thread.sleep(100);
System.gc();
}
}
privatevoid testContextCalls(Hashtable<String, String> env) throws Exception { // the context is returned here but it's the ContextFactory that // we're mainly interested in. Hence the counter test.
// test with null TCCL // this shouldn't increase the count since a null TCCL // means we default to System ClassLoader in this case (AppClassLoader) Thread.currentThread().setContextClassLoader(null);
cxt = NamingManager.getInitialContext(env);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.