test/sun/util/logging/PlatformLoggerTest.java

Print this page




  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     6882376 6985460 8010309
  27  * @summary Test if java.util.logging.Logger is created before and after
  28  *          logging is enabled.  Also validate some basic PlatformLogger
  29  *          operations.  othervm mode to make sure java.util.logging
  30  *          is not initialized.
  31  *
  32  * @compile -XDignore.symbol.file PlatformLoggerTest.java
  33  * @run main/othervm PlatformLoggerTest
  34  */
  35 
  36 import java.lang.reflect.Field;
  37 import java.util.logging.*;
  38 import sun.util.logging.PlatformLogger;

  39 
  40 public class PlatformLoggerTest {
  41     public static void main(String[] args) throws Exception {
  42         final String FOO_PLATFORM_LOGGER = "test.platformlogger.foo";
  43         final String BAR_PLATFORM_LOGGER = "test.platformlogger.bar";
  44         final String GOO_PLATFORM_LOGGER = "test.platformlogger.goo";
  45         final String BAR_LOGGER = "test.logger.bar";
  46         PlatformLogger goo = PlatformLogger.getLogger(GOO_PLATFORM_LOGGER);
  47         // test the PlatformLogger methods
  48         testLogMethods(goo);
  49 
  50         // Create a platform logger using the default
  51         PlatformLogger foo = PlatformLogger.getLogger(FOO_PLATFORM_LOGGER);
  52         checkPlatformLogger(foo, FOO_PLATFORM_LOGGER);
  53 
  54         // create a java.util.logging.Logger
  55         // now java.util.logging.Logger should be created for each platform logger
  56         Logger logger = Logger.getLogger(BAR_LOGGER);
  57         logger.setLevel(Level.WARNING);
  58 
  59         PlatformLogger bar = PlatformLogger.getLogger(BAR_PLATFORM_LOGGER);
  60         checkPlatformLogger(bar, BAR_PLATFORM_LOGGER);
  61 
  62         // test the PlatformLogger methods
  63         testLogMethods(goo);
  64         testLogMethods(bar);
  65 
  66         checkLogger(FOO_PLATFORM_LOGGER, Level.FINER);
  67         checkLogger(BAR_PLATFORM_LOGGER, Level.FINER);
  68 
  69         checkLogger(GOO_PLATFORM_LOGGER, null);
  70         checkLogger(BAR_LOGGER, Level.WARNING);
  71 
  72         foo.setLevel(PlatformLogger.SEVERE);
  73         checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE);
  74 
  75         checkPlatformLoggerLevels(foo, bar);
  76     }
  77 

  78     private static void checkPlatformLogger(PlatformLogger logger, String name) {
  79         if (!logger.getName().equals(name)) {
  80             throw new RuntimeException("Invalid logger's name " +
  81                 logger.getName() + " but expected " + name);
  82         }
  83 
  84         if (logger.getLevel() != null) {
  85             throw new RuntimeException("Invalid default level for logger " +
  86                 logger.getName() + ": " + logger.getLevel());
  87         }
  88 
  89         if (logger.isLoggable(PlatformLogger.FINE) != false) {
  90             throw new RuntimeException("isLoggerable(FINE) returns true for logger " +
  91                 logger.getName() + " but expected false");






  92         }
  93 
  94         logger.setLevel(PlatformLogger.FINER);
  95         if (logger.getLevel() != PlatformLogger.FINER) {
  96             throw new RuntimeException("Invalid level for logger " +
  97                 logger.getName() + " " + logger.getLevel());
  98         }
  99 
 100         if (logger.isLoggable(PlatformLogger.FINE) != true) {
 101             throw new RuntimeException("isLoggerable(FINE) returns false for logger " +
 102                 logger.getName() + " but expected true");
 103         }
 104 
 105         logger.info("OK: Testing log message");
















 106     }
 107 
 108     private static void checkLogger(String name, Level level) {
 109         Logger logger = LogManager.getLogManager().getLogger(name);
 110         if (logger == null) {
 111             throw new RuntimeException("Logger " + name +
 112                 " does not exist");
 113         }
 114 
 115         if (logger.getLevel() != level) {
 116             throw new RuntimeException("Invalid level for logger " +
 117                 logger.getName() + " " + logger.getLevel());
 118         }
 119     }
 120 
 121     private static void testLogMethods(PlatformLogger logger) {
 122         logger.severe("Test severe(String, Object...) {0} {1}", new Long(1), "string");
 123         // test Object[]
 124         logger.severe("Test severe(String, Object...) {0}", (Object[]) getPoints());
 125         logger.warning("Test warning(String, Throwable)", new Throwable("Testing"));


 129     private static void checkPlatformLoggerLevels(PlatformLogger... loggers) {
 130         final Level[] levels = new Level[] {
 131             Level.ALL, Level.CONFIG, Level.FINE, Level.FINER, Level.FINEST,
 132             Level.INFO, Level.OFF, Level.SEVERE, Level.WARNING
 133         };
 134 
 135         int count = PlatformLogger.Level.values().length;
 136         if (levels.length != count) {
 137             throw new RuntimeException("There are " + count +
 138                     " PlatformLogger.Level members, but " + levels.length +
 139                     " standard java.util.logging levels - the numbers should be equal.");
 140         }
 141         // check mappings
 142         for (Level level : levels) {
 143             checkPlatformLoggerLevelMapping(level);
 144         }
 145 
 146         for (Level level : levels) {
 147             PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
 148             for (PlatformLogger logger : loggers) {
 149                 // verify PlatformLogger.setLevel to a given level
 150                 logger.setLevel(platformLevel);
 151                 PlatformLogger.Level retrievedPlatformLevel = logger.getLevel();
 152                 if (platformLevel != retrievedPlatformLevel) {
 153                     throw new RuntimeException("Retrieved PlatformLogger level " +
 154                             retrievedPlatformLevel +
 155                             " is not the same as set level " + platformLevel);








 156                 }
 157 
 158                 // check the level set in java.util.logging.Logger
 159                 Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName());
 160                 Level javaLevel = javaLogger.getLevel();
 161                 if (javaLogger.getLevel() != level) {
 162                     throw new RuntimeException("Retrieved backing java.util.logging.Logger level " +
 163                             javaLevel + " is not the expected " + level);
 164                 }
 165             }
 166         }
 167     }
 168 
 169     private static void checkPlatformLoggerLevelMapping(Level level) {
 170         // map the given level to PlatformLogger.Level of the same name and value
 171         PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
 172         if (platformLevel.intValue() != level.intValue()) {
 173             throw new RuntimeException("Mismatched level: " + level
 174                     + " PlatformLogger.Level" + platformLevel);
 175         }
 176 
 177         PlatformLogger.Level plevel;
 178         try {
 179             // validate if there is a public static final field in PlatformLogger
 180             // matching the level name
 181             Field platformLevelField = PlatformLogger.class.getField(level.getName());
 182             plevel = (PlatformLogger.Level) platformLevelField.get(null);



 183         } catch (Exception e) {
 184             throw new RuntimeException("No public static PlatformLogger." + level.getName() +
 185                                        " field", e);
 186         }
 187         if (!plevel.name().equals(level.getName()))
 188             throw new RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is "
 189                                        + platformLevel.name() + " but expected " + level.getName());
 190 
 191         if (plevel.intValue() != level.intValue())
 192             throw new RuntimeException("The value of PlatformLogger." + level.intValue() + ".intValue() is "
 193                                        + platformLevel.intValue() + " but expected " + level.intValue());
 194     }
 195 
 196     static Point[] getPoints() {
 197         Point[] res = new Point[3];
 198         res[0] = new Point(0,0);
 199         res[1] = new Point(1,1);
 200         res[2] = new Point(2,2);
 201         return res;
 202     }
 203 
 204     static class Point {
 205         final int x;
 206         final int y;
 207         public Point(int x, int y) {
 208             this.x = x;
 209             this.y = y;
 210         }
 211         public String toString() {


  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     6882376 6985460 8010309
  27  * @summary Test if java.util.logging.Logger is created before and after
  28  *          logging is enabled.  Also validate some basic PlatformLogger
  29  *          operations.  othervm mode to make sure java.util.logging
  30  *          is not initialized.
  31  *
  32  * @compile -XDignore.symbol.file PlatformLoggerTest.java
  33  * @run main/othervm PlatformLoggerTest
  34  */
  35 
  36 import java.lang.reflect.Field;
  37 import java.util.logging.*;
  38 import sun.util.logging.PlatformLogger;
  39 import static sun.util.logging.PlatformLogger.Level.*;
  40 
  41 public class PlatformLoggerTest {
  42     public static void main(String[] args) throws Exception {
  43         final String FOO_PLATFORM_LOGGER = "test.platformlogger.foo";
  44         final String BAR_PLATFORM_LOGGER = "test.platformlogger.bar";
  45         final String GOO_PLATFORM_LOGGER = "test.platformlogger.goo";
  46         final String BAR_LOGGER = "test.logger.bar";
  47         PlatformLogger goo = PlatformLogger.getLogger(GOO_PLATFORM_LOGGER);
  48         // test the PlatformLogger methods
  49         testLogMethods(goo);
  50 
  51         // Create a platform logger using the default
  52         PlatformLogger foo = PlatformLogger.getLogger(FOO_PLATFORM_LOGGER);
  53         checkPlatformLogger(foo, FOO_PLATFORM_LOGGER);
  54 
  55         // create a java.util.logging.Logger
  56         // now java.util.logging.Logger should be created for each platform logger
  57         Logger logger = Logger.getLogger(BAR_LOGGER);
  58         logger.setLevel(Level.WARNING);
  59 
  60         PlatformLogger bar = PlatformLogger.getLogger(BAR_PLATFORM_LOGGER);
  61         checkPlatformLogger(bar, BAR_PLATFORM_LOGGER);
  62 
  63         // test the PlatformLogger methods
  64         testLogMethods(goo);
  65         testLogMethods(bar);
  66 
  67         checkLogger(FOO_PLATFORM_LOGGER, Level.FINER);
  68         checkLogger(BAR_PLATFORM_LOGGER, Level.FINER);
  69 
  70         checkLogger(GOO_PLATFORM_LOGGER, null);
  71         checkLogger(BAR_LOGGER, Level.WARNING);
  72 
  73         foo.setLevel(PlatformLogger.Level.SEVERE);
  74         checkLogger(FOO_PLATFORM_LOGGER, Level.SEVERE);
  75 
  76         checkPlatformLoggerLevels(foo, bar);
  77     }
  78 
  79     // don't use java.util.logging here to prevent it from initialized
  80     private static void checkPlatformLogger(PlatformLogger logger, String name) {
  81         if (!logger.getName().equals(name)) {
  82             throw new RuntimeException("Invalid logger's name " +
  83                 logger.getName() + " but expected " + name);
  84         }
  85 
  86         if (logger.level() != null) {
  87             throw new RuntimeException("Invalid default level for logger " +
  88                 logger.getName() + ": " + logger.level());
  89         }
  90 
  91         checkLoggable(logger, FINE, false);
  92 
  93         logger.setLevel(FINER);
  94         checkLevel(logger, FINER);
  95         checkLoggable(logger, FINER, true);
  96         checkLoggable(logger, FINE, true);
  97         checkLoggable(logger, FINEST, false);
  98 
  99         logger.info("OK: Testing log message");
 100     }
 101 
 102     private static void checkLoggable(PlatformLogger logger, PlatformLogger.Level level, boolean expected) {
 103         if (logger.isLoggable(level) != expected) {
 104             throw new RuntimeException("logger " + logger.getName() + ": " + level +
 105                 (expected ? " not loggable" : " loggable")); 
 106         }
 107 
 108         if (logger.isLoggable(level.intValue()) != expected) {
 109             throw new RuntimeException("logger " + logger.getName() + ": " + level.intValue() +
 110                 (expected ? " not loggable" : " loggable")); 
 111         }
 112 
 113         int value = level.intValue() + 5; // custom level value
 114         if (expected && !logger.isLoggable(value)) {
 115             throw new RuntimeException("logger " + logger.getName() + ": " + value +
 116                 " not loggable");
 117         }
 118     }
 119 
 120     private static void checkLevel(PlatformLogger logger, PlatformLogger.Level level) {
 121         if (logger.level() != level) {
 122             throw new RuntimeException("Invalid level for logger " +
 123                 logger.getName() + ": " + logger.level() + " != " + level);
 124         }
 125 
 126         if (logger.getLevel() != level.intValue()) {
 127             throw new RuntimeException("Invalid level for logger " +
 128                 logger.getName() + ": " + logger.getLevel() + " != " + level.intValue());
 129         }
 130     }
 131 
 132     private static void checkLogger(String name, Level level) {
 133         Logger logger = LogManager.getLogManager().getLogger(name);
 134         if (logger == null) {
 135             throw new RuntimeException("Logger " + name +
 136                 " does not exist");
 137         }
 138 
 139         if (logger.getLevel() != level) {
 140             throw new RuntimeException("Invalid level for logger " +
 141                 logger.getName() + " " + logger.getLevel());
 142         }
 143     }
 144 
 145     private static void testLogMethods(PlatformLogger logger) {
 146         logger.severe("Test severe(String, Object...) {0} {1}", new Long(1), "string");
 147         // test Object[]
 148         logger.severe("Test severe(String, Object...) {0}", (Object[]) getPoints());
 149         logger.warning("Test warning(String, Throwable)", new Throwable("Testing"));


 153     private static void checkPlatformLoggerLevels(PlatformLogger... loggers) {
 154         final Level[] levels = new Level[] {
 155             Level.ALL, Level.CONFIG, Level.FINE, Level.FINER, Level.FINEST,
 156             Level.INFO, Level.OFF, Level.SEVERE, Level.WARNING
 157         };
 158 
 159         int count = PlatformLogger.Level.values().length;
 160         if (levels.length != count) {
 161             throw new RuntimeException("There are " + count +
 162                     " PlatformLogger.Level members, but " + levels.length +
 163                     " standard java.util.logging levels - the numbers should be equal.");
 164         }
 165         // check mappings
 166         for (Level level : levels) {
 167             checkPlatformLoggerLevelMapping(level);
 168         }
 169 
 170         for (Level level : levels) {
 171             PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
 172             for (PlatformLogger logger : loggers) {
 173                 logger.setLevel(platformLevel);       // setLevel(PlatformLogger.Level)
 174                 checkLoggerLevel(logger, level);
 175 
 176                 logger.setLevel(ALL);  // setLevel(int)
 177                 checkLoggerLevel(logger, Level.ALL);
 178             }
 179         }
 180     }
 181 
 182     private static void checkLoggerLevel(PlatformLogger logger, Level level) {
 183         PlatformLogger.Level plevel = PlatformLogger.Level.valueOf(level.getName());
 184         if (plevel != logger.level()) {
 185             throw new RuntimeException("Retrieved PlatformLogger level "
 186                     + logger.level()
 187                     + " is not the same as set level " + plevel);
 188         }
 189 
 190         // check the level set in java.util.logging.Logger
 191         Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName());
 192         Level javaLevel = javaLogger.getLevel();
 193         if (javaLogger.getLevel() != level) {
 194             throw new RuntimeException("Retrieved backing java.util.logging.Logger level "
 195                     + javaLevel + " is not the expected " + level);


 196         }
 197     }
 198 
 199     private static void checkPlatformLoggerLevelMapping(Level level) {
 200         // map the given level to PlatformLogger.Level of the same name and value
 201         PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
 202         if (platformLevel.intValue() != level.intValue()) {
 203             throw new RuntimeException("Mismatched level: " + level
 204                     + " PlatformLogger.Level" + platformLevel);
 205         }
 206 

 207         try {
 208             // validate if there is a public static final field in PlatformLogger
 209             Field constantField = PlatformLogger.class.getField(level.getName());
 210             int l = (int) constantField.get(null);
 211             if (l != platformLevel.intValue()) {
 212                 throw new RuntimeException("static final " + level.getName() + " (" + 
 213                     l + ") != " + platformLevel.intValue());
 214             }
 215         } catch (Exception e) {
 216             throw new RuntimeException("No public static PlatformLogger." + level.getName() +
 217                                        " field", e);
 218         }
 219         if (!platformLevel.name().equals(level.getName()))
 220             throw new RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is "
 221                                        + platformLevel.name() + " but expected " + level.getName());
 222 
 223         if (platformLevel.intValue() != level.intValue())
 224             throw new RuntimeException("The value of PlatformLogger." + level.intValue() + ".intValue() is "
 225                                        + platformLevel.intValue() + " but expected " + level.intValue());
 226     }
 227 
 228     static Point[] getPoints() {
 229         Point[] res = new Point[3];
 230         res[0] = new Point(0,0);
 231         res[1] = new Point(1,1);
 232         res[2] = new Point(2,2);
 233         return res;
 234     }
 235 
 236     static class Point {
 237         final int x;
 238         final int y;
 239         public Point(int x, int y) {
 240             this.x = x;
 241             this.y = y;
 242         }
 243         public String toString() {