1 /*
2 * Copyright (c) 2012, 2013, 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 6498300
27 *
28 * @summary regression: parent loggers are not properly registered
29 * @author ss45998
30 *
31 * @build ParentLoggersTest
32 * @run main/othervm ParentLoggersTest
33 */
34
35 /*
36 * There are several original tests which were failed when
37 * this regression was introduced. This is an extra test
38 * to ensure that the parent loggers with the defined
39 * .level property are implicitly registered.
40 */
41
42 import java.util.*;
43 import java.io.*;
44 import java.util.logging.*;
45
46 public class ParentLoggersTest {
47 static final LogManager logMgr = LogManager.getLogManager();
48 static final PrintStream out = System.out;
49
50 static final boolean PASSED = true;
51 static final boolean FAILED = false;
52 static final String MSG_PASSED = "ParentLoggersTest: passed";
53 static final String MSG_FAILED = "ParentLoggersTest: failed";
54
55 /* Properties */
56 static final String TST_SRC_PROP = "test.src";
57 static final String CFG_FILE_PROP = "java.util.logging.config.file";
58 static final String LM_PROP_FNAME = "ParentLoggersTest.props";
59
60 /* Logger names */
61 static final String PARENT_NAME_1 = "myParentLogger";
62 static final String PARENT_NAME_2 = "abc.xyz.foo";
63 static final String LOGGER_NAME_1 = PARENT_NAME_1 + ".myLogger";
64 static final String LOGGER_NAME_2 = PARENT_NAME_2 + ".myBar.myLogger";
65
66 static final List<String> initialLoggerNames = new ArrayList<>();
67 static final List<Logger> createdLoggers = new ArrayList<>();
68
69 public static void main(String args[]) throws Exception {
70 // cache the initial set of loggers before this test begins
71 // to add any loggers
72 Enumeration<String> e = logMgr.getLoggerNames();
73 List<String> defaultLoggers = getDefaultLoggerNames();
74 while (e.hasMoreElements()) {
75 String logger = e.nextElement();
76 if (!defaultLoggers.contains(logger)) {
77 initialLoggerNames.add(logger);
78 }
79 }
80 System.out.println("# default loggers are: " + defaultLoggers);
81 System.out.println("# initial loggers are: " + initialLoggerNames);
82
83 String tstSrc = System.getProperty(TST_SRC_PROP);
84 File fname = new File(tstSrc, LM_PROP_FNAME);
85 String prop = fname.getCanonicalPath();
86 System.setProperty(CFG_FILE_PROP, prop);
87 logMgr.readConfiguration();
88
89 System.out.println();
90 if (checkLoggers() == PASSED) {
91 System.out.println(MSG_PASSED);
92 } else {
93 System.out.println(MSG_FAILED);
94 throw new Exception(MSG_FAILED);
95 }
96 }
97
98 public static List<String> getDefaultLoggerNames() {
99 List<String> expectedLoggerNames = new ArrayList<>();
100
101 // LogManager always creates two loggers:
102 expectedLoggerNames.add(""); // root logger: ""
103 expectedLoggerNames.add("global"); // global logger: "global"
104 return expectedLoggerNames;
105 }
106
107 /* Check: getLoggerNames() must return correct names
108 * for registered loggers and their parents.
109 * Returns boolean values: PASSED or FAILED
110 */
111 public static boolean checkLoggers() {
112 String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
113 List<String> expectedLoggerNames = new ArrayList<>(getDefaultLoggerNames());
114
115 // Create the logger LOGGER_NAME_1
116 createdLoggers.add(Logger.getLogger(LOGGER_NAME_1));
117 expectedLoggerNames.add(PARENT_NAME_1);
118 expectedLoggerNames.add(LOGGER_NAME_1);
119
120 // Create the logger LOGGER_NAME_2
121 createdLoggers.add(Logger.getLogger(LOGGER_NAME_2));
122 expectedLoggerNames.add(PARENT_NAME_2);
123 expectedLoggerNames.add(LOGGER_NAME_2);
124
125
126 Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
127 List<String> returnedLoggerNames = new ArrayList<>(0);
128 while (returnedLoggersEnum.hasMoreElements()) {
129 String logger = returnedLoggersEnum.nextElement();
130 if (!initialLoggerNames.contains(logger)) {
131 // filter out the loggers that have been added before this test runs
132 returnedLoggerNames.add(logger);
133 }
134
135 }
136 System.out.println("# Created loggers are: " +
137 createdLoggers.stream().map(Logger::getName).toList());
138 System.out.println("# Expected loggers are: " + expectedLoggerNames);
139 System.out.println("# Returned loggers are: " + returnedLoggerNames);
140 return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
141 }
142
143 // Returns boolean values: PASSED or FAILED
144 private static boolean checkNames(List<String> expNames,
145 List<String> retNames,
146 String failMsg) {
147 boolean status = PASSED;
148
149 if (expNames.size() != retNames.size()) {
150 status = FAILED;
151 } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) {
152 status = FAILED;
153 }
154 if (!status) {
155 printFailMsg(expNames, retNames, failMsg);
156 }
157 return status;
158 }
159
160 private static void printFailMsg(List<String> expNames,
161 List<String> retNames,
162 String failMsg) {
163 out.println();
164 out.println(failMsg);
165 if (expNames.isEmpty()) {
166 out.println("# there are NO expected logger names");
167 } else {
168 out.println("# expected logger names (" + expNames.size() + "):");
169 for (int i = 0; i < expNames.size(); i++) {
170 out.println(" expNames[" + i + "] = " + expNames.get(i));
171 }
172 }
173 if (retNames.isEmpty()) {
174 out.println("# there are NO returned logger names");
175 } else {
176 out.println("# returned logger names (" + retNames.size() + "):");
177 for (int i = 0; i < retNames.size(); i++) {
178 out.println(" retNames[" + i + "] = " + retNames.get(i));
179 }
180 }
181 }
182 }