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 
  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.Level.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.level() != null) {
  85             throw new RuntimeException("Invalid default level for logger " +
  86                 logger.getName() + ": " + logger.level());
  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.level());
  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"));
 126         logger.info("Test info(String)");
 127     }
 128 
 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                 logger.setLevel(platformLevel);       // setLevel(PlatformLogger.Level)
 150                 checkLoggerLevel(logger, level);
 151 
 152                 logger.setLevel(PlatformLogger.ALL);  // setLevel(int)
 153                 checkLoggerLevel(logger, Level.ALL);
 154             }
 155         }
 156     }
 157 
 158     private static void checkLoggerLevel(PlatformLogger logger, Level level) {
 159         PlatformLogger.Level plevel = PlatformLogger.Level.valueOf(level.getName());
 160         if (plevel != logger.level()) {
 161             throw new RuntimeException("Retrieved PlatformLogger level "
 162                     + logger.level()
 163                     + " is not the same as set level " + plevel);
 164         }
 165 
 166         // check the level set in java.util.logging.Logger
 167         Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName());
 168         Level javaLevel = javaLogger.getLevel();
 169         if (javaLogger.getLevel() != level) {
 170             throw new RuntimeException("Retrieved backing java.util.logging.Logger level "
 171                     + javaLevel + " is not the expected " + level);
 172         }
 173     }
 174 
 175     private static void checkPlatformLoggerLevelMapping(Level level) {
 176         // map the given level to PlatformLogger.Level of the same name and value
 177         PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName());
 178         if (platformLevel.intValue() != level.intValue()) {
 179             throw new RuntimeException("Mismatched level: " + level
 180                     + " PlatformLogger.Level" + platformLevel);
 181         }
 182 
 183         try {
 184             // validate if there is a public static final field in PlatformLogger
 185             Field constantField = PlatformLogger.class.getField(level.getName());
 186             int value = ((Integer)constantField.get(null)).intValue();
 187             if (value != platformLevel.intValue()) {
 188                 throw new RuntimeException("static final " + level.getName() +
 189                     " != " + platformLevel.intValue());
 190             }
 191         } catch (Exception e) {
 192             throw new RuntimeException("No public static PlatformLogger." + level.getName() +
 193                                        " field", e);
 194         }
 195         if (!platformLevel.name().equals(level.getName()))
 196             throw new RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is "
 197                                        + platformLevel.name() + " but expected " + level.getName());
 198 
 199         if (platformLevel.intValue() != level.intValue())
 200             throw new RuntimeException("The value of PlatformLogger." + level.intValue() + ".intValue() is "
 201                                        + platformLevel.intValue() + " but expected " + level.intValue());
 202     }
 203 
 204     static Point[] getPoints() {
 205         Point[] res = new Point[3];
 206         res[0] = new Point(0,0);
 207         res[1] = new Point(1,1);
 208         res[2] = new Point(2,2);
 209         return res;
 210     }
 211 
 212     static class Point {
 213         final int x;
 214         final int y;
 215         public Point(int x, int y) {
 216             this.x = x;
 217             this.y = y;
 218         }
 219         public String toString() {
 220             return "{x="+x + ", y=" + y + "}";
 221         }
 222     }
 223 
 224 }