jdk/test/java/util/logging/bundlesearch/ResourceBundleSearchTest.java
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
child 17487 77566e5979d1
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17159
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     1
/*
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     4
 *
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     8
 *
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    13
 * accompanied this code).
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    14
 *
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    18
 *
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    21
 * questions.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    22
 */
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    23
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    24
/*
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    25
 * @test
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    26
 * @bug     8002070
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    27
 * @summary Remove the stack search for a resource bundle Logger to use
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    28
 * @author  Jim Gish
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    29
 * @build  ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    30
 * @run main ResourceBundleSearchTest
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    31
 */
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    32
import java.net.URL;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    33
import java.net.URLClassLoader;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    34
import java.nio.file.Paths;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    35
import java.util.ArrayList;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    36
import java.util.List;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    37
import java.util.Locale;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    38
import java.util.MissingResourceException;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    39
import java.util.ResourceBundle;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    40
import java.util.logging.Logger;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    41
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    42
public class ResourceBundleSearchTest {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    43
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    44
    private final static boolean DEBUG = false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    45
    private final static String LOGGER_PREFIX = "myLogger.";
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    46
    private static int loggerNum = 0;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    47
    private final static String PROP_RB_NAME = "ClassPathTestBundle";
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    48
    private final static String TCCL_TEST_BUNDLE = "ContextClassLoaderTestBundle";
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    49
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    50
    private static int numPass = 0;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    51
    private static int numFail = 0;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    52
    private static List<String> msgs = new ArrayList<>();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    53
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    54
    public static void main(String[] args) throws Throwable {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    55
        ResourceBundleSearchTest test = new ResourceBundleSearchTest();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    56
        test.runTests();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    57
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    58
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    59
    private void runTests() throws Throwable {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    60
        // ensure we are using en as the default Locale so we can find the resource
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    61
        Locale.setDefault(Locale.ENGLISH);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    62
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    63
        String testClasses = System.getProperty("test.classes");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    64
        System.out.println( "test.classes = " + testClasses );
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    65
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    66
        ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    67
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    68
        // Find out where we are running from so we can setup the URLClassLoader URL
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    69
        String userDir = System.getProperty("user.dir");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    70
        String testDir = System.getProperty("test.src", userDir);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    71
        String sep = System.getProperty("file.separator");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    72
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    73
        URL[] urls = new URL[1];
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    74
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    75
        urls[0] = Paths.get(testDir, "resources").toUri().toURL();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    76
        URLClassLoader rbClassLoader = new URLClassLoader(urls);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    77
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    78
        // Test 1 - can we find a Logger bundle from doing a stack search?
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    79
        // We shouldn't be able to
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    80
        assertFalse(testGetBundleFromStackSearch(), "testGetBundleFromStackSearch");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    81
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    82
        // Test 2 - can we find a Logger bundle off of the Thread context class
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    83
        // loader? We should be able to.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    84
        assertTrue(
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    85
                testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader),
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    86
                "testGetBundleFromTCCL");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    87
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    88
        // Test 3 - Can we find a Logger bundle from the classpath?  We should be
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    89
        // able to, but ....
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    90
        // We check to see if the bundle is on the classpath or not so that this
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    91
        // will work standalone.  In the case of jtreg/samevm,
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    92
        // the resource bundles are not on the classpath.  Running standalone
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    93
        // (or othervm), they are
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    94
        if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    95
            debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    96
            assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    97
                    "testGetBundleFromSystemClassLoader");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    98
        } else {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
    99
            debug("We should not be able to see " + PROP_RB_NAME + " on the classpath");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   100
            assertFalse(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   101
                    "testGetBundleFromSystemClassLoader");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   102
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   103
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   104
        report();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   105
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   106
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   107
    private void report() throws Exception {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   108
        System.out.println("Num passed = " + numPass + " Num failed = " + numFail);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   109
        if (numFail > 0) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   110
            // We only care about the messages if they were errors
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   111
            for (String msg : msgs) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   112
                System.out.println(msg);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   113
            }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   114
            throw new Exception(numFail + " out of " + (numPass + numFail)
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   115
                    + " tests failed.");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   116
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   117
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   118
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   119
    public void assertTrue(boolean testResult, String testName) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   120
        if (testResult) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   121
            numPass++;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   122
        } else {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   123
            numFail++;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   124
            System.out.println("FAILED: " + testName
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   125
                    + " was supposed to return true but did NOT!");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   126
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   127
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   128
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   129
    public void assertFalse(boolean testResult, String testName) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   130
        if (!testResult) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   131
            numPass++;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   132
        } else {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   133
            numFail++;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   134
            System.out.println("FAILED: " + testName
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   135
                    + " was supposed to return false but did NOT!");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   136
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   137
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   138
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   139
    public boolean testGetBundleFromStackSearch() throws Throwable {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   140
        // This should fail.  This was the old functionality to search up the
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   141
        // caller's call stack
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   142
        IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   143
        return indirectLoader.loadAndTest();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   144
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   145
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   146
    public boolean testGetBundleFromTCCL(String bundleName,
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   147
            ClassLoader setOnTCCL) throws InterruptedException {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   148
        // This should succeed.  We should be able to get the bundle from the
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   149
        // thread context class loader
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   150
        debug("Looking for " + bundleName + " using TCCL");
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   151
        LoggingThread lr = new LoggingThread(bundleName, setOnTCCL);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   152
        lr.start();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   153
        synchronized (lr) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   154
            try {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   155
                lr.wait();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   156
            } catch (InterruptedException ex) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   157
                throw ex;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   158
            }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   159
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   160
        msgs.add(lr.msg);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   161
        return lr.foundBundle;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   162
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   163
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   164
    /*
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   165
     * @param String bundleClass
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   166
     * @param ClassLoader to use for search
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   167
     * @return true iff bundleClass is on system classpath
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   168
     */
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   169
    public static boolean isOnClassPath(String baseName, ClassLoader cl) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   170
        ResourceBundle rb = null;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   171
        try {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   172
            rb = ResourceBundle.getBundle(baseName, Locale.getDefault(), cl);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   173
            System.out.println("INFO: Found bundle " + baseName + " on " + cl);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   174
        } catch (MissingResourceException e) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   175
            System.out.println("INFO: Could not find bundle " + baseName + " on " + cl);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   176
            return false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   177
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   178
        return (rb != null);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   179
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   180
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   181
    private static String newLoggerName() {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   182
        // we need a new logger name every time we attempt to find a bundle via
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   183
        // the Logger.getLogger call, so we'll simply tack on an integer which
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   184
        // we increment each time this is called
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   185
        loggerNum++;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   186
        return LOGGER_PREFIX + loggerNum;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   187
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   188
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   189
    public boolean testGetBundleFromSystemClassLoader(String bundleName) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   190
        // this should succeed if the bundle is on the system classpath.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   191
        try {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   192
            Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   193
                    bundleName);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   194
        } catch (MissingResourceException re) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   195
            msgs.add("INFO: testGetBundleFromSystemClassLoader() did not find bundle "
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   196
                    + bundleName);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   197
            return false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   198
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   199
        msgs.add("INFO: testGetBundleFromSystemClassLoader() found the bundle "
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   200
                + bundleName);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   201
        return true;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   202
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   203
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   204
    public static class LoggingThread extends Thread {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   205
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   206
        boolean foundBundle = false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   207
        String msg = null;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   208
        ClassLoader clToSetOnTCCL = null;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   209
        String bundleName = null;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   210
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   211
        public LoggingThread(String bundleName) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   212
            this.bundleName = bundleName;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   213
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   214
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   215
        public LoggingThread(String bundleName, ClassLoader setOnTCCL) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   216
            this.clToSetOnTCCL = setOnTCCL;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   217
            this.bundleName = bundleName;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   218
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   219
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   220
        public void run() {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   221
            boolean setTCCL = false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   222
            try {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   223
                if (clToSetOnTCCL != null) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   224
                    Thread.currentThread().setContextClassLoader(clToSetOnTCCL);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   225
                    setTCCL = true;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   226
                }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   227
                // this should succeed if the bundle is on the system classpath.
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   228
                try {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   229
                    Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   230
                            bundleName);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   231
                    msg = "INFO: LoggingRunnable() found the bundle " + bundleName
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   232
                            + (setTCCL ? " with " : " without ") + "setting the TCCL";
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   233
                    foundBundle = true;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   234
                } catch (MissingResourceException re) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   235
                    msg = "INFO: LoggingRunnable() did not find the bundle " + bundleName
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   236
                            + (setTCCL ? " with " : " without ") + "setting the TCCL";
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   237
                    foundBundle = false;
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   238
                }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   239
            } catch (Throwable e) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   240
                e.printStackTrace();
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   241
                System.exit(1);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   242
            }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   243
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   244
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   245
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   246
    private void debug(String msg) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   247
        if (DEBUG) {
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   248
            System.out.println(msg);
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   249
        }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   250
    }
bb566a21b661 8012005: LogManager needs test to ensure stack trace is not being done to find bundle
jgish
parents:
diff changeset
   251
}