Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/BootstrapLogger/BootstrapLoggerAPIsTest.java
41161 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.PrintStream;24import java.lang.System.Logger;25import java.lang.System.Logger.Level;26import java.util.ArrayList;27import java.util.concurrent.atomic.AtomicBoolean;28import java.util.Enumeration;29import java.util.List;30import java.util.ResourceBundle;31import java.util.Set;32import jdk.internal.logger.BootstrapLogger;33import jdk.internal.logger.LazyLoggers;3435/*36* @test37* @bug 8144460 814421438* @summary Cover the logXX and LogEvent.valueOf APIs of BootstrapLogger39* and logXX APIs of SimpleConsoleLogger.40* @modules java.base/jdk.internal.logger:+open41* java.base/sun.util.logging42* @build BootstrapLoggerUtils LogStream43* @run main/othervm BootstrapLoggerAPIsTest44*/4546public class BootstrapLoggerAPIsTest {4748private static final LogStream ERR = new LogStream();4950public static void main(String[] args) throws Exception {5152final ContentManager MGR = new ContentManager();5354// private reflection hook that allows us to simulate a non booted VM55final AtomicBoolean VM_BOOTED = new AtomicBoolean(false);5657BootstrapLoggerUtils.setBootedHook(() -> VM_BOOTED.get());5859// We replace System.err to check the messages that have been logged60// by the JUL ConsoleHandler and default SimpleConsoleLogger61// implementaion62System.setErr(new PrintStream(ERR));6364VM_BOOTED.getAndSet(false);65if (BootstrapLogger.isBooted()) {66throw new RuntimeException("VM should not be booted!");67}6869final Logger LOGGER =70LazyLoggers.getLogger("foo.bar", Thread.class.getModule());71final sun.util.logging.PlatformLogger.Level PLATFORM_LEVEL =72sun.util.logging.PlatformLogger.Level.SEVERE;73final MyResources BUNDLE = new MyResources();7475/*76* Test logXX APIs for interface java.lang.System.Logger. Log content77* before VM is booted should be retained. Log content after VM was78* booted should be flushed instantly. VM is not booted in first round79* of loop, VM is booted in second round of loop.80*/81for (int i = 0; i < 2; i++) {82boolean booted = BootstrapLogger.isBooted();8384// make sure there is no [remaining] content in the LogStream.85MGR.failLog("xyz", "throwable #", "MyClass_#", "MyMethod_#");8687/*88* test logXX APIs for interface java.lang.System.Logger.89*/90// void log(java.lang.System$Logger$Level,java.util.ResourceBundle,91// java.lang.String,java.lang.Throwable)92LOGGER.log(Level.ERROR, BUNDLE, "abc #0", new RuntimeException("throwable #0"));93MGR.checkLog(booted, "xyz #0", "throwable #0");9495// void log(java.lang.System$Logger$Level,java.util.ResourceBundle,96// java.lang.String,java.lang.Object[])97LOGGER.log(Level.ERROR, BUNDLE, "abc #1");98MGR.checkLog(booted, "xyz #1");99100// void log(java.lang.System$Logger$Level,java.lang.String,java.lang.Object[])101LOGGER.log(Level.ERROR, BUNDLE, "abc {0}", "#2");102MGR.checkLog(booted, "xyz #2");103104// void log(java.lang.System$Logger$Level,java.lang.String,java.lang.Throwable)105LOGGER.log(Level.ERROR, "xyz #3", new RuntimeException("throwable #3"));106MGR.checkLog(booted, "xyz #3", "throwable #3");107108// void log(java.lang.System$Logger$Level,java.util.function.Supplier)109LOGGER.log(Level.ERROR, () -> "xyz #4");110MGR.checkLog(booted, "xyz #4");111112// void log(java.lang.System$Logger$Level,java.lang.Object)113LOGGER.log(Level.ERROR, new MyObject("xyz #5"));114MGR.checkLog(booted, "xyz #5");115116// void log(java.lang.System$Logger$Level,java.util.function.Supplier,117// java.lang.Throwable)118LOGGER.log(Level.ERROR, () -> "xyz #6", new RuntimeException("throwable #6"));119MGR.checkLog(booted, "xyz #6", "throwable #6");120121122/*123* test logXX APIs for interface124* sun.util.logging.PlatformLogger.Bridge.125*/126sun.util.logging.PlatformLogger.Bridge bridge =127(sun.util.logging.PlatformLogger.Bridge) LOGGER;128129// void log(sun.util.logging.PlatformLogger$Level,java.lang.String)130bridge.log(PLATFORM_LEVEL, "xyz #7");131MGR.checkLog(booted, "xyz #7");132133// void log(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.Throwable)134bridge.log(PLATFORM_LEVEL, "xyz #8", new RuntimeException("throwable #8"));135MGR.checkLog(booted, "xyz #8", "throwable #8");136137// void log(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.Object[])138bridge.log(PLATFORM_LEVEL, "xyz {0}", "#9");139MGR.checkLog(booted, "xyz #9");140141// void log(sun.util.logging.PlatformLogger$Level,java.util.function.Supplier)142bridge.log(PLATFORM_LEVEL, () -> "xyz #10");143MGR.checkLog(booted, "xyz #10");144145// void log(sun.util.logging.PlatformLogger$Level,146// java.lang.Throwable,java.util.function.Supplier)147bridge.log(PLATFORM_LEVEL, new RuntimeException("throwable #11"), () -> "xyz #11");148MGR.checkLog(booted, "xyz #11", "throwable #11");149150// void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,151// java.lang.String,java.lang.String)152bridge.logp(PLATFORM_LEVEL, "MyClass_#12", "MyMethod_#12", "xyz #12");153MGR.checkLog(booted, "xyz #12", "MyClass_#12", "MyMethod_#12");154155// void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,156// java.lang.String,java.util.function.Supplier)157bridge.logp(PLATFORM_LEVEL, "MyClass_#13", "MyMethod_#13", () -> "xyz #13");158MGR.checkLog(booted, "xyz #13", "MyClass_#13", "MyMethod_#13");159160// void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,161// java.lang.String,java.lang.String,java.lang.Object[])162bridge.logp(PLATFORM_LEVEL, "MyClass_#14", "MyMethod_#14", "xyz {0}", "#14");163MGR.checkLog(booted, "xyz #14", "MyClass_#14", "MyMethod_#14");164165// void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,166// java.lang.String,java.lang.String,java.lang.Throwable)167bridge.logp(PLATFORM_LEVEL, "MyClass_#15", "MyMethod_#15",168"xyz #15", new RuntimeException("throwable #15"));169MGR.checkLog(booted, "xyz #15", "throwable #15", "MyClass_#15", "MyMethod_#15");170171// void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,172// java.lang.String,java.lang.Throwable,java.util.function.Supplier)173bridge.logp(PLATFORM_LEVEL, "MyClass_#16", "MyMethod_#16",174new RuntimeException("throwable #16"), () -> "xyz #16");175MGR.checkLog(booted, "xyz #16", "throwable #16", "MyClass_#16", "MyMethod_#16");176177// void logrb(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.String,178// java.util.ResourceBundle,java.lang.String,java.lang.Object[])179bridge.logrb(PLATFORM_LEVEL, "MyClass_#17", "MyMethod_#17",180BUNDLE, "abc {0}", "#17");181MGR.checkLog(booted, "xyz #17", "MyClass_#17", "MyMethod_#17");182183// void logrb(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.String,184// java.util.ResourceBundle,java.lang.String,java.lang.Throwable)185bridge.logrb(PLATFORM_LEVEL, "MyClass_#18", "MyMethod_#18",186BUNDLE, "abc #18", new RuntimeException("throwable #18"));187MGR.checkLog(booted, "xyz #18", "throwable #18", "MyClass_#18", "MyMethod_#18");188189// void logrb(sun.util.logging.PlatformLogger$Level,java.util.ResourceBundle,190// java.lang.String,java.lang.Object[])191bridge.logrb(PLATFORM_LEVEL, BUNDLE, "abc {0}", "#19");192MGR.checkLog(booted, "xyz #19");193194// void logrb(sun.util.logging.PlatformLogger$Level,java.util.ResourceBundle,195// java.lang.String,java.lang.Throwable)196bridge.logrb(PLATFORM_LEVEL, BUNDLE, "abc #20",197new RuntimeException("throwable #20"));198MGR.checkLog(booted, "xyz #20", "throwable #20");199200/*201* retained log content should be flushed after VM is booted.202*/203if (!booted) {204VM_BOOTED.getAndSet(true);205// trigger the flush, make sure to call LOGGER.log(...)206// after VM_BOOTED.getAndSet(true) and before MGR.assertCachedLog()207LOGGER.log(Level.ERROR, "VM was just booted! This log should flush the cached logs.");208MGR.assertCachedLog();209}210}211}212213private static class ContentManager {214final List<String[]> cached = new ArrayList<String[]>();215String[] last;216217public void cache() {218cached.add(last);219}220221public ContentManager failLog(String... nonexistent) {222last = nonexistent;223for (String c : nonexistent) {224if (ERR.drain().contains(c)) {225throw new RuntimeException("Content \"" + nonexistent226+ "\" should not exist in the log!");227}228}229return this;230}231232public void assertLog(String... logs) {233String log = ERR.drain();234for (String str : logs) {235if (!log.contains(str)) {236throw new RuntimeException("Content \"" + str + "\" does not exist in the log!");237}238}239}240241public void checkLog(boolean booted, String... logs) {242if (!booted) {243failLog(logs).cache();244} else {245assertLog(logs);246}247}248249public void assertCachedLog() {250String log = ERR.drain();251for (String[] arr : cached) {252for (String c : arr) {253if (!log.contains(c)) {254throw new RuntimeException("Content \"" + c + "\" does not exist in the log!");255}256}257}258}259}260261private static class MyObject {262String str;263264public MyObject(String str) {265this.str = str;266}267268public String toString() {269return str;270}271}272273private static class MyResources extends ResourceBundle {274public Object handleGetObject(String key) {275if (key.contains("abc #") || key.contains("abc {")) {276return key.replaceAll("abc ", "xyz ");277}278return null;279}280281public Enumeration<String> getKeys() {282return null;283}284}285}286287288