1 /*
2  * Copyright (c) 2018, 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 /*
25  * @test
26  * @bug 8211093
27  * @summary Check default configuration
28  * @build DefaultConfigTest
29  * @run main/othervm DefaultConfigTest
30  */
31 
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.logging.ConsoleHandler;
36 import java.util.logging.Level;
37 import java.util.logging.LogManager;
38 import java.util.logging.Logger;
39 import java.util.stream.Collectors;
40 
41 public class DefaultConfigTest {
42     static final java.io.PrintStream out = System.out;
main(String arg[])43     public static void main(String arg[]) {
44         LogManager manager = LogManager.getLogManager();
45 
46         if (!Set.of(Collections.list(manager.getLoggerNames()).toArray())
47                 .equals(Set.of("", "global"))) {
48             throw new RuntimeException("Unexpected logger name in: "
49                     + Collections.list(manager.getLoggerNames()));
50         }
51         Logger global = Logger.getGlobal();
52         Logger root = Logger.getLogger("");
53         Logger foo  = Logger.getLogger("com.xyz.foo");
54         if (!Set.of(Collections.list(manager.getLoggerNames()).toArray())
55                 .equals(Set.of("", "global", "com.xyz.foo"))) {
56             throw new RuntimeException("Unexpected logger name in: "
57                     + Collections.list(manager.getLoggerNames()));
58         }
59         for (Logger l : List.of(global, foo)) {
60             if (l.getLevel() != null) {
61                 throw new RuntimeException("Unexpected level "
62                         + l.getLevel() + " for " + l.getName());
63             }
64             if (l.getHandlers().length != 0) {
65                 throw new RuntimeException("Unexpected handlers "
66                         + List.of(l.getHandlers()) + " for " + l.getName());
67             }
68             for (Level level : List.of(
69                     Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG)) {
70                 if (l.isLoggable(level)) {
71                     throw new RuntimeException("Level "
72                             + level + " should not be loggable for " + l.getName());
73                 }
74             }
75             for (Level level : List.of(
76                     Level.INFO, Level.WARNING, Level.SEVERE)) {
77                 if (!l.isLoggable(level)) {
78                     throw new RuntimeException("Level "
79                             + level + " should be loggable for " + l.getName());
80                 }
81             }
82         }
83         if (root.getLevel() != Level.INFO) {
84             throw new RuntimeException("Unexpected level "
85                     + root.getLevel() + " for root logger");
86         }
87         if (root.getHandlers().length != 1) {
88             throw new RuntimeException("Unexpected handlers "
89                     + List.of(root.getHandlers()) + " for root logger");
90         }
91         if (root.getHandlers()[0].getClass() != ConsoleHandler.class) {
92             throw new RuntimeException("Unexpected handlers "
93                     + List.of(root.getHandlers()) + " for root logger");
94         }
95         if (root.getHandlers()[0].getLevel() != Level.INFO) {
96             throw new RuntimeException("Unexpected level "
97                     + root.getHandlers()[0].getLevel() + " for handler "
98                     + root.getHandlers()[0]);
99         }
100         out.println(List.of(root, global, foo).stream()
101                 .map(Logger::getName)
102                 .map(s -> String.format("\"%s\"", s))
103                 .collect(Collectors.joining(", ",
104                         "Default configuration checked for ",
105                         ".")));
106     }
107 }
108