< prev index next >

test/jdk/java/lang/System/LoggerFinder/internal/LoggerFinderLoaderTest/LoggerFinderLoaderTest.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2017, 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  */


  46 import java.lang.System.Logger.Level;
  47 import java.security.AccessController;
  48 import java.security.PrivilegedAction;
  49 import java.util.EnumSet;
  50 import java.util.Iterator;
  51 import java.util.Locale;
  52 import java.util.ServiceConfigurationError;
  53 import java.util.ServiceLoader;
  54 import java.util.concurrent.atomic.AtomicReference;
  55 import jdk.internal.logger.SimpleConsoleLogger;
  56 
  57 /**
  58  * @test
  59  * @bug     8140364 8189291
  60  * @summary JDK implementation specific unit test for LoggerFinderLoader.
  61  *          Tests the behavior of LoggerFinderLoader with respect to the
  62  *          value of the internal diagnosability switches. Also test the
  63  *          DefaultLoggerFinder and SimpleConsoleLogger implementation.
  64  * @modules java.base/sun.util.logging
  65  *          java.base/jdk.internal.logger
  66  * @build AccessSystemLogger LoggerFinderLoaderTest CustomSystemClassLoader
  67  * @run  driver AccessSystemLogger
  68  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOSECURITY
  69  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOPERMISSIONS
  70  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest WITHPERMISSIONS
  71  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOSECURITY
  72  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOPERMISSIONS
  73  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest WITHPERMISSIONS
  74  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOSECURITY
  75  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOPERMISSIONS
  76  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest WITHPERMISSIONS
  77  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest NOSECURITY
  78  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest NOPERMISSIONS
  79  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest WITHPERMISSIONS
  80  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest NOSECURITY
  81  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest NOPERMISSIONS
  82  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest WITHPERMISSIONS
  83  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest NOSECURITY
  84  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest NOPERMISSIONS
  85  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest WITHPERMISSIONS
  86  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOSECURITY


 101                 new RuntimePermission("loggerFinder");
 102     final static boolean VERBOSE = false;
 103     static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
 104         @Override
 105         protected AtomicBoolean initialValue() {
 106             return  new AtomicBoolean(false);
 107         }
 108     };
 109     static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
 110         @Override
 111         protected AtomicBoolean initialValue() {
 112             return  new AtomicBoolean(false);
 113         }
 114     };
 115 
 116     final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger();
 117     static final Class<?>[] providerClass;
 118     static {
 119         try {
 120             providerClass = new Class<?>[] {
 121                 ClassLoader.getSystemClassLoader().loadClass("LoggerFinderLoaderTest$BaseLoggerFinder"),
 122                 ClassLoader.getSystemClassLoader().loadClass("LoggerFinderLoaderTest$BaseLoggerFinder2")
 123             };
 124         } catch (ClassNotFoundException ex) {
 125             throw new ExceptionInInitializerError(ex);
 126         }
 127     }
 128 
 129     /**
 130      * What our test provider needs to implement.
 131      */
 132     public static interface TestLoggerFinder {
 133         public final static AtomicBoolean fails = new AtomicBoolean();
 134         public final static AtomicReference<String> conf = new AtomicReference<>("");
 135         public final static AtomicLong sequencer = new AtomicLong();
 136         public final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>();
 137         public final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>();
 138 
 139         public class LoggerImpl implements System.Logger {
 140             final String name;
 141             final Logger logger;
 142 


 154             public boolean isLoggable(Logger.Level level) {
 155                 return logger.isLoggable(level);
 156             }
 157 
 158             @Override
 159             public void log(Logger.Level level, ResourceBundle bundle, String key, Throwable thrown) {
 160                 logger.log(level, bundle, key, thrown);
 161             }
 162 
 163             @Override
 164             public void log(Logger.Level level, ResourceBundle bundle, String format, Object... params) {
 165                 logger.log(level, bundle, format, params);
 166             }
 167 
 168         }
 169 
 170         public Logger getLogger(String name, Module caller);
 171         public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
 172     }
 173 
 174     public static class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder {
 175 
 176         static final RuntimePermission LOGGERFINDER_PERMISSION =
 177                     new RuntimePermission("loggerFinder");
 178         public BaseLoggerFinder() {
 179             if (fails.get()) {
 180                 throw new RuntimeException("Simulate exception while loading provider");
 181             }
 182         }
 183 
 184         System.Logger createSimpleLogger(String name) {
 185             PrivilegedAction<System.Logger> pa = () -> SimpleConsoleLogger.makeSimpleLogger(name);
 186             return AccessController.doPrivileged(pa);
 187         }
 188 
 189 
 190         @Override
 191         public Logger getLogger(String name, Module caller) {
 192             SecurityManager sm = System.getSecurityManager();
 193             if (sm != null) {
 194                 sm.checkPermission(LOGGERFINDER_PERMISSION);
 195             }
 196             PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
 197             ClassLoader callerLoader = AccessController.doPrivileged(pa);
 198             if (callerLoader == null) {
 199                 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n, createSimpleLogger(name)));
 200             } else {
 201                 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n, createSimpleLogger(name)));
 202             }
 203         }
 204     }
 205 
 206     public static class BaseLoggerFinder2 extends LoggerFinder implements TestLoggerFinder {
 207 
 208         static final RuntimePermission LOGGERFINDER_PERMISSION =
 209                     new RuntimePermission("loggerFinder");
 210         public BaseLoggerFinder2() {
 211             throw new ServiceConfigurationError("Should not come here");
 212         }
 213         @Override
 214         public Logger getLogger(String name, Module caller) {
 215             throw new ServiceConfigurationError("Should not come here");
 216         }
 217     }
 218 
 219     public static class MyBundle extends ResourceBundle {
 220 
 221         final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
 222 
 223         @Override
 224         protected Object handleGetObject(String key) {
 225             if (key.contains(" (translated)")) {
 226                 throw new RuntimeException("Unexpected key: " + key);
 227             }
 228             return map.computeIfAbsent(key, k -> k.toUpperCase(Locale.ROOT) + " (translated)");
 229         }
 230 
 231         @Override
 232         public Enumeration<String> getKeys() {
 233             return Collections.enumeration(map.keySet());
 234         }
 235 
 236     }
 237     public static class MyLoggerBundle extends MyBundle {
 238 


 253         try {
 254             TestLoggerFinder.sequencer.incrementAndGet();
 255             provider = LoggerFinder.getLoggerFinder();
 256             if (TestLoggerFinder.fails.get() || singleton) {
 257                 if ("ERROR".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 258                     throw new RuntimeException("Expected exception not thrown");
 259                 } else if ("WARNING".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 260                     String warning = ErrorStream.errorStream.peek();
 261                     if (!warning.contains("WARNING: Failed to instantiate LoggerFinder provider; Using default.")) {
 262                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 263                     }
 264                 } else if ("DEBUG".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 265                     String warning = ErrorStream.errorStream.peek();
 266                     if (!warning.contains("WARNING: Failed to instantiate LoggerFinder provider; Using default.")) {
 267                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 268                     }
 269                     if (!warning.contains("WARNING: Exception raised trying to instantiate LoggerFinder")) {
 270                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 271                     }
 272                     if (TestLoggerFinder.fails.get()) {
 273                         if (!warning.contains("java.util.ServiceConfigurationError: java.lang.System$LoggerFinder: Provider LoggerFinderLoaderTest$BaseLoggerFinder could not be instantiated")) {
 274                             throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 275                         }
 276                     } else if (singleton) {
 277                         if (!warning.contains("java.util.ServiceConfigurationError: More than on LoggerFinder implementation")) {
 278                             throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 279                         }
 280                     }
 281                 } else if ("QUIET".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 282                     if (!ErrorStream.errorStream.peek().isEmpty()) {
 283                         throw new RuntimeException("Unexpected error message found: "
 284                                 + ErrorStream.errorStream.peek());
 285                     }
 286                 }
 287             }
 288         } catch(AccessControlException a) {
 289             throw a;
 290         } catch(Throwable t) {
 291             if (TestLoggerFinder.fails.get() || singleton) {
 292                 // must check System.err
 293                 if ("ERROR".equals(errorPolicy.toUpperCase(Locale.ROOT))) {


 408 
 409 
 410     public static void test(String[] args) {
 411 
 412         final String errorPolicy =  System.getProperty("jdk.logger.finder.error", "WARNING");
 413         final Boolean ensureSingleton = Boolean.getBoolean("jdk.logger.finder.singleton");
 414 
 415         final Class<?> expectedClass =
 416                 TestLoggerFinder.fails.get() || ensureSingleton
 417                 ? jdk.internal.logger.DefaultLoggerFinder.class
 418                 : TestLoggerFinder.class;
 419 
 420         System.out.println("Declared provider class: " + providerClass[0]
 421                 + "[" + providerClass[0].getClassLoader() + "]");
 422 
 423         if (!TestLoggerFinder.fails.get()) {
 424             ServiceLoader<LoggerFinder> serviceLoader =
 425                 ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader());
 426             Iterator<LoggerFinder> iterator = serviceLoader.iterator();
 427             Object firstProvider = iterator.next();
 428             if (!firstProvider.getClass().getName().equals("LoggerFinderLoaderTest$BaseLoggerFinder")) {
 429                 throw new RuntimeException("Unexpected provider: " + firstProvider.getClass().getName());
 430             }
 431             if (!iterator.hasNext()) {
 432                 throw new RuntimeException("Expected two providers");
 433             }
 434         }
 435 
 436         Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
 437             LoggerFinder provider;
 438             ErrorStream.errorStream.restore();
 439             switch (testCase) {
 440                 case NOSECURITY:
 441                     System.out.println("\n*** Without Security Manager\n");
 442                     System.out.println(TestLoggerFinder.conf.get());
 443                     provider = getLoggerFinder(expectedClass, errorPolicy, ensureSingleton);
 444                     test(provider, true);
 445                     System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
 446                     break;
 447                 case NOPERMISSIONS:
 448                     System.out.println("\n*** With Security Manager, without permissions\n");


   1 /*
   2  * Copyright (c) 2015, 2018, 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  */


  46 import java.lang.System.Logger.Level;
  47 import java.security.AccessController;
  48 import java.security.PrivilegedAction;
  49 import java.util.EnumSet;
  50 import java.util.Iterator;
  51 import java.util.Locale;
  52 import java.util.ServiceConfigurationError;
  53 import java.util.ServiceLoader;
  54 import java.util.concurrent.atomic.AtomicReference;
  55 import jdk.internal.logger.SimpleConsoleLogger;
  56 
  57 /**
  58  * @test
  59  * @bug     8140364 8189291
  60  * @summary JDK implementation specific unit test for LoggerFinderLoader.
  61  *          Tests the behavior of LoggerFinderLoader with respect to the
  62  *          value of the internal diagnosability switches. Also test the
  63  *          DefaultLoggerFinder and SimpleConsoleLogger implementation.
  64  * @modules java.base/sun.util.logging
  65  *          java.base/jdk.internal.logger
  66  * @build AccessSystemLogger LoggerFinderLoaderTest CustomSystemClassLoader BaseLoggerFinder BaseLoggerFinder2
  67  * @run  driver AccessSystemLogger
  68  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOSECURITY
  69  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOPERMISSIONS
  70  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest WITHPERMISSIONS
  71  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOSECURITY
  72  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOPERMISSIONS
  73  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest WITHPERMISSIONS
  74  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOSECURITY
  75  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOPERMISSIONS
  76  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest WITHPERMISSIONS
  77  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest NOSECURITY
  78  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest NOPERMISSIONS
  79  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=DEBUG LoggerFinderLoaderTest WITHPERMISSIONS
  80  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest NOSECURITY
  81  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest NOPERMISSIONS
  82  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true -Djdk.logger.finder.error=QUIET LoggerFinderLoaderTest WITHPERMISSIONS
  83  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest NOSECURITY
  84  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest NOPERMISSIONS
  85  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true LoggerFinderLoaderTest WITHPERMISSIONS
  86  * @run  main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Djdk.logger.finder.singleton=true -Djdk.logger.finder.error=ERROR LoggerFinderLoaderTest NOSECURITY


 101                 new RuntimePermission("loggerFinder");
 102     final static boolean VERBOSE = false;
 103     static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
 104         @Override
 105         protected AtomicBoolean initialValue() {
 106             return  new AtomicBoolean(false);
 107         }
 108     };
 109     static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
 110         @Override
 111         protected AtomicBoolean initialValue() {
 112             return  new AtomicBoolean(false);
 113         }
 114     };
 115 
 116     final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger();
 117     static final Class<?>[] providerClass;
 118     static {
 119         try {
 120             providerClass = new Class<?>[] {
 121                 ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder"),
 122                 ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder2")
 123             };
 124         } catch (ClassNotFoundException ex) {
 125             throw new ExceptionInInitializerError(ex);
 126         }
 127     }
 128 
 129     /**
 130      * What our test provider needs to implement.
 131      */
 132     public static interface TestLoggerFinder {
 133         public final static AtomicBoolean fails = new AtomicBoolean();
 134         public final static AtomicReference<String> conf = new AtomicReference<>("");
 135         public final static AtomicLong sequencer = new AtomicLong();
 136         public final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>();
 137         public final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>();
 138 
 139         public class LoggerImpl implements System.Logger {
 140             final String name;
 141             final Logger logger;
 142 


 154             public boolean isLoggable(Logger.Level level) {
 155                 return logger.isLoggable(level);
 156             }
 157 
 158             @Override
 159             public void log(Logger.Level level, ResourceBundle bundle, String key, Throwable thrown) {
 160                 logger.log(level, bundle, key, thrown);
 161             }
 162 
 163             @Override
 164             public void log(Logger.Level level, ResourceBundle bundle, String format, Object... params) {
 165                 logger.log(level, bundle, format, params);
 166             }
 167 
 168         }
 169 
 170         public Logger getLogger(String name, Module caller);
 171         public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
 172     }
 173 













































 174     public static class MyBundle extends ResourceBundle {
 175 
 176         final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
 177 
 178         @Override
 179         protected Object handleGetObject(String key) {
 180             if (key.contains(" (translated)")) {
 181                 throw new RuntimeException("Unexpected key: " + key);
 182             }
 183             return map.computeIfAbsent(key, k -> k.toUpperCase(Locale.ROOT) + " (translated)");
 184         }
 185 
 186         @Override
 187         public Enumeration<String> getKeys() {
 188             return Collections.enumeration(map.keySet());
 189         }
 190 
 191     }
 192     public static class MyLoggerBundle extends MyBundle {
 193 


 208         try {
 209             TestLoggerFinder.sequencer.incrementAndGet();
 210             provider = LoggerFinder.getLoggerFinder();
 211             if (TestLoggerFinder.fails.get() || singleton) {
 212                 if ("ERROR".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 213                     throw new RuntimeException("Expected exception not thrown");
 214                 } else if ("WARNING".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 215                     String warning = ErrorStream.errorStream.peek();
 216                     if (!warning.contains("WARNING: Failed to instantiate LoggerFinder provider; Using default.")) {
 217                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 218                     }
 219                 } else if ("DEBUG".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 220                     String warning = ErrorStream.errorStream.peek();
 221                     if (!warning.contains("WARNING: Failed to instantiate LoggerFinder provider; Using default.")) {
 222                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 223                     }
 224                     if (!warning.contains("WARNING: Exception raised trying to instantiate LoggerFinder")) {
 225                         throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 226                     }
 227                     if (TestLoggerFinder.fails.get()) {
 228                         if (!warning.contains("java.util.ServiceConfigurationError: java.lang.System$LoggerFinder: Provider BaseLoggerFinder could not be instantiated")) {
 229                             throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 230                         }
 231                     } else if (singleton) {
 232                         if (!warning.contains("java.util.ServiceConfigurationError: More than on LoggerFinder implementation")) {
 233                             throw new RuntimeException("Expected message not found. Error stream contained: " + warning);
 234                         }
 235                     }
 236                 } else if ("QUIET".equals(errorPolicy.toUpperCase(Locale.ROOT))) {
 237                     if (!ErrorStream.errorStream.peek().isEmpty()) {
 238                         throw new RuntimeException("Unexpected error message found: "
 239                                 + ErrorStream.errorStream.peek());
 240                     }
 241                 }
 242             }
 243         } catch(AccessControlException a) {
 244             throw a;
 245         } catch(Throwable t) {
 246             if (TestLoggerFinder.fails.get() || singleton) {
 247                 // must check System.err
 248                 if ("ERROR".equals(errorPolicy.toUpperCase(Locale.ROOT))) {


 363 
 364 
 365     public static void test(String[] args) {
 366 
 367         final String errorPolicy =  System.getProperty("jdk.logger.finder.error", "WARNING");
 368         final Boolean ensureSingleton = Boolean.getBoolean("jdk.logger.finder.singleton");
 369 
 370         final Class<?> expectedClass =
 371                 TestLoggerFinder.fails.get() || ensureSingleton
 372                 ? jdk.internal.logger.DefaultLoggerFinder.class
 373                 : TestLoggerFinder.class;
 374 
 375         System.out.println("Declared provider class: " + providerClass[0]
 376                 + "[" + providerClass[0].getClassLoader() + "]");
 377 
 378         if (!TestLoggerFinder.fails.get()) {
 379             ServiceLoader<LoggerFinder> serviceLoader =
 380                 ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader());
 381             Iterator<LoggerFinder> iterator = serviceLoader.iterator();
 382             Object firstProvider = iterator.next();
 383             if (!firstProvider.getClass().getName().equals("BaseLoggerFinder")) {
 384                 throw new RuntimeException("Unexpected provider: " + firstProvider.getClass().getName());
 385             }
 386             if (!iterator.hasNext()) {
 387                 throw new RuntimeException("Expected two providers");
 388             }
 389         }
 390 
 391         Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
 392             LoggerFinder provider;
 393             ErrorStream.errorStream.restore();
 394             switch (testCase) {
 395                 case NOSECURITY:
 396                     System.out.println("\n*** Without Security Manager\n");
 397                     System.out.println(TestLoggerFinder.conf.get());
 398                     provider = getLoggerFinder(expectedClass, errorPolicy, ensureSingleton);
 399                     test(provider, true);
 400                     System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
 401                     break;
 402                 case NOPERMISSIONS:
 403                     System.out.println("\n*** With Security Manager, without permissions\n");


< prev index next >