6330N/A/*
6330N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6330N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6330N/A *
6330N/A * This code is free software; you can redistribute it and/or modify it
6330N/A * under the terms of the GNU General Public License version 2 only, as
6330N/A * published by the Free Software Foundation.
6330N/A *
6330N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6330N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6330N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6330N/A * version 2 for more details (a copy is included in the LICENSE file that
6330N/A * accompanied this code).
6330N/A *
6330N/A * You should have received a copy of the GNU General Public License version
6330N/A * 2 along with this work; if not, write to the Free Software Foundation,
6330N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6330N/A *
6330N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6330N/A * or visit www.oracle.com if you need additional information or have any
6330N/A * questions.
6330N/A */
6330N/A
6330N/Aimport java.lang.reflect.InvocationTargetException;
6330N/Aimport java.lang.reflect.Method;
6394N/Aimport java.net.MalformedURLException;
6330N/Aimport java.net.URL;
6330N/Aimport java.net.URLClassLoader;
6394N/Aimport java.nio.file.Paths;
6394N/Aimport java.util.logging.Logger;
6330N/A
6330N/A/**
6330N/A * This class is used to ensure that a resource bundle loadable by a classloader
6394N/A * is on the caller's stack, but not on the classpath or TCCL. It tests that
6394N/A * Logger.getLogger() can load the bundle via the immediate caller's classloader
6330N/A *
6330N/A * @author Jim Gish
6330N/A */
6330N/Apublic class IndirectlyLoadABundle {
6330N/A
6394N/A private final static String rbName = "CallerSearchableResource";
6330N/A
6330N/A public boolean loadAndTest() throws Throwable {
6330N/A // Make sure we can find it via the URLClassLoader
6394N/A URLClassLoader yetAnotherResourceCL = new URLClassLoader(getURLs(), null);
6330N/A if (!testForValidResourceSetup(yetAnotherResourceCL)) {
6330N/A throw new Exception("Couldn't directly load bundle " + rbName
6330N/A + " as expected. Test config problem");
6330N/A }
6330N/A // But it shouldn't be available via the system classloader
6330N/A ClassLoader myCL = this.getClass().getClassLoader();
6330N/A if (testForValidResourceSetup(myCL)) {
6330N/A throw new Exception("Was able to directly load bundle " + rbName
6330N/A + " from " + myCL + " but shouldn't have been"
6330N/A + " able to. Test config problem");
6330N/A }
6330N/A
6394N/A Class<?> loadItUpClazz = Class.forName("LoadItUp1", true,
6394N/A yetAnotherResourceCL);
6330N/A ClassLoader actual = loadItUpClazz.getClassLoader();
6330N/A if (actual != yetAnotherResourceCL) {
6394N/A throw new Exception("LoadItUp1 was loaded by an unexpected CL: " + actual);
6330N/A }
6330N/A Object loadItUp = loadItUpClazz.newInstance();
6394N/A Method testMethod = loadItUpClazz.getMethod("getLogger", String.class, String.class);
6330N/A try {
6394N/A return (Logger)testMethod.invoke(loadItUp, "NestedLogger1", rbName) != null;
6394N/A } catch (InvocationTargetException ex) {
6394N/A throw ex.getTargetException();
6394N/A }
6394N/A }
6394N/A
6394N/A public boolean testGetAnonymousLogger() throws Throwable {
6394N/A // Test getAnonymousLogger()
6394N/A URLClassLoader loadItUpCL = new URLClassLoader(getURLs(), null);
6394N/A Class<?> loadItUpClazz = Class.forName("LoadItUp1", true, loadItUpCL);
6394N/A ClassLoader actual = loadItUpClazz.getClassLoader();
6394N/A if (actual != loadItUpCL) {
6394N/A throw new Exception("LoadItUp1 was loaded by an unexpected CL: "
6394N/A + actual);
6394N/A }
6394N/A Object loadItUpAnon = loadItUpClazz.newInstance();
6394N/A Method testAnonMethod = loadItUpClazz.getMethod("getAnonymousLogger",
6394N/A String.class);
6394N/A try {
6394N/A return (Logger)testAnonMethod.invoke(loadItUpAnon, rbName) != null;
6330N/A } catch (InvocationTargetException ex) {
6330N/A throw ex.getTargetException();
6330N/A }
6330N/A }
6330N/A
6394N/A
6394N/A public boolean testGetLoggerGetLoggerWithBundle() throws Throwable {
6394N/A // test getLogger("NestedLogger2"); followed by
6394N/A // getLogger("NestedLogger2", rbName) to see if the bundle is found
6394N/A //
6394N/A URL[] urls = getURLs();
6394N/A if (getLoggerWithNewCL(urls, "NestedLogger2", null)) {
6394N/A return getLoggerWithNewCL(urls, "NestedLogger2", rbName);
6394N/A
6394N/A } else {
6394N/A throw new Exception("TEST FAILED: first call to getLogger() failed "
6394N/A + " in IndirectlyLoadABundle."
6394N/A + "testGetLoggerGetLoggerWithBundle");
6394N/A }
6394N/A }
6394N/A
6394N/A private URL[] getURLs() throws MalformedURLException {
6394N/A // Find out where we are running from so we can setup the URLClassLoader URLs
6394N/A // test.src and test.classes will be set if running in jtreg, but probably
6394N/A // not otherwise
6394N/A String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
6394N/A String testClassesDir = System.getProperty("test.classes",
6394N/A System.getProperty("user.dir"));
6394N/A URL[] urls = new URL[2];
6394N/A // Allow for both jtreg and standalone cases here
6394N/A urls[0] = Paths.get(testDir, "resources").toUri().toURL();
6394N/A urls[1] = Paths.get(testClassesDir).toUri().toURL();
6394N/A
6394N/A return urls;
6394N/A }
6394N/A
6394N/A private boolean getLoggerWithNewCL(URL[] urls, String loggerName,
6394N/A String bundleName) throws Throwable {
6394N/A Logger result = null;;
6394N/A // Test getLogger("foo"); getLogger("foo", "rbName");
6394N/A // First do the getLogger() call with no bundle name
6394N/A URLClassLoader getLoggerCL = new URLClassLoader(urls, null);
6394N/A Class<?> loadItUpClazz1 = Class.forName("LoadItUp1", true, getLoggerCL);
6394N/A ClassLoader actual = loadItUpClazz1.getClassLoader();
6394N/A if (actual != getLoggerCL) {
6394N/A throw new Exception("LoadItUp1 was loaded by an unexpected CL: "
6394N/A + actual);
6394N/A }
6394N/A Object loadItUp1 = loadItUpClazz1.newInstance();
6394N/A if (bundleName != null) {
6394N/A Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger",
6394N/A String.class,
6394N/A String.class);
6394N/A try {
6394N/A result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName,
6394N/A bundleName);
6394N/A } catch (InvocationTargetException ex) {
6394N/A throw ex.getTargetException();
6394N/A }
6394N/A } else {
6394N/A Method getLoggerMethod = loadItUpClazz1.getMethod("getLogger",
6394N/A String.class);
6394N/A try {
6394N/A result = (Logger) getLoggerMethod.invoke(loadItUp1, loggerName);
6394N/A } catch (InvocationTargetException ex) {
6394N/A throw ex.getTargetException();
6394N/A }
6394N/A }
6394N/A return result != null;
6394N/A }
6394N/A
6330N/A private boolean testForValidResourceSetup(ClassLoader cl) {
6394N/A // First make sure the test environment is setup properly and the bundle
6394N/A // actually exists
6330N/A return ResourceBundleSearchTest.isOnClassPath(rbName, cl);
6330N/A }
6330N/A}