< prev index next >

test/java/lang/System/LoggerFinder/internal/PlatformLoggerBridgeTest/PlatformLoggerBridgeTest.java

Print this page




  32 import java.util.Arrays;
  33 import java.util.Collections;
  34 import java.util.Enumeration;
  35 import java.util.HashMap;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import java.util.Queue;
  39 import java.util.ResourceBundle;
  40 import java.util.concurrent.ArrayBlockingQueue;
  41 import java.util.concurrent.ConcurrentHashMap;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import java.util.concurrent.atomic.AtomicLong;
  44 import java.util.function.Supplier;
  45 import java.util.logging.Handler;
  46 import java.util.logging.LogRecord;
  47 import java.lang.System.LoggerFinder;
  48 import java.lang.System.Logger;
  49 import java.lang.System.Logger.Level;
  50 import java.util.stream.Stream;
  51 import sun.util.logging.PlatformLogger;

  52 
  53 /**
  54  * @test
  55  * @bug     8140364
  56  * @summary JDK implementation specific unit test for JDK internal artifacts.
  57  *          Tests all bridge methods from PlatformLogger with the a custom
  58  *          backend whose loggers implement PlatformLogger.Bridge.
  59  * @modules java.base/sun.util.logging
  60  *          java.logging
  61  * @build CustomSystemClassLoader PlatformLoggerBridgeTest
  62  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest NOSECURITY
  63  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest NOPERMISSIONS
  64  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest WITHPERMISSIONS
  65  * @author danielfuchs
  66  */
  67 public class PlatformLoggerBridgeTest {
  68 
  69     static final RuntimePermission LOGGERFINDER_PERMISSION =
  70                 new RuntimePermission("loggerFinder");
  71     final static AtomicLong sequencer = new AtomicLong();


  77         }
  78     };
  79     static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
  80         @Override
  81         protected AtomicBoolean initialValue() {
  82             return  new AtomicBoolean(false);
  83         }
  84     };
  85     static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
  86         @Override
  87         protected AtomicBoolean initialValue() {
  88             return  new AtomicBoolean(false);
  89         }
  90     };
  91 
  92     static final Class<?> providerClass;
  93     static {
  94         try {
  95             // Preload classes before the security manager is on.
  96             providerClass = ClassLoader.getSystemClassLoader().loadClass("PlatformLoggerBridgeTest$LogProducerFinder");
  97             ((LoggerFinder)providerClass.newInstance()).getLogger("foo", providerClass);
  98         } catch (Exception ex) {
  99             throw new ExceptionInInitializerError(ex);
 100         }
 101     }
 102 
 103     public static final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);
 104 
 105     public static final class LogEvent implements Cloneable {
 106 
 107         public LogEvent() {
 108             this(sequencer.getAndIncrement());
 109         }
 110 
 111         LogEvent(long sequenceNumber) {
 112             this.sequenceNumber = sequenceNumber;
 113         }
 114 
 115         long sequenceNumber;
 116         boolean isLoggable;
 117         String loggerName;


 398                     String msg, Throwable thrown) {
 399                 log(LogEvent.of(isLoggable(level), name, null, null,
 400                         level, bundle, msg, thrown, (Object[])null));
 401             }
 402 
 403             @Override
 404             public boolean isLoggable(sun.util.logging.PlatformLogger.Level level) {
 405                 return this.level != OFF && level.intValue()
 406                         >= this.level.intValue();
 407             }
 408 
 409             @Override
 410             public boolean isEnabled() {
 411                 return this.level != OFF;
 412             }
 413 
 414 
 415         }
 416 
 417         @Override
 418         public Logger getLogger(String name, Class<?> caller) {
 419             SecurityManager sm = System.getSecurityManager();
 420             if (sm != null) {
 421                 sm.checkPermission(LOGGERFINDER_PERMISSION);
 422             }
 423             PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
 424             ClassLoader callerLoader = AccessController.doPrivileged(pa);
 425             if (callerLoader == null) {
 426                 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
 427             } else {
 428                 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
 429             }
 430         }
 431     }
 432 
 433     static final sun.util.logging.PlatformLogger.Level[] julLevels = {
 434         sun.util.logging.PlatformLogger.Level.ALL,
 435         sun.util.logging.PlatformLogger.Level.FINEST,
 436         sun.util.logging.PlatformLogger.Level.FINER,
 437         sun.util.logging.PlatformLogger.Level.FINE,
 438         sun.util.logging.PlatformLogger.Level.CONFIG,


 581             }
 582             if (!acx.getPermission().equals(SimplePolicy.ACCESS_LOGGING)) {
 583                 throw new RuntimeException("Unexpected permission in exception: " + acx, acx);
 584             }
 585             final boolean old = allowAccess.get().get();
 586             allowAccess.get().set(true);
 587             try {
 588                 sysLogger1 = PlatformLogger.getLogger("foo");
 589                 loggerDescMap.put(sysLogger1, "PlatformLogger.getLogger(\"foo\")");
 590             } finally {
 591                 allowAccess.get().set(old);
 592             }
 593             System.out.println("Got expected exception for system logger: " + acx);
 594         }
 595 
 596         final LogProducerFinder.LoggerImpl sysSink;
 597         boolean old = allowControl.get().get();
 598         allowControl.get().set(true);
 599         try {
 600            sysSink = LogProducerFinder.LoggerImpl.class.cast(
 601                         provider.getLogger("foo", Thread.class));
 602         } finally {
 603             allowControl.get().set(old);
 604         }
 605 
 606         testLogger(provider, loggerDescMap, "foo", null, sysLogger1, sysSink);
 607     }
 608 
 609     public static class Foo {
 610 
 611     }
 612 
 613     static void verbose(String msg) {
 614        if (VERBOSE) {
 615            System.out.println(msg);
 616        }
 617     }
 618 
 619     static void checkLogEvent(LoggerFinder provider, String desc,
 620             LogEvent expected) {
 621         LogEvent actual =  eventQueue.poll();




  32 import java.util.Arrays;
  33 import java.util.Collections;
  34 import java.util.Enumeration;
  35 import java.util.HashMap;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import java.util.Queue;
  39 import java.util.ResourceBundle;
  40 import java.util.concurrent.ArrayBlockingQueue;
  41 import java.util.concurrent.ConcurrentHashMap;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import java.util.concurrent.atomic.AtomicLong;
  44 import java.util.function.Supplier;
  45 import java.util.logging.Handler;
  46 import java.util.logging.LogRecord;
  47 import java.lang.System.LoggerFinder;
  48 import java.lang.System.Logger;
  49 import java.lang.System.Logger.Level;
  50 import java.util.stream.Stream;
  51 import sun.util.logging.PlatformLogger;
  52 import java.lang.reflect.Module;
  53 
  54 /**
  55  * @test
  56  * @bug     8140364
  57  * @summary JDK implementation specific unit test for JDK internal artifacts.
  58  *          Tests all bridge methods from PlatformLogger with the a custom
  59  *          backend whose loggers implement PlatformLogger.Bridge.
  60  * @modules java.base/sun.util.logging
  61  *          java.logging
  62  * @build CustomSystemClassLoader PlatformLoggerBridgeTest
  63  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest NOSECURITY
  64  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest NOPERMISSIONS
  65  * @run  main/othervm -Djava.system.class.loader=CustomSystemClassLoader PlatformLoggerBridgeTest WITHPERMISSIONS
  66  * @author danielfuchs
  67  */
  68 public class PlatformLoggerBridgeTest {
  69 
  70     static final RuntimePermission LOGGERFINDER_PERMISSION =
  71                 new RuntimePermission("loggerFinder");
  72     final static AtomicLong sequencer = new AtomicLong();


  78         }
  79     };
  80     static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
  81         @Override
  82         protected AtomicBoolean initialValue() {
  83             return  new AtomicBoolean(false);
  84         }
  85     };
  86     static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
  87         @Override
  88         protected AtomicBoolean initialValue() {
  89             return  new AtomicBoolean(false);
  90         }
  91     };
  92 
  93     static final Class<?> providerClass;
  94     static {
  95         try {
  96             // Preload classes before the security manager is on.
  97             providerClass = ClassLoader.getSystemClassLoader().loadClass("PlatformLoggerBridgeTest$LogProducerFinder");
  98             ((LoggerFinder)providerClass.newInstance()).getLogger("foo", providerClass.getModule());
  99         } catch (Exception ex) {
 100             throw new ExceptionInInitializerError(ex);
 101         }
 102     }
 103 
 104     public static final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);
 105 
 106     public static final class LogEvent implements Cloneable {
 107 
 108         public LogEvent() {
 109             this(sequencer.getAndIncrement());
 110         }
 111 
 112         LogEvent(long sequenceNumber) {
 113             this.sequenceNumber = sequenceNumber;
 114         }
 115 
 116         long sequenceNumber;
 117         boolean isLoggable;
 118         String loggerName;


 399                     String msg, Throwable thrown) {
 400                 log(LogEvent.of(isLoggable(level), name, null, null,
 401                         level, bundle, msg, thrown, (Object[])null));
 402             }
 403 
 404             @Override
 405             public boolean isLoggable(sun.util.logging.PlatformLogger.Level level) {
 406                 return this.level != OFF && level.intValue()
 407                         >= this.level.intValue();
 408             }
 409 
 410             @Override
 411             public boolean isEnabled() {
 412                 return this.level != OFF;
 413             }
 414 
 415 
 416         }
 417 
 418         @Override
 419         public Logger getLogger(String name, Module caller) {
 420             SecurityManager sm = System.getSecurityManager();
 421             if (sm != null) {
 422                 sm.checkPermission(LOGGERFINDER_PERMISSION);
 423             }
 424             PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
 425             ClassLoader callerLoader = AccessController.doPrivileged(pa);
 426             if (callerLoader == null) {
 427                 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
 428             } else {
 429                 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
 430             }
 431         }
 432     }
 433 
 434     static final sun.util.logging.PlatformLogger.Level[] julLevels = {
 435         sun.util.logging.PlatformLogger.Level.ALL,
 436         sun.util.logging.PlatformLogger.Level.FINEST,
 437         sun.util.logging.PlatformLogger.Level.FINER,
 438         sun.util.logging.PlatformLogger.Level.FINE,
 439         sun.util.logging.PlatformLogger.Level.CONFIG,


 582             }
 583             if (!acx.getPermission().equals(SimplePolicy.ACCESS_LOGGING)) {
 584                 throw new RuntimeException("Unexpected permission in exception: " + acx, acx);
 585             }
 586             final boolean old = allowAccess.get().get();
 587             allowAccess.get().set(true);
 588             try {
 589                 sysLogger1 = PlatformLogger.getLogger("foo");
 590                 loggerDescMap.put(sysLogger1, "PlatformLogger.getLogger(\"foo\")");
 591             } finally {
 592                 allowAccess.get().set(old);
 593             }
 594             System.out.println("Got expected exception for system logger: " + acx);
 595         }
 596 
 597         final LogProducerFinder.LoggerImpl sysSink;
 598         boolean old = allowControl.get().get();
 599         allowControl.get().set(true);
 600         try {
 601            sysSink = LogProducerFinder.LoggerImpl.class.cast(
 602                         provider.getLogger("foo", Thread.class.getModule()));
 603         } finally {
 604             allowControl.get().set(old);
 605         }
 606 
 607         testLogger(provider, loggerDescMap, "foo", null, sysLogger1, sysSink);
 608     }
 609 
 610     public static class Foo {
 611 
 612     }
 613 
 614     static void verbose(String msg) {
 615        if (VERBOSE) {
 616            System.out.println(msg);
 617        }
 618     }
 619 
 620     static void checkLogEvent(LoggerFinder provider, String desc,
 621             LogEvent expected) {
 622         LogEvent actual =  eventQueue.poll();


< prev index next >