/* * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
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.