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.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"));
 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                 // 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() {
 212             return "{x="+x + ", y=" + y + "}";
 213         }
 214     }
 215 
 216 }