Path: blob/master/test/jdk/sun/util/logging/SourceClassName.java
41149 views
/*1* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 698546026* @summary Test the source class name and method output by the platform27* logger.28*29* @modules java.base/sun.util.logging30* java.logging31* @compile -XDignore.symbol.file SourceClassName.java32* @run main/othervm SourceClassName33*/3435import java.util.logging.*;36import java.io.*;37import sun.util.logging.PlatformLogger;3839public class SourceClassName {40public static void main(String[] args) throws Exception {41File dir = new File(System.getProperty("user.dir", "."));42File log = new File(dir, "testlog.txt");43PrintStream logps = new PrintStream(log);44writeLogRecords(logps);45checkLogRecords(log);46}4748private static void writeLogRecords(PrintStream logps) throws Exception {49PrintStream err = System.err;50try {51System.setErr(logps);5253Object[] params = new Object[] { new Long(1), "string"};54PlatformLogger plog = PlatformLogger.getLogger("test.log.foo");55plog.severe("Log message {0} {1}", (Object[]) params);5657// create a java.util.logging.Logger58// now java.util.logging.Logger should be created for each platform59// logger60Logger logger = Logger.getLogger("test.log.bar");61logger.log(Level.SEVERE, "Log message {0} {1}", params);6263plog.severe("Log message {0} {1}", (Object[]) params);64} finally {65logps.flush();66logps.close();67System.setErr(err);68}69}7071private static void checkLogRecords(File log) throws Exception {72System.out.println("Checking log records in file: " + log);73FileInputStream in = new FileInputStream(log);74String EXPECTED_LOG = "SEVERE: Log message 1 string";75try {76BufferedReader reader = new BufferedReader(new InputStreamReader(in));77String line;78String[] record = new String[2];79int count = 0;80int i = 0;81while ((line = reader.readLine()) != null) {82line = line.trim();83System.out.println(line);84record[i++] = line;85if (i == 2) {86i = 0;87// check log message88if (!record[1].equals(EXPECTED_LOG)) {89// it can sometime happen that some static initializer90// in the system will log an error message - due to e.g.91// some kind of misconfiguration or system settings.92// For instance - somethink like:93// INFO: currency.properties entry for FR ignored94// because the value format is not recognized.95// instead of failing if we get such an unexpected96// message, we will simply print that out.97System.out.println("*** WARNING: Unexpected log: " + record[1]);98continue;99}100count++;101// check source class name and method102String[] ss = record[0].split("\\s+");103int len = ss.length;104if (!ss[len-2].equals("SourceClassName") ||105!ss[len-1].equals("writeLogRecords")) {106throw new RuntimeException("Unexpected source: " +107ss[len-2] + " " + ss[len-1]);108}109110}111}112if (count != 3) {113throw new RuntimeException("Unexpected number of records: " + count);114}115} finally {116in.close();117}118}119}120121122