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.time.Instant;
29 import java.util.ResourceBundle;
30 import java.util.ListResourceBundle;
31 
32 /*
33  * Tests when logger client is in named module m.l.a
34  */
35 public class TestA {
36 
main(String[] args)37     public static void main(String[] args) throws Exception {
38         assertTrue(args.length == 2);
39         String loggerMode = args[0];
40         String loggerClassName = args[1];
41         System.err.println("TestA starting at " + Instant.now());
42         try {
43             testLogger(loggerMode, loggerClassName);
44             testLog(loggerClassName);
45         } finally {
46             System.err.println("TestA finished at " + Instant.now());
47         }
48     }
49 
50     /*
51      * Tests System.getLogger(String) get expected logger.
52      */
testLogger(String loggerMode, String loggerClassName)53     private static void testLogger(String loggerMode, String loggerClassName) {
54         final Module m = TestA.class.getModule();
55         final ClassLoader moduleCL = m.getClassLoader();
56         assertTrue(m.isNamed());
57         assertTrue(moduleCL != null);
58 
59         String name = "test.a";
60         Logger logger = getLogger(name);
61         printLogger(logger);
62 
63         final Module lm = logger.getClass().getModule();
64         final ClassLoader loggerCL = lm.getClassLoader();
65         if (loggerMode.equals("system")) {
66             assertTrue(lm.isNamed());
67             assertTrue(loggerCL == null);
68         } else if(loggerMode.equals("named")) {
69             assertTrue(lm.isNamed());
70             assertTrue(loggerCL != null);
71         } else if(loggerMode.equals("unnamed")) {
72             assertTrue(!lm.isNamed());
73             assertTrue(loggerCL != null);
74         } else {
75             throw new RuntimeException("wrong parameter");
76         }
77 
78         assertTrue(loggerClassName.equals(logger.getClass().getName()));
79         assertTrue(!loggerClassName.equals("jdk.internal.logger.LazyLoggers$JdkLazyLogger"));
80     }
81 
82     /*
83      * Tests Logger retrieved by System.getLogger(String, ResourceBundle) and
84      * System.getLogger(String) works well.
85      */
testLog(String loggerClassName)86     private static void testLog(String loggerClassName) throws Exception {
87         if (loggerClassName.equals("pkg.a.l.LoggerA")
88                 || loggerClassName.equals("pkg.b.l.LoggerB")) {
89 
90             String name = "test.a.A";
91             String plainMsg = "this is test log message #1";
92             ResourceBundle rb = new MyResourcesA();
93             Throwable ex = new Throwable("this is an expected exception to be logged");
94             Class<?> clazz = Class.forName(loggerClassName);
95             Method method = clazz.getMethod("checkLog", String.class,
96                                             System.Logger.Level.class,
97                                             ResourceBundle.class, String.class,
98                                             Throwable.class, Object[].class);
99 
100             Logger logger = getLogger(name);
101             printLogger(logger);
102             assertTrue(logger.getClass().getName().equals(loggerClassName));
103             assertTrue(logger.getName().equals(loggerClassName));
104             logger.log(Logger.Level.WARNING, plainMsg);
105             boolean pass = (boolean)method.invoke(null, name,
106                                                   Logger.Level.WARNING,
107                                                   null, plainMsg, ex, (Object)null);
108             assertTrue(pass);
109             pass = (boolean)method.invoke(null, name, Logger.Level.INFO,
110                                           rb, MyResourcesA.VALUE, (Throwable)null,
111                                           (Object)null);
112             assertTrue(!pass);
113 
114             logger = getLogger(name, rb);
115             printLogger(logger);
116             assertTrue(logger.getClass().getName()
117                              .equals("jdk.internal.logger.LocalizedLoggerWrapper"));
118             assertTrue(logger.getName().equals(loggerClassName));
119             logger.log(Logger.Level.INFO, MyResourcesA.KEY);
120             pass = (boolean)method.invoke(null, name, Logger.Level.INFO,
121                                           rb, MyResourcesA.VALUE, (Throwable)null,
122                                           (Object)null);
123             assertTrue(pass);
124             pass = (boolean)method.invoke(null, name, Logger.Level.WARNING,
125                                           null, plainMsg, ex, (Object)null);
126             assertTrue(pass);
127         }
128     }
129 
130     private static class MyResourcesA extends ListResourceBundle {
131         static final String KEY = "this is the key in MyResourcesA";
132         static final String VALUE = "THIS IS THE VALUE IN MyResourcesA";
133 
134         @Override
getContents()135         protected Object[][] getContents() {
136             return new Object[][] {
137                 {KEY, VALUE}
138             };
139         }
140     }
141 
getLogger(String name)142     private static Logger getLogger(String name) {
143         return System.getLogger(name);
144     }
145 
getLogger(String name, ResourceBundle rb)146     private static Logger getLogger(String name, ResourceBundle rb) {
147         return System.getLogger(name, rb);
148     }
149 
printLogger(Logger logger)150     private static void printLogger(Logger logger) {
151         System.err.println("logger name: " + logger.getName()
152                            + ", logger class: " + logger.getClass());
153     }
154 
assertTrue(boolean b)155     private static void assertTrue(boolean b) {
156         if (!b) {
157             throw new RuntimeException("expected true, but get false.");
158         }
159     }
160 }
161