jdk/test/java/util/logging/bundlesearch/IndirectlyLoadABundle.java
changeset 17159 bb566a21b661
child 17487 77566e5979d1
equal deleted inserted replaced
17158:55eecadc4f82 17159:bb566a21b661
       
     1 /*
       
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.lang.reflect.InvocationTargetException;
       
    25 import java.lang.reflect.Method;
       
    26 import java.net.URL;
       
    27 import java.net.URLClassLoader;
       
    28 import java.nio.file.Paths;
       
    29 
       
    30 /**
       
    31  * This class is used to ensure that a resource bundle loadable by a classloader
       
    32  * is on the caller's stack, but not on the classpath or TCCL to ensure that
       
    33  * Logger.getLogger() can't load the bundle via a stack search
       
    34  *
       
    35  * @author Jim Gish
       
    36  */
       
    37 public class IndirectlyLoadABundle {
       
    38 
       
    39     private final static String rbName = "StackSearchableResource";
       
    40 
       
    41     public boolean loadAndTest() throws Throwable {
       
    42         // Find out where we are running from so we can setup the URLClassLoader URLs
       
    43         // test.src and test.classes will be set if running in jtreg, but probably
       
    44         // not otherwise
       
    45         String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
       
    46         String testClassesDir = System.getProperty("test.classes",
       
    47                 System.getProperty("user.dir"));
       
    48         String sep = System.getProperty("file.separator");
       
    49 
       
    50         URL[] urls = new URL[2];
       
    51 
       
    52         // Allow for both jtreg and standalone cases here
       
    53         urls[0] = Paths.get(testDir, "resources").toUri().toURL();
       
    54         urls[1] = Paths.get(testClassesDir).toUri().toURL();
       
    55 
       
    56         System.out.println("INFO: urls[0] = " + urls[0]);
       
    57         System.out.println("INFO: urls[1] = " + urls[1]);
       
    58 
       
    59         // Make sure we can find it via the URLClassLoader
       
    60         URLClassLoader yetAnotherResourceCL = new URLClassLoader(urls, null);
       
    61         if (!testForValidResourceSetup(yetAnotherResourceCL)) {
       
    62             throw new Exception("Couldn't directly load bundle " + rbName
       
    63                     + " as expected. Test config problem");
       
    64         }
       
    65         // But it shouldn't be available via the system classloader
       
    66         ClassLoader myCL = this.getClass().getClassLoader();
       
    67         if (testForValidResourceSetup(myCL)) {
       
    68             throw new Exception("Was able to directly load bundle " + rbName
       
    69                     + " from " + myCL + " but shouldn't have been"
       
    70                     + " able to. Test config problem");
       
    71         }
       
    72 
       
    73         Class<?> loadItUpClazz = Class.forName("LoadItUp", true, yetAnotherResourceCL);
       
    74         ClassLoader actual = loadItUpClazz.getClassLoader();
       
    75         if (actual != yetAnotherResourceCL) {
       
    76             throw new Exception("LoadItUp was loaded by an unexpected CL: " + actual);
       
    77         }
       
    78         Object loadItUp = loadItUpClazz.newInstance();
       
    79         Method testMethod = loadItUpClazz.getMethod("test", String.class);
       
    80         try {
       
    81             return (Boolean) testMethod.invoke(loadItUp, rbName);
       
    82         } catch (InvocationTargetException ex) {
       
    83             throw ex.getTargetException();
       
    84         }
       
    85     }
       
    86 
       
    87     private boolean testForValidResourceSetup(ClassLoader cl) {
       
    88         // First make sure the test environment is setup properly and the bundle actually
       
    89         // exists
       
    90         return ResourceBundleSearchTest.isOnClassPath(rbName, cl);
       
    91     }
       
    92 }