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. 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.util.logging.*; 37 import sun.util.logging.PlatformLogger; 38 39 public class PlatformLoggerTest { 40 private static final int defaultEffectiveLevel = 0; 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 } 76 77 private static void checkPlatformLogger(PlatformLogger logger, String name) { 78 if (!logger.getName().equals(name)) { 79 throw new RuntimeException("Invalid logger's name " + 80 logger.getName() + " but expected " + name); 81 } 82 83 if (logger.getLevel() != defaultEffectiveLevel) { 84 throw new RuntimeException("Invalid default level for logger " + 85 logger.getName()); 86 } 87 88 if (logger.isLoggable(PlatformLogger.FINE) != false) { 89 throw new RuntimeException("isLoggerable(FINE) returns true for logger " + 90 logger.getName() + " but expected false"); 91 } 92 93 logger.setLevel(PlatformLogger.FINER); 94 if (logger.getLevel() != Level.FINER.intValue()) { 95 throw new RuntimeException("Invalid level for logger " + 96 logger.getName() + " " + logger.getLevel()); 97 } 98 99 if (logger.isLoggable(PlatformLogger.FINE) != true) { 100 throw new RuntimeException("isLoggerable(FINE) returns false for logger " + 101 logger.getName() + " but expected true"); 102 } 103 104 logger.info("OK: Testing log message"); 105 } 106 107 private static void checkLogger(String name, Level level) { 108 Logger logger = LogManager.getLogManager().getLogger(name); 109 if (logger == null) { 110 throw new RuntimeException("Logger " + name + 111 " does not exist"); 112 } 113 114 if (logger.getLevel() != level) { 115 throw new RuntimeException("Invalid level for logger " + 116 logger.getName() + " " + logger.getLevel()); 117 } 118 } 119 120 private static void testLogMethods(PlatformLogger logger) { 121 logger.severe("Test severe(String, Object...) {0} {1}", new Long(1), "string"); 122 // test Object[] 123 logger.severe("Test severe(String, Object...) {0}", (Object[]) getPoints()); 124 logger.warning("Test warning(String, Throwable)", new Throwable("Testing")); 125 logger.info("Test info(String)"); 126 } 127 128 static Point[] getPoints() { 129 Point[] res = new Point[3]; 130 res[0] = new Point(0,0); 131 res[1] = new Point(1,1); 132 res[2] = new Point(2,2); 133 return res; 134 } 135 136 static class Point { 137 final int x; 138 final int y; 139 public Point(int x, int y) { 140 this.x = x; 141 this.y = y; 142 } 143 public String toString() { 144 return "{x="+x + ", y=" + y + "}"; 145 } 146 } 147 148 }