author | jlahoda |
Wed, 12 Dec 2012 20:26:56 +0100 | |
changeset 14803 | 88347e495d34 |
parent 10607 | 1ba66093449c |
child 14683 | 38921a46c62d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* @test |
|
3 |
* @bug 6498300 |
|
4 |
* |
|
5 |
* @summary regression: parent loggers are not properly registered |
|
6 |
* @author ss45998 |
|
7 |
* |
|
8 |
* @build ParentLoggersTest |
|
10607
1ba66093449c
7092627: use agentvm mode instead of samevm in regtests
weijun
parents:
5803
diff
changeset
|
9 |
* @run main/othervm ParentLoggersTest |
2 | 10 |
*/ |
11 |
||
12 |
/* |
|
13 |
* There are several original tests which were failed when |
|
14 |
* this regression was introduced. This is an extra test |
|
15 |
* to ensure that the parent loggers with the defined |
|
16 |
* .level property are implicitly registered. |
|
17 |
*/ |
|
18 |
||
19 |
import java.util.*; |
|
20 |
import java.io.*; |
|
21 |
import java.util.logging.*; |
|
22 |
||
23 |
public class ParentLoggersTest { |
|
24 |
static final LogManager logMgr = LogManager.getLogManager(); |
|
25 |
static final PrintStream out = System.out; |
|
26 |
||
27 |
static final boolean PASSED = true; |
|
28 |
static final boolean FAILED = false; |
|
29 |
static final String MSG_PASSED = "ParentLoggersTest: passed"; |
|
30 |
static final String MSG_FAILED = "ParentLoggersTest: failed"; |
|
31 |
||
32 |
/* Properties */ |
|
33 |
static final String TST_SRC_PROP = "test.src"; |
|
34 |
static final String CFG_FILE_PROP = "java.util.logging.config.file"; |
|
35 |
static final String LM_PROP_FNAME = "ParentLoggersTest.props"; |
|
36 |
||
37 |
/* Logger names */ |
|
38 |
static final String PARENT_NAME_1 = "myParentLogger"; |
|
39 |
static final String PARENT_NAME_2 = "abc.xyz.foo"; |
|
40 |
static final String LOGGER_NAME_1 = PARENT_NAME_1 + ".myLogger"; |
|
41 |
static final String LOGGER_NAME_2 = PARENT_NAME_2 + ".myBar.myLogger"; |
|
42 |
||
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
43 |
static final List<String> initialLoggerNames = new ArrayList<String>(); |
2 | 44 |
public static void main(String args[]) throws Exception { |
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
45 |
// cache the initial set of loggers before this test begins |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
46 |
// to add any loggers |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
47 |
Enumeration<String> e = logMgr.getLoggerNames(); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
48 |
List<String> defaultLoggers = getDefaultLoggerNames(); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
49 |
while (e.hasMoreElements()) { |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
50 |
String logger = e.nextElement(); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
51 |
if (!defaultLoggers.contains(logger)) { |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
52 |
initialLoggerNames.add(logger); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
53 |
} |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
54 |
}; |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
55 |
|
2 | 56 |
String tstSrc = System.getProperty(TST_SRC_PROP); |
57 |
File fname = new File(tstSrc, LM_PROP_FNAME); |
|
58 |
String prop = fname.getCanonicalPath(); |
|
59 |
System.setProperty(CFG_FILE_PROP, prop); |
|
60 |
logMgr.readConfiguration(); |
|
61 |
||
62 |
System.out.println(); |
|
63 |
if (checkLoggers() == PASSED) { |
|
64 |
System.out.println(MSG_PASSED); |
|
65 |
} else { |
|
66 |
System.out.println(MSG_FAILED); |
|
67 |
throw new Exception(MSG_FAILED); |
|
68 |
} |
|
69 |
} |
|
70 |
||
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
71 |
public static List<String> getDefaultLoggerNames() { |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
72 |
List<String> expectedLoggerNames = new ArrayList<String>(); |
2 | 73 |
|
74 |
// LogManager always creates two loggers: |
|
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
75 |
expectedLoggerNames.add(""); // root logger: "" |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
76 |
expectedLoggerNames.add("global"); // global logger: "global" |
2 | 77 |
return expectedLoggerNames; |
78 |
} |
|
79 |
||
80 |
/* Check: getLoggerNames() must return correct names |
|
81 |
* for registered loggers and their parents. |
|
82 |
* Returns boolean values: PASSED or FAILED |
|
83 |
*/ |
|
84 |
public static boolean checkLoggers() { |
|
85 |
String failMsg = "# checkLoggers: getLoggerNames() returned unexpected loggers"; |
|
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
86 |
Vector<String> expectedLoggerNames = new Vector<String>(getDefaultLoggerNames()); |
2 | 87 |
|
88 |
// Create the logger LOGGER_NAME_1 |
|
89 |
Logger logger1 = Logger.getLogger(LOGGER_NAME_1); |
|
90 |
expectedLoggerNames.addElement(PARENT_NAME_1); |
|
91 |
expectedLoggerNames.addElement(LOGGER_NAME_1); |
|
92 |
||
93 |
// Create the logger LOGGER_NAME_2 |
|
94 |
Logger logger2 = Logger.getLogger(LOGGER_NAME_2); |
|
95 |
expectedLoggerNames.addElement(PARENT_NAME_2); |
|
96 |
expectedLoggerNames.addElement(LOGGER_NAME_2); |
|
97 |
||
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
98 |
Enumeration<String> returnedLoggersEnum = logMgr.getLoggerNames(); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
99 |
Vector<String> returnedLoggerNames = new Vector<String>(0); |
2 | 100 |
while (returnedLoggersEnum.hasMoreElements()) { |
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
101 |
String logger = returnedLoggersEnum.nextElement(); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
102 |
if (!initialLoggerNames.contains(logger)) { |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
103 |
// filter out the loggers that have been added before this test runs |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
104 |
returnedLoggerNames.addElement(logger); |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
105 |
} |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
106 |
|
2 | 107 |
}; |
108 |
||
109 |
return checkNames(expectedLoggerNames, returnedLoggerNames, failMsg); |
|
110 |
} |
|
111 |
||
112 |
// Returns boolean values: PASSED or FAILED |
|
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
113 |
private static boolean checkNames(Vector<String> expNames, |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
114 |
Vector<String> retNames, |
2 | 115 |
String failMsg) { |
116 |
boolean status = PASSED; |
|
117 |
||
118 |
if (expNames.size() != retNames.size()) { |
|
119 |
status = FAILED; |
|
120 |
} else { |
|
121 |
boolean checked[] = new boolean[retNames.size()]; |
|
122 |
for (int i = 0; i < expNames.size(); i++) { |
|
123 |
int j = 0; |
|
124 |
for (; j < retNames.size(); j++) { |
|
125 |
if (!checked[j] && |
|
126 |
expNames.elementAt(i).equals(retNames.elementAt(j))) { |
|
127 |
checked[j] = true; |
|
128 |
break; |
|
129 |
} |
|
130 |
} |
|
131 |
if (j >= retNames.size()) { |
|
132 |
status = FAILED; |
|
133 |
break; |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
if (!status) { |
|
138 |
printFailMsg(expNames, retNames, failMsg); |
|
139 |
} |
|
140 |
return status; |
|
141 |
} |
|
142 |
||
5803
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
143 |
private static void printFailMsg(Vector<String> expNames, |
c3ddaebe216b
6961408: test/java/util/logging/ParentLoggersTest.java fails in samevm mode
mchung
parents:
2
diff
changeset
|
144 |
Vector<String> retNames, |
2 | 145 |
String failMsg) { |
146 |
out.println(); |
|
147 |
out.println(failMsg); |
|
148 |
if (expNames.size() == 0) { |
|
149 |
out.println("# there are NO expected logger names"); |
|
150 |
} else { |
|
151 |
out.println("# expected logger names (" + expNames.size() + "):"); |
|
152 |
for (int i = 0; i < expNames.size(); i++) { |
|
153 |
out.println(" expNames[" + i + "] = " + expNames.elementAt(i)); |
|
154 |
} |
|
155 |
} |
|
156 |
if (retNames.size() == 0) { |
|
157 |
out.println("# there are NO returned logger names"); |
|
158 |
} else { |
|
159 |
out.println("# returned logger names (" + retNames.size() + "):"); |
|
160 |
for (int i = 0; i < retNames.size(); i++) { |
|
161 |
out.println(" retNames[" + i + "] = " + retNames.elementAt(i)); |
|
162 |
} |
|
163 |
} |
|
164 |
} |
|
165 |
} |