ResourceBundleSearchTest.java revision 6358
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
6330N/A * @bug 8010127
6330N/A * @summary Remove the stack search for a resource bundle Logger to use
6330N/A * @author Jim Gish
6330N/A * @build ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp
6330N/A * @run main ResourceBundleSearchTest
6330N/A */
6358N/Aimport java.io.File;
6330N/Aimport java.net.URL;
6330N/Aimport java.net.URLClassLoader;
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
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;
6330N/A private static List<String> msgs = new ArrayList<String>();
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 String testClasses = System.getProperty("test.classes");
6330N/A System.out.println( "test.classes = " + testClasses );
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 String sep = System.getProperty("file.separator");
6330N/A
6330N/A URL[] urls = new URL[1];
6358N/A
6358N/A urls[0] = new File( testDir + sep + "resources" + sep ).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
6330N/A assertFalse(testGetBundleFromStackSearch(), "testGetBundleFromStackSearch");
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.
6330N/A assertTrue(
6330N/A testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader),
6330N/A "testGetBundleFromTCCL");
6330N/A
6330N/A // Test 3 - Can we find a Logger bundle from the classpath? We should be
6330N/A // able to, but ....
6330N/A // We check to see if the bundle is on the classpath or not so that this
6330N/A // will work standalone. In the case of jtreg/samevm,
6330N/A // the resource bundles are not on the classpath. Running standalone
6330N/A // (or othervm), they are
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),
6330N/A "testGetBundleFromSystemClassLoader");
6330N/A } else {
6330N/A debug("We should not be able to see " + PROP_RB_NAME + " on the classpath");
6330N/A assertFalse(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
6330N/A "testGetBundleFromSystemClassLoader");
6330N/A }
6330N/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)
6330N/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
6330N/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
6330N/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
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 "
6330N/A + bundleName);
6330N/A return false;
6330N/A }
6330N/A msgs.add("INFO: testGetBundleFromSystemClassLoader() found the bundle "
6330N/A + bundleName);
6330N/A return true;
6330N/A }
6330N/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(),
6330N/A bundleName);
6330N/A msg = "INFO: LoggingRunnable() found the bundle " + bundleName
6330N/A + (setTCCL ? " with " : " without ") + "setting the TCCL";
6330N/A foundBundle = true;
6330N/A } catch (MissingResourceException re) {
6330N/A msg = "INFO: LoggingRunnable() did not find the bundle " + bundleName
6330N/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}