1/* 2 * Copyright (c) 2017, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package pkg.b.t; 27 28import java.lang.reflect.Method; 29import java.lang.System.Logger; 30import java.util.ResourceBundle; 31import java.util.ListResourceBundle; 32 33/* 34 * Tests when logger client is in unnamed module 35 */ 36public class TestB { 37 38 public static void main(String[] args) throws Exception { 39 assertTrue(args.length == 2); 40 String loggerMode = args[0]; 41 String loggerClassName = args[1]; 42 43 testLogger(loggerMode, loggerClassName); 44 testLog(loggerClassName); 45 } 46 47 /* 48 * Tests System.getLogger(String) get expected logger. 49 */ 50 private static void testLogger(String loggerMode, String loggerClassName) { 51 final Module m = TestB.class.getModule(); 52 final ClassLoader moduleCL = m.getClassLoader(); 53 assertTrue(!m.isNamed()); 54 assertTrue(moduleCL != null); 55 56 String name = "test.b"; 57 Logger logger = getLogger(name); 58 printLogger(logger); 59 60 final Module lm = logger.getClass().getModule(); 61 final ClassLoader loggerCL = lm.getClassLoader(); 62 if (loggerMode.equals("system")) { 63 assertTrue(lm.isNamed()); 64 assertTrue(loggerCL == null); 65 } else if(loggerMode.equals("named")) { 66 assertTrue(lm.isNamed()); 67 assertTrue(loggerCL != null); 68 } else if(loggerMode.equals("unnamed")) { 69 assertTrue(!lm.isNamed()); 70 assertTrue(loggerCL != null); 71 } else { 72 throw new RuntimeException("wrong parameter"); 73 } 74 75 assertTrue(loggerClassName.equals(logger.getClass().getName())); 76 assertTrue(!loggerClassName.equals("jdk.internal.logger.LazyLoggers$JdkLazyLogger")); 77 } 78 79 /* 80 * Tests Logger retrieved by System.getLogger(String, ResourceBundle) and 81 * System.getLogger(String) works well. 82 */ 83 private static void testLog(String loggerClassName) throws Exception { 84 if (loggerClassName.equals("pkg.a.l.LoggerA") 85 || loggerClassName.equals("pkg.b.l.LoggerB")) { 86 87 String name = "test.b.B"; 88 String plainMsg = "this is test log message #1"; 89 ResourceBundle rb = new MyResourcesB(); 90 Throwable ex = new Throwable("this is an expected exception to be logged"); 91 Class<?> clazz = Class.forName(loggerClassName); 92 Method method = clazz.getMethod("checkLog", String.class, 93 System.Logger.Level.class, 94 ResourceBundle.class, String.class, 95 Throwable.class, Object[].class); 96 97 Logger logger = getLogger(name); 98 printLogger(logger); 99 assertTrue(logger.getClass().getName().equals(loggerClassName)); 100 assertTrue(logger.getName().equals(loggerClassName)); 101 logger.log(Logger.Level.WARNING, plainMsg); 102 boolean pass = (boolean)method.invoke(null, name, Logger.Level.WARNING, 103 null, plainMsg, ex, (Object)null); 104 assertTrue(pass); 105 pass = (boolean)method.invoke(null, name, Logger.Level.INFO, 106 rb, MyResourcesB.VALUE, (Throwable)null, 107 (Object)null); 108 assertTrue(!pass); 109 110 logger = getLogger(name, rb); 111 printLogger(logger); 112 assertTrue(logger.getClass().getName() 113 .equals("jdk.internal.logger.LocalizedLoggerWrapper")); 114 assertTrue(logger.getName().equals(loggerClassName)); 115 logger.log(Logger.Level.INFO, MyResourcesB.KEY); 116 pass = (boolean)method.invoke(null, name, Logger.Level.INFO, 117 rb, MyResourcesB.VALUE, (Throwable)null, 118 (Object)null); 119 assertTrue(pass); 120 pass = (boolean)method.invoke(null, name, Logger.Level.WARNING, 121 null, plainMsg, ex, (Object)null); 122 assertTrue(pass); 123 } 124 } 125 126 private static class MyResourcesB extends ListResourceBundle { 127 static final String KEY = "this is the key in MyResourcesB"; 128 static final String VALUE = "THIS IS THE VALUE IN MyResourcesB"; 129 130 @Override 131 protected Object[][] getContents() { 132 return new Object[][] { 133 {KEY, VALUE} 134 }; 135 } 136 } 137 138 private static Logger getLogger(String name) { 139 return System.getLogger(name); 140 } 141 142 private static Logger getLogger(String name, ResourceBundle rb) { 143 return System.getLogger(name, rb); 144 } 145 146 private static void printLogger(Logger logger) { 147 System.err.println("logger name: " + logger.getName() 148 + ", logger class: " + logger.getClass()); 149 } 150 151 private static void assertTrue(boolean b) { 152 if (!b) { 153 throw new RuntimeException("expected true, but get false."); 154 } 155 } 156} 157