Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/logging/SourceClassName.java
38838 views
1
/*
2
* Copyright (c) 2010, 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 6985460
27
* @summary Test the source class name and method output by the platform
28
* logger.
29
*
30
* @compile -XDignore.symbol.file SourceClassName.java
31
* @run main/othervm SourceClassName
32
*/
33
34
import java.util.logging.*;
35
import java.io.*;
36
import sun.util.logging.PlatformLogger;
37
38
public class SourceClassName {
39
public static void main(String[] args) throws Exception {
40
File dir = new File(System.getProperty("user.dir", "."));
41
File log = new File(dir, "testlog.txt");
42
PrintStream logps = new PrintStream(log);
43
writeLogRecords(logps);
44
checkLogRecords(log);
45
}
46
47
private static void writeLogRecords(PrintStream logps) throws Exception {
48
PrintStream err = System.err;
49
try {
50
System.setErr(logps);
51
52
Object[] params = new Object[] { new Long(1), "string"};
53
PlatformLogger plog = PlatformLogger.getLogger("test.log.foo");
54
plog.severe("Log message {0} {1}", (Object[]) params);
55
56
// create a java.util.logging.Logger
57
// now java.util.logging.Logger should be created for each platform logger
58
Logger logger = Logger.getLogger("test.log.bar");
59
logger.log(Level.SEVERE, "Log message {0} {1}", params);
60
61
plog.severe("Log message {0} {1}", (Object[]) params);
62
} finally {
63
logps.flush();
64
logps.close();
65
System.setErr(err);
66
}
67
}
68
69
private static void checkLogRecords(File log) throws Exception {
70
System.out.println("Checking log records in file: " + log);
71
FileInputStream in = new FileInputStream(log);
72
String EXPECTED_LOG = "SEVERE: Log message 1 string";
73
try {
74
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
75
String line;
76
String[] record = new String[2];
77
int count = 0;
78
int i = 0;
79
while ((line = reader.readLine()) != null) {
80
line = line.trim();
81
System.out.println(line);
82
record[i++] = line;
83
if (i == 2) {
84
i = 0;
85
count++;
86
// check source class name and method
87
String[] ss = record[0].split("\\s+");
88
int len = ss.length;
89
if (!ss[len-2].equals("SourceClassName") ||
90
!ss[len-1].equals("writeLogRecords")) {
91
throw new RuntimeException("Unexpected source: " +
92
ss[len-2] + " " + ss[len-1]);
93
}
94
95
// check log message
96
if (!record[1].equals(EXPECTED_LOG)) {
97
throw new RuntimeException("Unexpected log: " + record[1]);
98
}
99
}
100
}
101
if (count != 3) {
102
throw new RuntimeException("Unexpected number of records: " + count);
103
}
104
} finally {
105
in.close();
106
}
107
}
108
}
109
110