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