/* * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 6882376 6985460 8010309 8011638 * @summary Test if java.util.logging.Logger is created before and after * logging is enabled. Also validate some basic PlatformLogger * operations. othervm mode to make sure java.util.logging * is not initialized. * * @modules java.base/sun.util.logging * java.logging/sun.util.logging.internal * @run main/othervm PlatformLoggerTest
*/
publicstaticvoid main(String[] args) throws Exception { final String FOO_PLATFORM_LOGGER = "test.platformlogger.foo"; final String BAR_PLATFORM_LOGGER = "test.platformlogger.bar"; final String GOO_PLATFORM_LOGGER = "test.platformlogger.goo"; final String BAR_LOGGER = "test.logger.bar";
goo = PlatformLogger.getLogger(GOO_PLATFORM_LOGGER); // test the PlatformLogger methods
testLogMethods(goo);
// Create a platform logger using the default
foo = PlatformLogger.getLogger(FOO_PLATFORM_LOGGER);
checkPlatformLogger(foo, FOO_PLATFORM_LOGGER);
// create a java.util.logging.Logger // now java.util.logging.Logger should be created for each platform logger
logger = Logger.getLogger(BAR_LOGGER);
logger.setLevel(Level.WARNING);
bar = PlatformLogger.getLogger(BAR_PLATFORM_LOGGER);
checkPlatformLogger(bar, BAR_PLATFORM_LOGGER);
// test the PlatformLogger methods
testLogMethods(goo);
testLogMethods(bar);
// don't use java.util.logging here to prevent it from initialized privatestaticvoid checkPlatformLogger(PlatformLogger logger, String name) { if (!logger.getName().equals(name)) { thrownew RuntimeException("Invalid logger's name " +
logger.getName() + " but expected " + name);
}
if (logger.level() != null) { thrownew RuntimeException("Invalid default level for logger " +
logger.getName() + ": " + logger.level());
}
final Logger javaLogger = Logger.getLogger("foo.bar.baz"); for (Level level : levels) {
checkJavaLoggerLevel(javaLogger, level);
}
Reference.reachabilityFence(javaLogger);
}
privatestaticvoid checkLoggerLevel(PlatformLogger logger, Level level) {
PlatformLogger.Level plevel = PlatformLogger.Level.valueOf(level.getName()); if (plevel != logger.level()) { thrownew RuntimeException("Retrieved PlatformLogger level "
+ logger.level()
+ " is not the same as set level " + plevel);
}
// check the level set in java.util.logging.Logger final Logger javaLogger = LogManager.getLogManager().getLogger(logger.getName());
Level javaLevel = javaLogger.getLevel(); if (javaLogger.getLevel() != level) { thrownew RuntimeException("Retrieved backing java.util.logging.Logger level "
+ javaLevel + " is not the expected " + level);
}
Reference.reachabilityFence(javaLogger);
}
privatestaticvoid checkJavaLoggerLevel(Logger logger, Level level) { // This method exercise the mapping of java level to platform level // when the java level is not one of the standard levels...
// create a brand new java logger final Logger javaLogger = sun.util.logging.internal.LoggingProviderImpl.getLogManagerAccess()
.demandLoggerFor(LogManager.getLogManager(),
logger.getName()+"."+level.getName(), Thread.class.getModule());
// Set a non standard java.util.logging.Level on the java logger // (except for OFF & ALL - which will remain unchanged) int intValue = level.intValue(); if (level != Level.ALL && level != Level.OFF) {
intValue -= 7;
}
javaLogger.setLevel(Level.parse(String.valueOf(intValue)));
// check the level set in java.util.logging.Logger
Level effectiveLevel = javaLogger.getLevel();
System.out.println("Effective Java Level used is: " + effectiveLevel);
if (effectiveLevel.intValue() != intValue) { thrownew RuntimeException("Retrieved backing java.util.logging.Logger level.intValue() "
+ effectiveLevel.intValue() + " is not the expected " + intValue);
} if (intValue != level.intValue() && javaLogger.getLevel() == level) { thrownew RuntimeException("Retrieved backing java.util.logging.Logger level "
+ effectiveLevel + " is " + level);
} if (intValue == level.intValue() && javaLogger.getLevel() != level) { thrownew RuntimeException("Retrieved backing java.util.logging.Logger level "
+ effectiveLevel + " is not " + level);
}
// check the level set in the PlatformLogger
PlatformLogger plogger = PlatformLogger.getLogger(javaLogger.getName());
PlatformLogger.Level expected = PlatformLogger.Level.valueOf(level.getName()); if (plogger.level() != expected) { thrownew RuntimeException("Retrieved backing PlatformLogger level "
+ plogger.level() + " is not the expected " + expected);
}
Reference.reachabilityFence(javaLogger);
}
privatestaticvoid checkPlatformLoggerLevelMapping(Level level) { // map the given level to PlatformLogger.Level of the same name and value
PlatformLogger.Level platformLevel = PlatformLogger.Level.valueOf(level.getName()); if (platformLevel.intValue() != level.intValue()) { thrownew RuntimeException("Mismatched level: " + level
+ " PlatformLogger.Level" + platformLevel);
} if (!platformLevel.name().equals(level.getName())) { thrownew RuntimeException("The value of PlatformLogger." + level.getName() + ".name() is "
+ platformLevel.name() + " but expected " + level.getName());
}
}
static Point[] getPoints() {
Point[] res = new Point[3];
res[0] = new Point(0,0);
res[1] = new Point(1,1);
res[2] = new Point(2,2); return res;
}
staticclass Point { finalint x; finalint y; public Point(int x, int y) { this.x = x; this.y = y;
} public String toString() { return"{x="+x + ", y=" + y + "}";
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.