Path: blob/master/test/jdk/javax/naming/spi/DummyContextFactory.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.lang.ref.WeakReference;25import java.net.URL;26import java.net.URLClassLoader;27import javax.naming.Context;28import javax.naming.InitialContext;29import javax.naming.NamingException;30import javax.naming.NoInitialContextException;31import javax.naming.spi.InitialContextFactory;32import javax.naming.spi.NamingManager;33import java.util.Hashtable;3435public class DummyContextFactory implements InitialContextFactory {36static final String DUMMY_FACTORY = "DummyContextFactory";37static final String DUMMY_FACTORY2 = "DummyContextFactory2";38static final String MISSING_FACTORY = "NonExistant";39static int counter = 0;40ClassLoader origContextLoader = Thread.currentThread().getContextClassLoader();4142public static void main(String[] s) throws Exception {43DummyContextFactory dcf = new DummyContextFactory();44dcf.runTest();45}4647private void runTest() throws Exception {48final String classes = System.getProperty("url.dir", ".");49final URL curl = new File(classes).toURI().toURL();50URLClassLoader testLoader = new URLClassLoader(new URL[] {curl}, null);51WeakReference<URLClassLoader> weakRef = new WeakReference<>(testLoader);52Thread.currentThread().setContextClassLoader(testLoader);53Hashtable<String, String> env = new Hashtable<>();54env.put(Context.INITIAL_CONTEXT_FACTORY, DUMMY_FACTORY);55testContextCalls(env);5657// now test with another factory58Thread.currentThread().setContextClassLoader(testLoader);59env.put(Context.INITIAL_CONTEXT_FACTORY, DUMMY_FACTORY2);60testContextCalls(env);6162// one count is derived from a default constructor call (ignored for test)63// class associated with this ClassLoader should have 2 counts64if (counter != 2) {65throw new RuntimeException("wrong count: " + counter);66}6768// a test for handling non-existent classes69env.put(Context.INITIAL_CONTEXT_FACTORY, MISSING_FACTORY);70testBadContextCall(env);7172// test that loader gets GC'ed73testLoader = null;74System.gc();75while (weakRef.get() != null) {76Thread.sleep(100);77System.gc();78}79}8081private void testContextCalls(Hashtable<String, String> env) throws Exception {82// the context is returned here but it's the ContextFactory that83// we're mainly interested in. Hence the counter test.8485// 1st call populates the WeakHashMap86// Uses URLClassLoader87Context cxt = NamingManager.getInitialContext(env);8889// 2nd call uses cached factory90cxt = NamingManager.getInitialContext(env);9192Thread.currentThread().setContextClassLoader(origContextLoader);9394// 3rd call uses new factory95// AppClassLoader96cxt = NamingManager.getInitialContext(env);9798// test with null TCCL99// this shouldn't increase the count since a null TCCL100// means we default to System ClassLoader in this case (AppClassLoader)101Thread.currentThread().setContextClassLoader(null);102cxt = NamingManager.getInitialContext(env);103}104105private void testBadContextCall(Hashtable<String, String> env) throws Exception {106try {107Context cxt = NamingManager.getInitialContext(env);108throw new RuntimeException("Expected NoInitialContextException");109} catch (NoInitialContextException e) {110if (!(e.getCause() instanceof ClassNotFoundException)) {111throw new RuntimeException("unexpected cause", e.getCause());112}113}114}115116public DummyContextFactory() {117System.out.println("New DummyContextFactory " + (++counter));118//new Throwable().printStackTrace(System.out);119}120121@Override122public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {123return new DummyContext(environment);124}125126public class DummyContext extends InitialContext {127128private Hashtable<?, ?> env;129130DummyContext(Hashtable<?, ?> env) throws NamingException {131this.env = env;132}133134public Hashtable<?, ?> getEnvironment() {135return env;136}137}138}139140141