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 
26 package pkg.a.t;
27 
28 import java.lang.reflect.Method;
29 import java.lang.System.Logger;
30 import java.util.ResourceBundle;
31 import java.util.ListResourceBundle;
32 
33 /*
34  * Tests when logger client is in named module m.l.a
35  */
36 public class TestA {
37 
main(String[] args)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      */
testLogger(String loggerMode, String loggerClassName)50     private static void testLogger(String loggerMode, String loggerClassName) {
51         final Module m = TestA.class.getModule();
52         final ClassLoader moduleCL = m.getClassLoader();
53         assertTrue(m.isNamed());
54         assertTrue(moduleCL != null);
55 
56         String name = "test.a";
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      */
testLog(String loggerClassName)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.a.A";
88             String plainMsg = "this is test log message #1";
89             ResourceBundle rb = new MyResourcesA();
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,
103                                                   Logger.Level.WARNING,
104                                                   null, plainMsg, ex, (Object)null);
105             assertTrue(pass);
106             pass = (boolean)method.invoke(null, name, Logger.Level.INFO,
107                                           rb, MyResourcesA.VALUE, (Throwable)null,
108                                           (Object)null);
109             assertTrue(!pass);
110 
111             logger = getLogger(name, rb);
112             printLogger(logger);
113             assertTrue(logger.getClass().getName()
114                              .equals("jdk.internal.logger.LocalizedLoggerWrapper"));
115             assertTrue(logger.getName().equals(loggerClassName));
116             logger.log(Logger.Level.INFO, MyResourcesA.KEY);
117             pass = (boolean)method.invoke(null, name, Logger.Level.INFO,
118                                           rb, MyResourcesA.VALUE, (Throwable)null,
119                                           (Object)null);
120             assertTrue(pass);
121             pass = (boolean)method.invoke(null, name, Logger.Level.WARNING,
122                                           null, plainMsg, ex, (Object)null);
123             assertTrue(pass);
124         }
125     }
126 
127     private static class MyResourcesA extends ListResourceBundle {
128         static final String KEY = "this is the key in MyResourcesA";
129         static final String VALUE = "THIS IS THE VALUE IN MyResourcesA";
130 
131         @Override
getContents()132         protected Object[][] getContents() {
133             return new Object[][] {
134                 {KEY, VALUE}
135             };
136         }
137     }
138 
getLogger(String name)139     private static Logger getLogger(String name) {
140         return System.getLogger(name);
141     }
142 
getLogger(String name, ResourceBundle rb)143     private static Logger getLogger(String name, ResourceBundle rb) {
144         return System.getLogger(name, rb);
145     }
146 
printLogger(Logger logger)147     private static void printLogger(Logger logger) {
148         System.err.println("logger name: " + logger.getName()
149                            + ", logger class: " + logger.getClass());
150     }
151 
assertTrue(boolean b)152     private static void assertTrue(boolean b) {
153         if (!b) {
154             throw new RuntimeException("expected true, but get false.");
155         }
156     }
157 }
158