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
81 String tstSrc = System.getProperty(TST_SRC_PROP);
82 File fname = new File(tstSrc, LM_PROP_FNAME);
83 String prop = fname.getCanonicalPath();
84 System.setProperty(CFG_FILE_PROP, prop);
85 logMgr.readConfiguration();
86
87 System.out.println();
88 if (checkLoggers() == PASSED) {
89 System.out.println(MSG_PASSED);
90 } else {
91 System.out.println(MSG_FAILED);
92 throw new Exception(MSG_FAILED);
93 }
94 }
95
96 public static List<String> getDefaultLoggerNames() {
97 List<String> expectedLoggerNames = new ArrayList<>();
98
99 // LogManager always creates two loggers:
100 expectedLoggerNames.add(""); // root logger: ""
101 expectedLoggerNames.add("global"); // global logger: "global"
102 return expectedLoggerNames;
103 }
104
105 /* Check: getLoggerNames() must return correct names
106 * for registered loggers and their parents.
107 * Returns boolean values: PASSED or FAILED
108 */
109 public static boolean checkLoggers() {
110 String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers";
111 List<String> expectedLoggerNames = new ArrayList<>(getDefaultLoggerNames());
112
113 // Create the logger LOGGER_NAME_1
114 createdLoggers.add(Logger.getLogger(LOGGER_NAME_1));
115 expectedLoggerNames.add(PARENT_NAME_1);
116 expectedLoggerNames.add(LOGGER_NAME_1);
117
118 // Create the logger LOGGER_NAME_2
119 createdLoggers.add(Logger.getLogger(LOGGER_NAME_2));
120 expectedLoggerNames.add(PARENT_NAME_2);
121 expectedLoggerNames.add(LOGGER_NAME_2);
122
123
124 Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames();
125 List<String> returnedLoggerNames = new ArrayList<>(0);
126 while (returnedLoggersEnum.hasMoreElements()) {
127 String logger = returnedLoggersEnum.nextElement();
128 if (!initialLoggerNames.contains(logger)) {
129 // filter out the loggers that have been added before this test runs
130 returnedLoggerNames.add(logger);
131 }
132
133 }
134 System.out.println(returnedLoggerNames);
135 return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg);
136 }
137
138 // Returns boolean values: PASSED or FAILED
139 private static boolean checkNames(List<String> expNames,
140 List<String> retNames,
141 String failMsg) {
142 boolean status = PASSED;
143
144 if (expNames.size() != retNames.size()) {
145 status = FAILED;
146 } else if (!new HashSet<>(expNames).equals(new HashSet<>(retNames))) {
147 status = FAILED;
148 }
149 if (!status) {
150 printFailMsg(expNames, retNames, failMsg);
151 }
152 return status;
153 }
154
155 private static void printFailMsg(List<String> expNames,
156 List<String> retNames,
157 String failMsg) {
158 out.println();
159 out.println(failMsg);
160 if (expNames.isEmpty()) {
161 out.println("# there are NO expected logger names");
162 } else {
163 out.println("# expected logger names (" + expNames.size() + "):");
164 for (int i = 0; i < expNames.size(); i++) {
165 out.println(" expNames[" + i + "] = " + expNames.get(i));
166 }
167 }
168 if (retNames.isEmpty()) {
169 out.println("# there are NO returned logger names");
170 } else {
171 out.println("# returned logger names (" + retNames.size() + "):");
172 for (int i = 0; i < retNames.size(); i++) {
173 out.println(" retNames[" + i + "] = " + retNames.get(i));
174 }
175 }
176 }
177 }