1 /*
   2  * Copyright (c) 2009, 2011, 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 /*
  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"));
 150         logger.info("Test info(String)");
 151     }
 152 
 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() {
 244             return "{x="+x + ", y=" + y + "}";
 245         }
 246     }
 247 
 248 }