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/A/*
6330N/A * @test
6394N/A * @bug 8010127 8013380
6330N/A * @summary Remove the stack search for a resource bundle Logger to use
6330N/A * @author Jim Gish
6394N/A * @build ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp1 LoadItUp2 TwiceIndirectlyLoadABundle LoadItUp2Invoker
6394N/A * @run main/othervm ResourceBundleSearchTest
6397N/A * @run main/othervm -Djdk.logging.allowStackWalkSearch=true ResourceBundleSearchTest
6330N/A */
6330N/Aimport java.net.URL;
6330N/Aimport java.net.URLClassLoader;
6394N/Aimport java.nio.file.Paths;
6330N/Aimport java.util.ArrayList;
6330N/Aimport java.util.List;
6330N/Aimport java.util.Locale;
6330N/Aimport java.util.MissingResourceException;
6330N/Aimport java.util.ResourceBundle;
6330N/Aimport java.util.logging.Logger;
6330N/A
6394N/A/**
6394N/A * This class tests various scenarios of loading resource bundles from
6394N/A * java.util.logging. Since jtreg uses the logging system, it is necessary to
6394N/A * run these tests using othervm mode to ensure no interference from logging
6394N/A * initialization by jtreg
6394N/A */
6330N/Apublic class ResourceBundleSearchTest {
6330N/A
6330N/A private final static boolean DEBUG = false;
6330N/A private final static String LOGGER_PREFIX = "myLogger.";
6330N/A private static int loggerNum = 0;
6330N/A private final static String PROP_RB_NAME = "ClassPathTestBundle";
6330N/A private final static String TCCL_TEST_BUNDLE = "ContextClassLoaderTestBundle";
6330N/A
6330N/A private static int numPass = 0;
6330N/A private static int numFail = 0;
6394N/A private static List<String> msgs = new ArrayList<>();
6330N/A
6330N/A public static void main(String[] args) throws Throwable {
6330N/A ResourceBundleSearchTest test = new ResourceBundleSearchTest();
6330N/A test.runTests();
6330N/A }
6330N/A
6330N/A private void runTests() throws Throwable {
6330N/A // ensure we are using en as the default Locale so we can find the resource
6330N/A Locale.setDefault(Locale.ENGLISH);
6330N/A
6330N/A ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
6330N/A
6330N/A // Find out where we are running from so we can setup the URLClassLoader URL
6330N/A String userDir = System.getProperty("user.dir");
6330N/A String testDir = System.getProperty("test.src", userDir);
6330N/A
6330N/A URL[] urls = new URL[1];
6358N/A
6394N/A urls[0] = Paths.get(testDir, "resources").toUri().toURL();
6330N/A URLClassLoader rbClassLoader = new URLClassLoader(urls);
6330N/A
6330N/A // Test 1 - can we find a Logger bundle from doing a stack search?
6330N/A // We shouldn't be able to
6397N/A // unless -Djdk.logging.allowStackWalkSearch=true is set
6397N/A
6397N/A boolean allowStackWalkSearch = Boolean.getBoolean("jdk.logging.allowStackWalkSearch");
6397N/A if (allowStackWalkSearch) {
6397N/A assertTrue(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch");
6397N/A } else {
6397N/A // default behavior
6397N/A assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch");
6397N/A }
6330N/A
6330N/A // Test 2 - can we find a Logger bundle off of the Thread context class
6330N/A // loader? We should be able to.
6394N/A assertTrue(testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader),
6394N/A "2-testGetBundleFromTCCL");
6330N/A
6330N/A // Test 3 - Can we find a Logger bundle from the classpath? We should be
6394N/A // able to. We'll first check to make sure the setup is correct and
6394N/A // it actually is on the classpath before checking whether logging
6394N/A // can see it there.
6330N/A if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
6330N/A debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
6330N/A assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
6394N/A "3-testGetBundleFromSystemClassLoader");
6330N/A } else {
6394N/A throw new Exception("TEST SETUP FAILURE: Cannot see " + PROP_RB_NAME
6394N/A + " on the classpath");
6330N/A }
6330N/A
6394N/A // Test 4 - we should be able to find a bundle from the caller's
6394N/A // classloader, but only one level up.
6394N/A assertTrue(testGetBundleFromCallersClassLoader(),
6394N/A "4-testGetBundleFromCallersClassLoader");
6394N/A
6394N/A // Test 5 - this ensures that getAnonymousLogger(String rbName)
6394N/A // can find the bundle from the caller's classloader
6394N/A assertTrue(testGetAnonymousLogger(), "5-testGetAnonymousLogger");
6394N/A
6394N/A // Test 6 - first call getLogger("myLogger").
6394N/A // Then call getLogger("myLogger","bundleName") from a different ClassLoader
6394N/A // Make sure we find the bundle
6397N/A if (!allowStackWalkSearch) {
6397N/A assertTrue(testGetBundleFromSecondCallersClassLoader(),
6397N/A "6-testGetBundleFromSecondCallersClassLoader");
6397N/A }
6394N/A
6330N/A report();
6330N/A }
6330N/A
6330N/A private void report() throws Exception {
6330N/A System.out.println("Num passed = " + numPass + " Num failed = " + numFail);
6330N/A if (numFail > 0) {
6330N/A // We only care about the messages if they were errors
6330N/A for (String msg : msgs) {
6330N/A System.out.println(msg);
6330N/A }
6330N/A throw new Exception(numFail + " out of " + (numPass + numFail)
6394N/A + " tests failed.");
6330N/A }
6330N/A }
6330N/A
6330N/A public void assertTrue(boolean testResult, String testName) {
6330N/A if (testResult) {
6330N/A numPass++;
6330N/A } else {
6330N/A numFail++;
6330N/A System.out.println("FAILED: " + testName
6394N/A + " was supposed to return true but did NOT!");
6330N/A }
6330N/A }
6330N/A
6330N/A public void assertFalse(boolean testResult, String testName) {
6330N/A if (!testResult) {
6330N/A numPass++;
6330N/A } else {
6330N/A numFail++;
6330N/A System.out.println("FAILED: " + testName
6394N/A + " was supposed to return false but did NOT!");
6330N/A }
6330N/A }
6330N/A
6330N/A public boolean testGetBundleFromStackSearch() throws Throwable {
6330N/A // This should fail. This was the old functionality to search up the
6330N/A // caller's call stack
6394N/A TwiceIndirectlyLoadABundle indirectLoader = new TwiceIndirectlyLoadABundle();
6394N/A return indirectLoader.loadAndTest();
6394N/A }
6394N/A
6394N/A public boolean testGetBundleFromCallersClassLoader() throws Throwable {
6394N/A // This should pass. This exercises getting the bundle using the
6394N/A // class loader of the caller (one level up)
6330N/A IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
6330N/A return indirectLoader.loadAndTest();
6330N/A }
6330N/A
6330N/A public boolean testGetBundleFromTCCL(String bundleName,
6330N/A ClassLoader setOnTCCL) throws InterruptedException {
6330N/A // This should succeed. We should be able to get the bundle from the
6330N/A // thread context class loader
6330N/A debug("Looking for " + bundleName + " using TCCL");
6330N/A LoggingThread lr = new LoggingThread(bundleName, setOnTCCL);
6330N/A lr.start();
6330N/A synchronized (lr) {
6330N/A try {
6330N/A lr.wait();
6330N/A } catch (InterruptedException ex) {
6330N/A throw ex;
6330N/A }
6330N/A }
6330N/A msgs.add(lr.msg);
6330N/A return lr.foundBundle;
6330N/A }
6330N/A
6330N/A /*
6330N/A * @param String bundleClass
6330N/A * @param ClassLoader to use for search
6330N/A * @return true iff bundleClass is on system classpath
6330N/A */
6330N/A public static boolean isOnClassPath(String baseName, ClassLoader cl) {
6330N/A ResourceBundle rb = null;
6330N/A try {
6330N/A rb = ResourceBundle.getBundle(baseName, Locale.getDefault(), cl);
6330N/A System.out.println("INFO: Found bundle " + baseName + " on " + cl);
6330N/A } catch (MissingResourceException e) {
6330N/A System.out.println("INFO: Could not find bundle " + baseName + " on " + cl);
6330N/A return false;
6330N/A }
6330N/A return (rb != null);
6330N/A }
6330N/A
6330N/A private static String newLoggerName() {
6330N/A // we need a new logger name every time we attempt to find a bundle via
6330N/A // the Logger.getLogger call, so we'll simply tack on an integer which
6330N/A // we increment each time this is called
6330N/A loggerNum++;
6330N/A return LOGGER_PREFIX + loggerNum;
6330N/A }
6330N/A
6330N/A public boolean testGetBundleFromSystemClassLoader(String bundleName) {
6330N/A // this should succeed if the bundle is on the system classpath.
6330N/A try {
6330N/A Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
6330N/A bundleName);
6330N/A } catch (MissingResourceException re) {
6330N/A msgs.add("INFO: testGetBundleFromSystemClassLoader() did not find bundle "
6394N/A + bundleName);
6330N/A return false;
6330N/A }
6330N/A msgs.add("INFO: testGetBundleFromSystemClassLoader() found the bundle "
6394N/A + bundleName);
6330N/A return true;
6330N/A }
6330N/A
6394N/A private boolean testGetAnonymousLogger() throws Throwable {
6394N/A // This should pass. This exercises getting the bundle using the
6394N/A // class loader of the caller (one level up) when calling
6394N/A // Logger.getAnonymousLogger(String rbName)
6394N/A IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
6394N/A return indirectLoader.testGetAnonymousLogger();
6394N/A }
6394N/A
6394N/A private boolean testGetBundleFromSecondCallersClassLoader() throws Throwable {
6394N/A // This should pass. This exercises getting the bundle using the
6394N/A // class loader of the caller (one level up)
6394N/A IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
6394N/A return indirectLoader.testGetLoggerGetLoggerWithBundle();
6394N/A }
6394N/A
6330N/A public static class LoggingThread extends Thread {
6330N/A
6330N/A boolean foundBundle = false;
6330N/A String msg = null;
6330N/A ClassLoader clToSetOnTCCL = null;
6330N/A String bundleName = null;
6330N/A
6330N/A public LoggingThread(String bundleName) {
6330N/A this.bundleName = bundleName;
6330N/A }
6330N/A
6330N/A public LoggingThread(String bundleName, ClassLoader setOnTCCL) {
6330N/A this.clToSetOnTCCL = setOnTCCL;
6330N/A this.bundleName = bundleName;
6330N/A }
6330N/A
6330N/A public void run() {
6330N/A boolean setTCCL = false;
6330N/A try {
6330N/A if (clToSetOnTCCL != null) {
6330N/A Thread.currentThread().setContextClassLoader(clToSetOnTCCL);
6330N/A setTCCL = true;
6330N/A }
6330N/A // this should succeed if the bundle is on the system classpath.
6330N/A try {
6330N/A Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
6394N/A bundleName);
6394N/A msg = "INFO: LoggingThread.run() found the bundle " + bundleName
6394N/A + (setTCCL ? " with " : " without ") + "setting the TCCL";
6330N/A foundBundle = true;
6330N/A } catch (MissingResourceException re) {
6394N/A msg = "INFO: LoggingThread.run() did not find the bundle " + bundleName
6394N/A + (setTCCL ? " with " : " without ") + "setting the TCCL";
6330N/A foundBundle = false;
6330N/A }
6330N/A } catch (Throwable e) {
6330N/A e.printStackTrace();
6330N/A System.exit(1);
6330N/A }
6330N/A }
6330N/A }
6330N/A
6330N/A private void debug(String msg) {
6330N/A if (DEBUG) {
6330N/A System.out.println(msg);
6330N/A }
6330N/A }
6330N/A}