test/java/util/logging/bundlesearch/ResourceBundleSearchTest.java

Print this page
rev 7022 : 8013380: Removal of stack walk to find resource bundle breaks Glassfish startup
Summary: Use caller's classloader to load resource as an alternative to thread context classloader and system classloader
Reviewed-by: duke


   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 /*
  25  * @test
  26  * @bug     8002070
  27  * @summary Remove the stack search for a resource bundle Logger to use
  28  * @author  Jim Gish
  29  * @build  ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp
  30  * @run main ResourceBundleSearchTest
  31  */
  32 import java.net.URL;
  33 import java.net.URLClassLoader;
  34 import java.nio.file.Paths;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Locale;
  38 import java.util.MissingResourceException;
  39 import java.util.ResourceBundle;
  40 import java.util.logging.Logger;
  41 






  42 public class ResourceBundleSearchTest {
  43 
  44     private final static boolean DEBUG = false;
  45     private final static String LOGGER_PREFIX = "myLogger.";
  46     private static int loggerNum = 0;
  47     private final static String PROP_RB_NAME = "ClassPathTestBundle";
  48     private final static String TCCL_TEST_BUNDLE = "ContextClassLoaderTestBundle";
  49 
  50     private static int numPass = 0;
  51     private static int numFail = 0;
  52     private static List<String> msgs = new ArrayList<>();
  53 
  54     public static void main(String[] args) throws Throwable {
  55         ResourceBundleSearchTest test = new ResourceBundleSearchTest();
  56         test.runTests();
  57     }
  58 
  59     private void runTests() throws Throwable {
  60         // ensure we are using en as the default Locale so we can find the resource
  61         Locale.setDefault(Locale.ENGLISH);
  62 
  63         String testClasses = System.getProperty("test.classes");
  64         System.out.println( "test.classes = " + testClasses );
  65 
  66         ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
  67 
  68         // Find out where we are running from so we can setup the URLClassLoader URL
  69         String userDir = System.getProperty("user.dir");
  70         String testDir = System.getProperty("test.src", userDir);
  71         String sep = System.getProperty("file.separator");
  72 
  73         URL[] urls = new URL[1];
  74 
  75         urls[0] = Paths.get(testDir, "resources").toUri().toURL();
  76         URLClassLoader rbClassLoader = new URLClassLoader(urls);
  77 
  78         // Test 1 - can we find a Logger bundle from doing a stack search?
  79         // We shouldn't be able to
  80         assertFalse(testGetBundleFromStackSearch(), "testGetBundleFromStackSearch");
  81 
  82         // Test 2 - can we find a Logger bundle off of the Thread context class
  83         // loader? We should be able to.
  84         assertTrue(
  85                 testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader),
  86                 "testGetBundleFromTCCL");
  87 
  88         // Test 3 - Can we find a Logger bundle from the classpath?  We should be
  89         // able to, but ....
  90         // We check to see if the bundle is on the classpath or not so that this
  91         // will work standalone.  In the case of jtreg/samevm,
  92         // the resource bundles are not on the classpath.  Running standalone
  93         // (or othervm), they are
  94         if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
  95             debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
  96             assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
  97                     "testGetBundleFromSystemClassLoader");
  98         } else {
  99             debug("We should not be able to see " + PROP_RB_NAME + " on the classpath");
 100             assertFalse(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
 101                     "testGetBundleFromSystemClassLoader");
 102         }
 103 





 104         report();
 105     }
 106 
 107     private void report() throws Exception {
 108         System.out.println("Num passed = " + numPass + " Num failed = " + numFail);
 109         if (numFail > 0) {
 110             // We only care about the messages if they were errors
 111             for (String msg : msgs) {
 112                 System.out.println(msg);
 113             }
 114             throw new Exception(numFail + " out of " + (numPass + numFail)
 115                     + " tests failed.");
 116         }
 117     }
 118 
 119     public void assertTrue(boolean testResult, String testName) {
 120         if (testResult) {
 121             numPass++;
 122         } else {
 123             numFail++;
 124             System.out.println("FAILED: " + testName
 125                     + " was supposed to return true but did NOT!");
 126         }
 127     }
 128 
 129     public void assertFalse(boolean testResult, String testName) {
 130         if (!testResult) {
 131             numPass++;
 132         } else {
 133             numFail++;
 134             System.out.println("FAILED: " + testName
 135                     + " was supposed to return false but did NOT!");
 136         }
 137     }
 138 
 139     public boolean testGetBundleFromStackSearch() throws Throwable {
 140         // This should fail.  This was the old functionality to search up the
 141         // caller's call stack







 142         IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
 143         return indirectLoader.loadAndTest();
 144     }
 145 
 146     public boolean testGetBundleFromTCCL(String bundleName,
 147             ClassLoader setOnTCCL) throws InterruptedException {
 148         // This should succeed.  We should be able to get the bundle from the
 149         // thread context class loader
 150         debug("Looking for " + bundleName + " using TCCL");
 151         LoggingThread lr = new LoggingThread(bundleName, setOnTCCL);
 152         lr.start();
 153         synchronized (lr) {
 154             try {
 155                 lr.wait();
 156             } catch (InterruptedException ex) {
 157                 throw ex;
 158             }
 159         }
 160         msgs.add(lr.msg);
 161         return lr.foundBundle;


 211         public LoggingThread(String bundleName) {
 212             this.bundleName = bundleName;
 213         }
 214 
 215         public LoggingThread(String bundleName, ClassLoader setOnTCCL) {
 216             this.clToSetOnTCCL = setOnTCCL;
 217             this.bundleName = bundleName;
 218         }
 219 
 220         public void run() {
 221             boolean setTCCL = false;
 222             try {
 223                 if (clToSetOnTCCL != null) {
 224                     Thread.currentThread().setContextClassLoader(clToSetOnTCCL);
 225                     setTCCL = true;
 226                 }
 227                 // this should succeed if the bundle is on the system classpath.
 228                 try {
 229                     Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
 230                             bundleName);
 231                     msg = "INFO: LoggingRunnable() found the bundle " + bundleName
 232                             + (setTCCL ? " with " : " without ") + "setting the TCCL";
 233                     foundBundle = true;
 234                 } catch (MissingResourceException re) {
 235                     msg = "INFO: LoggingRunnable() did not find the bundle " + bundleName
 236                             + (setTCCL ? " with " : " without ") + "setting the TCCL";
 237                     foundBundle = false;
 238                 }
 239             } catch (Throwable e) {
 240                 e.printStackTrace();
 241                 System.exit(1);
 242             }
 243         }
 244     }
 245 
 246     private void debug(String msg) {
 247         if (DEBUG) {
 248             System.out.println(msg);
 249         }
 250     }
 251 }


   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 /*
  25  * @test
  26  * @bug     8002070
  27  * @summary Remove the stack search for a resource bundle Logger to use
  28  * @author  Jim Gish
  29  * @build  ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp1 LoadItUp2 TwiceIndirectlyLoadABundle LoadItUp2Invoker
  30  * @run main/othervm ResourceBundleSearchTest
  31  */
  32 import java.net.URL;
  33 import java.net.URLClassLoader;
  34 import java.nio.file.Paths;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Locale;
  38 import java.util.MissingResourceException;
  39 import java.util.ResourceBundle;
  40 import java.util.logging.Logger;
  41 
  42 /**
  43  * This class tests various scenarios of loading resource bundles from
  44  * java.util.logging.  Since jtreg uses the logging system, it is necessary to
  45  * run these tests using othervm mode to ensure no interference from logging
  46  * initialization by jtreg
  47  */
  48 public class ResourceBundleSearchTest {
  49 
  50     private final static boolean DEBUG = false;
  51     private final static String LOGGER_PREFIX = "myLogger.";
  52     private static int loggerNum = 0;
  53     private final static String PROP_RB_NAME = "ClassPathTestBundle";
  54     private final static String TCCL_TEST_BUNDLE = "ContextClassLoaderTestBundle";
  55 
  56     private static int numPass = 0;
  57     private static int numFail = 0;
  58     private static List<String> msgs = new ArrayList<>();
  59 
  60     public static void main(String[] args) throws Throwable {
  61         ResourceBundleSearchTest test = new ResourceBundleSearchTest();
  62         test.runTests();
  63     }
  64 
  65     private void runTests() throws Throwable {
  66         // ensure we are using en as the default Locale so we can find the resource
  67         Locale.setDefault(Locale.ENGLISH);
  68 



  69         ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
  70 
  71         // Find out where we are running from so we can setup the URLClassLoader URL
  72         String userDir = System.getProperty("user.dir");
  73         String testDir = System.getProperty("test.src", userDir);

  74 
  75         URL[] urls = new URL[1];
  76 
  77         urls[0] = Paths.get(testDir, "resources").toUri().toURL();
  78         URLClassLoader rbClassLoader = new URLClassLoader(urls);
  79 
  80         // Test 1 - can we find a Logger bundle from doing a stack search?
  81         // We shouldn't be able to
  82         assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch");
  83 
  84         // Test 2 - can we find a Logger bundle off of the Thread context class
  85         // loader? We should be able to.
  86         assertTrue(
  87                 testGetBundleFromTCCL(TCCL_TEST_BUNDLE, rbClassLoader),
  88                 "2-testGetBundleFromTCCL");
  89 
  90         // Test 3 - Can we find a Logger bundle from the classpath?  We should be
  91         // able to.  We'll first check to make sure the setup is correct and
  92         // it actually is on the classpath before checking whether logging
  93         // can see it there.


  94         if (isOnClassPath(PROP_RB_NAME, myClassLoader)) {
  95             debug("We should be able to see " + PROP_RB_NAME + " on the classpath");
  96             assertTrue(testGetBundleFromSystemClassLoader(PROP_RB_NAME),
  97                     "3-testGetBundleFromSystemClassLoader");
  98         } else {
  99             throw new Exception("TEST SETUP FAILURE: Cannot see " + PROP_RB_NAME
 100                     + " on the classpath");

 101         }
 102 
 103         // Test 4 - we should be able to find a bundle from the caller's
 104         // classloader, but only one level up.
 105         assertTrue(testGetBundlerFromCallersClassLoader(),
 106                 "4-testGetBundleFromCallersClassLoader");
 107 
 108         report();
 109     }
 110 
 111     private void report() throws Exception {
 112         System.out.println("Num passed = " + numPass + " Num failed = " + numFail);
 113         if (numFail > 0) {
 114             // We only care about the messages if they were errors
 115             for (String msg : msgs) {
 116                 System.out.println(msg);
 117             }
 118             throw new Exception(numFail + " out of " + (numPass + numFail)
 119                     + " tests failed.");
 120         }
 121     }
 122 
 123     public void assertTrue(boolean testResult, String testName) {
 124         if (testResult) {
 125             numPass++;
 126         } else {
 127             numFail++;
 128             System.out.println("FAILED: " + testName
 129                     + " was supposed to return true but did NOT!");
 130         }
 131     }
 132 
 133     public void assertFalse(boolean testResult, String testName) {
 134         if (!testResult) {
 135             numPass++;
 136         } else {
 137             numFail++;
 138             System.out.println("FAILED: " + testName
 139                     + " was supposed to return false but did NOT!");
 140         }
 141     }
 142 
 143     public boolean testGetBundleFromStackSearch() throws Throwable {
 144         // This should fail.  This was the old functionality to search up the
 145         // caller's call stack
 146         TwiceIndirectlyLoadABundle indirectLoader = new TwiceIndirectlyLoadABundle();
 147         return indirectLoader.loadAndTest();
 148     }
 149 
 150     public boolean testGetBundlerFromCallersClassLoader() throws Throwable {
 151         // This should pass.  This exercises getting the bundle using the
 152         // class loader of the caller (one level up)
 153         IndirectlyLoadABundle indirectLoader = new IndirectlyLoadABundle();
 154         return indirectLoader.loadAndTest();
 155     }
 156 
 157     public boolean testGetBundleFromTCCL(String bundleName,
 158             ClassLoader setOnTCCL) throws InterruptedException {
 159         // This should succeed.  We should be able to get the bundle from the
 160         // thread context class loader
 161         debug("Looking for " + bundleName + " using TCCL");
 162         LoggingThread lr = new LoggingThread(bundleName, setOnTCCL);
 163         lr.start();
 164         synchronized (lr) {
 165             try {
 166                 lr.wait();
 167             } catch (InterruptedException ex) {
 168                 throw ex;
 169             }
 170         }
 171         msgs.add(lr.msg);
 172         return lr.foundBundle;


 222         public LoggingThread(String bundleName) {
 223             this.bundleName = bundleName;
 224         }
 225 
 226         public LoggingThread(String bundleName, ClassLoader setOnTCCL) {
 227             this.clToSetOnTCCL = setOnTCCL;
 228             this.bundleName = bundleName;
 229         }
 230 
 231         public void run() {
 232             boolean setTCCL = false;
 233             try {
 234                 if (clToSetOnTCCL != null) {
 235                     Thread.currentThread().setContextClassLoader(clToSetOnTCCL);
 236                     setTCCL = true;
 237                 }
 238                 // this should succeed if the bundle is on the system classpath.
 239                 try {
 240                     Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
 241                             bundleName);
 242                     msg = "INFO: LoggingThread.run() found the bundle " + bundleName
 243                             + (setTCCL ? " with " : " without ") + "setting the TCCL";
 244                     foundBundle = true;
 245                 } catch (MissingResourceException re) {
 246                     msg = "INFO: LoggingThread.run() did not find the bundle " + bundleName
 247                             + (setTCCL ? " with " : " without ") + "setting the TCCL";
 248                     foundBundle = false;
 249                 }
 250             } catch (Throwable e) {
 251                 e.printStackTrace();
 252                 System.exit(1);
 253             }
 254         }
 255     }
 256 
 257     private void debug(String msg) {
 258         if (DEBUG) {
 259             System.out.println(msg);
 260         }
 261     }
 262 }