Path: blob/master/test/jdk/java/lang/System/LoggerFinder/BaseLoggerFinderTest/TestLoggerFinder.java
41154 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.util.Arrays;24import java.util.Objects;25import java.util.Queue;26import java.util.ResourceBundle;27import java.util.concurrent.ArrayBlockingQueue;28import java.util.concurrent.ConcurrentHashMap;29import java.util.concurrent.atomic.AtomicLong;30import java.util.function.Supplier;31import java.lang.System.Logger;3233/**34* What our test provider needs to implement.35* @author danielfuchs36*/37public interface TestLoggerFinder {38public final static AtomicLong sequencer = new AtomicLong();39public final ConcurrentHashMap<String, LoggerImpl> system = new ConcurrentHashMap<>();40public final ConcurrentHashMap<String, LoggerImpl> user = new ConcurrentHashMap<>();41public final Queue<LogEvent> eventQueue = new ArrayBlockingQueue<>(128);4243public static final class LogEvent {4445public LogEvent() {46this(sequencer.getAndIncrement());47}4849LogEvent(long sequenceNumber) {50this.sequenceNumber = sequenceNumber;51}5253long sequenceNumber;54boolean isLoggable;55String loggerName;56Logger.Level level;57ResourceBundle bundle;58Throwable thrown;59Object[] args;60Supplier<String> supplier;61String msg;6263Object[] toArray() {64return new Object[] {65sequenceNumber,66isLoggable,67loggerName,68level,69bundle,70thrown,71args,72supplier,73msg,74};75}7677@Override78public String toString() {79return Arrays.deepToString(toArray());80}81828384@Override85public boolean equals(Object obj) {86return obj instanceof LogEvent87&& Objects.deepEquals(this.toArray(), ((LogEvent)obj).toArray());88}8990@Override91public int hashCode() {92return Objects.hash(toArray());93}949596public static LogEvent of(boolean isLoggable, String name,97Logger.Level level, ResourceBundle bundle,98String key, Throwable thrown) {99LogEvent evt = new LogEvent();100evt.isLoggable = isLoggable;101evt.loggerName = name;102evt.level = level;103evt.args = null;104evt.bundle = bundle;105evt.thrown = thrown;106evt.supplier = null;107evt.msg = key;108return evt;109}110111public static LogEvent of(boolean isLoggable, String name,112Logger.Level level, ResourceBundle bundle,113String key, Object... params) {114LogEvent evt = new LogEvent();115evt.isLoggable = isLoggable;116evt.loggerName = name;117evt.level = level;118evt.args = params;119evt.bundle = bundle;120evt.thrown = null;121evt.supplier = null;122evt.msg = key;123return evt;124}125126public static LogEvent of(long sequenceNumber,127boolean isLoggable, String name,128Logger.Level level, ResourceBundle bundle,129String key, Supplier<String> supplier,130Throwable thrown, Object... params) {131LogEvent evt = new LogEvent(sequenceNumber);132evt.loggerName = name;133evt.level = level;134evt.args = params;135evt.bundle = bundle;136evt.thrown = thrown;137evt.supplier = supplier;138evt.msg = key;139evt.isLoggable = isLoggable;140return evt;141}142143}144145public class LoggerImpl implements Logger {146final String name;147Logger.Level level = Logger.Level.INFO;148149public LoggerImpl(String name) {150this.name = name;151}152153@Override154public String getName() {155return name;156}157158@Override159public boolean isLoggable(Logger.Level level) {160return this.level != Logger.Level.OFF && this.level.getSeverity() <= level.getSeverity();161}162163@Override164public void log(Logger.Level level, ResourceBundle bundle, String key, Throwable thrown) {165log(LogEvent.of(isLoggable(level), this.name, level, bundle, key, thrown));166}167168@Override169public void log(Logger.Level level, ResourceBundle bundle, String format, Object... params) {170log(LogEvent.of(isLoggable(level), name, level, bundle, format, params));171}172173void log(LogEvent event) {174eventQueue.add(event);175}176}177178public Logger getLogger(String name, Module caller);179public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);180}181182183