Path: blob/master/test/jdk/java/lang/System/Logger/interface/LoggerInterfaceTest.java
41155 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.ResourceBundle;24import java.util.function.Consumer;25import java.lang.System.Logger.Level;26import java.util.Arrays;27import java.util.LinkedList;28import java.util.Objects;29import java.util.Queue;30import java.util.function.Supplier;3132/**33* @test34* @bug 814036435* @summary Tests the default body of the System.Logger interface.36* @author danielfuchs37*/38public class LoggerInterfaceTest {3940public static class LoggerImpl implements System.Logger {4142public static class LogEvent implements Cloneable {43Level level;44ResourceBundle bundle;45String msg;46Throwable thrown;47Object[] params;48StackTraceElement[] callStack;4950@Override51protected LogEvent clone() {52try {53return (LogEvent)super.clone();54} catch (CloneNotSupportedException x) {55throw new RuntimeException(x);56}57}585960}6162public static class LogEventBuilder {63private LogEvent event = new LogEvent();64public LogEventBuilder level(Level level) {65event.level = level;66return this;67}68public LogEventBuilder stack(StackTraceElement... stack) {69event.callStack = stack;70return this;71}72public LogEventBuilder bundle(ResourceBundle bundle) {73event.bundle = bundle;74return this;75}76public LogEventBuilder msg(String msg) {77event.msg = msg;78return this;79}80public LogEventBuilder thrown(Throwable thrown) {81event.thrown = thrown;82return this;83}84public LogEventBuilder params(Object... params) {85event.params = params;86return this;87}88public LogEvent build() {89return event.clone();90}9192public LogEventBuilder clear() {93event = new LogEvent();94return this;95}9697}9899Level level = Level.WARNING;100Consumer<LogEvent> consumer;101final LogEventBuilder builder = new LogEventBuilder();102103@Override104public String getName() {105return "noname";106}107108@Override109public boolean isLoggable(Level level) {110return level.getSeverity() >= this.level.getSeverity();111}112113@Override114public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {115builder.clear().level(level).bundle(bundle).msg(msg).thrown(thrown)116.stack(new Exception().getStackTrace());117consumer.accept(builder.build());118}119120@Override121public void log(Level level, ResourceBundle bundle, String format, Object... params) {122builder.clear().level(level).bundle(bundle).msg(format).params(params)123.stack(new Exception().getStackTrace());124consumer.accept(builder.build());125}126127}128129static class Throwing {130@Override131public String toString() {132throw new RuntimeException("should not have been called");133}134}135static class NotTrowing {136private final String toString;137private int count = 0;138public NotTrowing(String toString) {139this.toString = toString;140}141142@Override143public String toString() {144return toString + "[" + (++count) + "]";145}146}147148public static void main(String[] args) {149final LoggerImpl loggerImpl = new LoggerImpl();150final System.Logger logger = loggerImpl;151final Queue<LoggerImpl.LogEvent> events = new LinkedList<>();152loggerImpl.consumer = (x) -> events.add(x);153154System.out.println("\nlogger.isLoggable(Level)");155assertTrue(logger.isLoggable(Level.WARNING), "logger.isLoggable(Level.WARNING)"," ");156assertFalse(logger.isLoggable(Level.INFO), "logger.isLoggable(Level.INFO)", " ");157158159System.out.println("\nlogger.log(Level, Object)");160for (Level l : Level.values()) {161boolean logged = l.compareTo(Level.WARNING) >= 0;162Object[][] cases = new Object[][] {163{null}, {"baz"}164};165for (Object[] p : cases) {166String msg = (String)p[0];167final Object obj = msg == null ? null : logged ? new NotTrowing(msg) : new Throwing();168String par1 = msg == null ? "(Object)null"169: logged ? "new NotTrowing(\""+ msg+"\")" : "new Throwing()";170System.out.println(" logger.log(" + l + ", " + par1 + ")");171try {172logger.log(l, obj);173if (obj == null) {174throw new RuntimeException("Expected NullPointerException not thrown for"175+ " logger.log(" + l + ", " + par1 + ")");176}177} catch (NullPointerException x) {178if (obj == null) {179System.out.println(" Got expected exception: " + x);180continue;181} else {182throw x;183}184}185LoggerImpl.LogEvent e = events.poll();186if (logged) {187assertNonNull(e, "e", " ");188assertEquals(l, e.level, "e.level", " ");189assertToString(e.msg, msg, 1, "e.msg", " ");190assertEquals(e.bundle, null, "e.bundle", " ");191assertEquals(e.params, null, "e.params", " ");192assertEquals(e.thrown, null, "e.thrown", " ");193assertEquals(e.bundle, null, "e.bundle", " ");194assertEquals(e.callStack[0].getMethodName(), "log",195"e.callStack[0].getMethodName()", " ");196assertEquals(e.callStack[0].getClassName(),197logger.getClass().getName(),198"e.callStack[0].getClassName() ", " ");199assertEquals(e.callStack[1].getMethodName(), "log",200"e.callStack[1].getMethodName()", " ");201assertEquals(e.callStack[1].getClassName(),202System.Logger.class.getName(),203"e.callStack[1].getClassName() ", " ");204assertEquals(e.callStack[2].getMethodName(), "main",205"e.callStack[2].getMethodName()", " ");206} else {207assertEquals(e, null, "e", " ");208}209}210}211System.out.println(" logger.log(" + null + ", " +212"new NotThrowing(\"foobar\")" + ")");213try {214logger.log(null, new NotTrowing("foobar"));215throw new RuntimeException("Expected NullPointerException not thrown for"216+ " logger.log(" + null + ", "217+ "new NotThrowing(\"foobar\")" + ")");218} catch (NullPointerException x) {219System.out.println(" Got expected exception: " + x);220}221222223System.out.println("\nlogger.log(Level, String)");224for (Level l : Level.values()) {225boolean logged = l.compareTo(Level.WARNING) >= 0;226String par = "bar";227System.out.println(" logger.log(" + l + ", \"" + par +"\");");228logger.log(l, par);229LoggerImpl.LogEvent e = events.poll();230assertNonNull(e, "e", " ");231assertEquals(e.level, l, "e.level", " ");232assertEquals(e.msg, "bar", "e.msg", " ");233assertEquals(e.bundle, null, "e.bundle", " ");234assertEquals(e.params, null, "e.params", " ");235assertEquals(e.thrown, null, "e.thrown", " ");236assertEquals(e.bundle, null, "e.bundle", " ");237assertEquals(e.callStack[0].getMethodName(), "log",238"e.callStack[0].getMethodName()", " ");239assertEquals(e.callStack[0].getClassName(),240logger.getClass().getName(),241"e.callStack[0].getClassName() ", " ");242assertEquals(e.callStack[1].getMethodName(), "log",243"e.callStack[1].getMethodName()", " ");244assertEquals(e.callStack[1].getClassName(),245System.Logger.class.getName(),246"e.callStack[1].getClassName() ", " ");247assertEquals(e.callStack[2].getMethodName(), "main",248"e.callStack[2].getMethodName()", " ");249250System.out.println(" logger.log(" + l + ", (String)null);");251logger.log(l, (String)null);252e = events.poll();253assertNonNull(e, "e", " ");254assertEquals(e.level, l, "e.level", " ");255assertEquals(e.msg, null, "e.msg", " ");256assertEquals(e.bundle, null, "e.bundle", " ");257assertEquals(e.params, null, "e.params", " ");258assertEquals(e.thrown, null, "e.thrown", " ");259assertEquals(e.bundle, null, "e.bundle", " ");260assertEquals(e.callStack[0].getMethodName(), "log",261"e.callStack[0].getMethodName()", " ");262assertEquals(e.callStack[0].getClassName(),263logger.getClass().getName(),264"e.callStack[0].getClassName() ", " ");265assertEquals(e.callStack[1].getMethodName(), "log",266"e.callStack[1].getMethodName()", " ");267assertEquals(e.callStack[1].getClassName(),268System.Logger.class.getName(),269"e.callStack[1].getClassName() ", " ");270assertEquals(e.callStack[2].getMethodName(), "main",271"e.callStack[2].getMethodName()", " ");272}273274System.out.println("\nlogger.log(Level, Supplier<String>)");275for (Level l : Level.values()) {276boolean logged = l.compareTo(Level.WARNING) >= 0;277Object[][] cases = new Object[][] {278{null}, {"baz"}279};280for (Object[] p : cases) {281String msg = (String)p[0];282final Object obj = msg == null ? null : logged ? new NotTrowing(msg) : new Throwing();283final Supplier<String> s = msg == null ? null : () -> obj.toString();284String par1 = msg == null ? "(Supplier<String>)null"285: logged ? "() -> new NotTrowing(\""+ msg+"\").toString()" : "new Throwing()";286System.out.println(" logger.log(" + l + ", " + par1 + ")");287try {288logger.log(l, s);289if (s == null) {290throw new RuntimeException("Expected NullPointerException not thrown for"291+ " logger.log(" + l + ", " + par1 + ")");292}293} catch (NullPointerException x) {294if (s == null) {295System.out.println(" Got expected exception: " + x);296continue;297} else {298throw x;299}300}301LoggerImpl.LogEvent e = events.poll();302if (logged) {303assertNonNull(e, "e", " ");304assertEquals(l, e.level, "e.level", " ");305assertToString(e.msg, msg, 1, "e.msg", " ");306assertEquals(e.bundle, null, "e.bundle", " ");307assertEquals(e.params, null, "e.params", " ");308assertEquals(e.thrown, null, "e.thrown", " ");309assertEquals(e.bundle, null, "e.bundle", " ");310assertEquals(e.callStack[0].getMethodName(), "log",311"e.callStack[0].getMethodName()", " ");312assertEquals(e.callStack[0].getClassName(),313logger.getClass().getName(),314"e.callStack[0].getClassName() ", " ");315assertEquals(e.callStack[1].getMethodName(), "log",316"e.callStack[1].getMethodName()", " ");317assertEquals(e.callStack[1].getClassName(),318System.Logger.class.getName(),319"e.callStack[1].getClassName() ", " ");320assertEquals(e.callStack[2].getMethodName(), "main",321"e.callStack[2].getMethodName()", " ");322} else {323assertEquals(e, null, "e", " ");324}325}326}327System.out.println(" logger.log(" + null + ", " + "() -> \"biz\"" + ")");328try {329logger.log(null, () -> "biz");330throw new RuntimeException("Expected NullPointerException not thrown for"331+ " logger.log(" + null + ", "332+ "() -> \"biz\"" + ")");333} catch (NullPointerException x) {334System.out.println(" Got expected exception: " + x);335}336337System.out.println("\nlogger.log(Level, String, Object...)");338for (Level l : Level.values()) {339boolean logged = l.compareTo(Level.WARNING) >= 0;340String par = "bam";341Object[] params = null;342System.out.println(" logger.log(" + l + ", \"" + par +"\", null);");343logger.log(l, par, params);344LoggerImpl.LogEvent e = events.poll();345assertNonNull(e, "e", " ");346assertEquals(l, e.level, "e.level", " ");347assertEquals(e.msg, "bam", "e.msg", " ");348assertEquals(e.bundle, null, "e.bundle", " ");349assertEquals(e.params, null, "e.params", " ");350assertEquals(e.thrown, null, "e.thrown", " ");351assertEquals(e.bundle, null, "e.bundle", " ");352assertEquals(e.callStack[0].getMethodName(), "log",353"e.callStack[0].getMethodName()", " ");354assertEquals(e.callStack[0].getClassName(),355logger.getClass().getName(),356"e.callStack[0].getClassName() ", " ");357assertEquals(e.callStack[1].getMethodName(), "log",358"e.callStack[1].getMethodName()", " ");359assertEquals(e.callStack[1].getClassName(),360System.Logger.class.getName(),361"e.callStack[1].getClassName() ", " ");362assertEquals(e.callStack[2].getMethodName(), "main",363"e.callStack[2].getMethodName()", " ");364365params = new Object[] {new NotTrowing("one")};366par = "bam {0}";367System.out.println(" logger.log(" + l + ", \"" + par368+ "\", new NotTrowing(\"one\"));");369logger.log(l, par, params[0]);370e = events.poll();371assertNonNull(e, "e", " ");372assertEquals(l, e.level, "e.level", " ");373assertEquals(e.msg, par, "e.msg", " ");374assertEquals(e.bundle, null, "e.bundle", " ");375assertArrayEquals(e.params, params, "e.params", " ");376assertEquals(e.thrown, null, "e.thrown", " ");377assertEquals(e.bundle, null, "e.bundle", " ");378assertEquals(e.callStack[0].getMethodName(), "log",379"e.callStack[0].getMethodName()", " ");380assertEquals(e.callStack[0].getClassName(),381logger.getClass().getName(),382"e.callStack[0].getClassName() ", " ");383assertEquals(e.callStack[1].getMethodName(), "log",384"e.callStack[1].getMethodName()", " ");385assertEquals(e.callStack[1].getClassName(),386System.Logger.class.getName(),387"e.callStack[1].getClassName() ", " ");388assertEquals(e.callStack[2].getMethodName(), "main",389"e.callStack[2].getMethodName()", " ");390391params = new Object[] {new NotTrowing("fisrt"), new NotTrowing("second")};392par = "bam {0} {1}";393System.out.println(" logger.log(" + l + ", \"" + par394+ "\", new NotTrowing(\"fisrt\"),"395+ " new NotTrowing(\"second\"));");396logger.log(l, par, params[0], params[1]);397e = events.poll();398assertNonNull(e, "e", " ");399assertEquals(l, e.level, "e.level", " ");400assertEquals(e.msg, par, "e.msg", " ");401assertEquals(e.bundle, null, "e.bundle", " ");402assertArrayEquals(e.params, params, "e.params", " ");403assertEquals(e.thrown, null, "e.thrown", " ");404assertEquals(e.bundle, null, "e.bundle", " ");405assertEquals(e.callStack[0].getMethodName(), "log",406"e.callStack[0].getMethodName()", " ");407assertEquals(e.callStack[0].getClassName(),408logger.getClass().getName(),409"e.callStack[0].getClassName() ", " ");410assertEquals(e.callStack[1].getMethodName(), "log",411"e.callStack[1].getMethodName()", " ");412assertEquals(e.callStack[1].getClassName(),413System.Logger.class.getName(),414"e.callStack[1].getClassName() ", " ");415assertEquals(e.callStack[2].getMethodName(), "main",416"e.callStack[2].getMethodName()", " ");417418params = new Object[] {new NotTrowing("third"), new NotTrowing("fourth")};419par = "bam {2}";420System.out.println(" logger.log(" + l + ", \"" + par421+ "\", new Object[] {new NotTrowing(\"third\"),"422+ " new NotTrowing(\"fourth\")});");423logger.log(l, par, params);424e = events.poll();425assertNonNull(e, "e", " ");426assertEquals(l, e.level, "e.level", " ");427assertEquals(e.msg, par, "e.msg", " ");428assertEquals(e.bundle, null, "e.bundle", " ");429assertArrayEquals(e.params, params, "e.params", " ");430assertEquals(e.thrown, null, "e.thrown", " ");431assertEquals(e.bundle, null, "e.bundle", " ");432assertEquals(e.callStack[0].getMethodName(), "log",433"e.callStack[0].getMethodName()", " ");434assertEquals(e.callStack[0].getClassName(), logger.getClass().getName(),435"e.callStack[0].getClassName() ", " ");436assertEquals(e.callStack[1].getMethodName(), "log",437"e.callStack[1].getMethodName()", " ");438assertEquals(e.callStack[1].getClassName(),439System.Logger.class.getName(),440"e.callStack[1].getClassName() ", " ");441assertEquals(e.callStack[2].getMethodName(), "main",442"e.callStack[2].getMethodName()", " ");443}444445System.out.println("\nlogger.log(Level, String, Throwable)");446for (Level l : Level.values()) {447boolean logged = l.compareTo(Level.WARNING) >= 0;448Object[][] cases = new Object[][] {449{null, null}, {null, new Throwable()}, {"biz", null}, {"boz", new Throwable()}450};451for (Object[] p : cases) {452String msg = (String)p[0];453Throwable thrown = (Throwable)p[1];454String par1 = msg == null ? "(String)null" : "\"" + msg + "\"";455String par2 = thrown == null ? "(Throwable)null" : "new Throwable()";456System.out.println(" logger.log(" + l + ", " + par1 +", " + par2 + ")");457logger.log(l, msg, thrown);458LoggerImpl.LogEvent e = events.poll();459assertNonNull(e, "e", " ");460assertEquals(e.level, l, "e.level", " ");461assertEquals(e.msg, msg, "e.msg", " ");462assertEquals(e.bundle, null, "e.bundle", " ");463assertEquals(e.params, null, "e.params", " ");464assertEquals(e.thrown, thrown, "e.thrown", " ");465assertEquals(e.bundle, null, "e.bundle", " ");466assertEquals(e.callStack[0].getMethodName(),467"log", "e.callStack[0].getMethodName()", " ");468assertEquals(e.callStack[0].getClassName(),469logger.getClass().getName(),470"e.callStack[0].getClassName() ", " ");471assertEquals(e.callStack[1].getMethodName(), "log",472"e.callStack[1].getMethodName()", " ");473assertEquals(e.callStack[1].getClassName(),474System.Logger.class.getName(),475"e.callStack[1].getClassName() ", " ");476assertEquals(e.callStack[2].getMethodName(), "main",477"e.callStack[2].getMethodName()", " ");478}479}480481System.out.println("\nlogger.log(Level, Supplier<String>, Throwable)");482for (Level l : Level.values()) {483boolean logged = l.compareTo(Level.WARNING) >= 0;484Object[][] cases = new Object[][] {485{null, null}, {null, new Throwable()}, {"biz", null}, {"boz", new Throwable()}486};487for (Object[] p : cases) {488String msg = (String)p[0];489Throwable thrown = (Throwable)p[1];490final Object obj = msg == null ? null : logged ? new NotTrowing(msg) : new Throwing();491final Supplier<String> s = msg == null ? null : () -> obj.toString();492String par1 = msg == null ? "(Supplier<String>)null"493: logged ? "() -> new NotTrowing(\""+ msg+"\").toString()" : "new Throwing()";494String par2 = thrown == null ? "(Throwable)null" : "new Throwable()";495System.out.println(" logger.log(" + l + ", " + par1 +", " + par2 + ")");496try {497logger.log(l, s, thrown);498if (s== null) {499throw new RuntimeException("Expected NullPointerException not thrown for"500+ " logger.log(" + l + ", " + par1 +", " + par2 + ")");501}502} catch (NullPointerException x) {503if (s == null) {504System.out.println(" Got expected exception: " + x);505continue;506} else {507throw x;508}509}510LoggerImpl.LogEvent e = events.poll();511if (logged) {512assertNonNull(e, "e", " ");513assertEquals(l, e.level, "e.level", " ");514assertToString(e.msg, msg, 1, "e.msg", " ");515assertEquals(e.bundle, null, "e.bundle", " ");516assertEquals(e.params, null, "e.params", " ");517assertEquals(e.thrown, thrown, "e.thrown", " ");518assertEquals(e.bundle, null, "e.bundle", " ");519assertEquals(e.callStack[0].getMethodName(), "log",520"e.callStack[0].getMethodName()", " ");521assertEquals(e.callStack[0].getClassName(),522logger.getClass().getName(),523"e.callStack[0].getClassName() ", " ");524assertEquals(e.callStack[1].getMethodName(), "log",525"e.callStack[1].getMethodName()", " ");526assertEquals(e.callStack[1].getClassName(),527System.Logger.class.getName(),528"e.callStack[1].getClassName() ", " ");529assertEquals(e.callStack[2].getMethodName(), "main",530"e.callStack[2].getMethodName()", " ");531} else {532assertEquals(e, null, "e", " ");533}534}535}536System.out.println(" logger.log(" + null + ", " + "() -> \"biz\""537+ ", " + "new Throwable()" + ")");538try {539logger.log(null, () -> "biz", new Throwable());540throw new RuntimeException("Expected NullPointerException not thrown for"541+ " logger.log(" + null + ", "542+ "() -> \"biz\"" + ", "543+ "new Throwable()" + ")");544} catch (NullPointerException x) {545System.out.println(" Got expected exception: " + x);546}547548System.out.println("Checking that we have no spurious events in the queue");549assertEquals(events.poll(), null, "events.poll()", " ");550}551552static void assertTrue(boolean test, String what, String prefix) {553if (!test) {554throw new RuntimeException("Expected true for " + what);555}556System.out.println(prefix + "Got expected " + what + ": " + test);557}558static void assertFalse(boolean test, String what, String prefix) {559if (test) {560throw new RuntimeException("Expected false for " + what);561}562System.out.println(prefix + "Got expected " + what + ": " + test);563}564static void assertToString(String actual, String expected, int count, String what, String prefix) {565assertEquals(actual, expected + "["+count+"]", what, prefix);566}567static void assertEquals(Object actual, Object expected, String what, String prefix) {568if (!Objects.equals(actual, expected)) {569throw new RuntimeException("Bad " + what + ":"570+ "\n\t expected: " + expected571+ "\n\t actual: " + actual);572}573System.out.println(prefix + "Got expected " + what + ": " + actual);574}575static void assertArrayEquals(Object[] actual, Object[] expected, String what, String prefix) {576if (!Objects.deepEquals(actual, expected)) {577throw new RuntimeException("Bad " + what + ":"578+ "\n\t expected: " + expected == null ? "null" : Arrays.deepToString(expected)579+ "\n\t actual: " + actual == null ? "null" : Arrays.deepToString(actual));580}581System.out.println(prefix + "Got expected " + what + ": " + Arrays.deepToString(actual));582}583static void assertNonNull(Object actual, String what, String prefix) {584if (Objects.equals(actual, null)) {585throw new RuntimeException("Bad " + what + ":"586+ "\n\t expected: non null"587+ "\n\t actual: " + actual);588}589System.out.println(prefix + "Got expected " + what + ": " + "non null");590}591}592593594