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