Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/logging/SourceClassName.java
38838 views
/*1* Copyright (c) 2010, 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* @compile -XDignore.symbol.file SourceClassName.java30* @run main/othervm SourceClassName31*/3233import java.util.logging.*;34import java.io.*;35import sun.util.logging.PlatformLogger;3637public class SourceClassName {38public static void main(String[] args) throws Exception {39File dir = new File(System.getProperty("user.dir", "."));40File log = new File(dir, "testlog.txt");41PrintStream logps = new PrintStream(log);42writeLogRecords(logps);43checkLogRecords(log);44}4546private static void writeLogRecords(PrintStream logps) throws Exception {47PrintStream err = System.err;48try {49System.setErr(logps);5051Object[] params = new Object[] { new Long(1), "string"};52PlatformLogger plog = PlatformLogger.getLogger("test.log.foo");53plog.severe("Log message {0} {1}", (Object[]) params);5455// create a java.util.logging.Logger56// now java.util.logging.Logger should be created for each platform logger57Logger logger = Logger.getLogger("test.log.bar");58logger.log(Level.SEVERE, "Log message {0} {1}", params);5960plog.severe("Log message {0} {1}", (Object[]) params);61} finally {62logps.flush();63logps.close();64System.setErr(err);65}66}6768private static void checkLogRecords(File log) throws Exception {69System.out.println("Checking log records in file: " + log);70FileInputStream in = new FileInputStream(log);71String EXPECTED_LOG = "SEVERE: Log message 1 string";72try {73BufferedReader reader = new BufferedReader(new InputStreamReader(in));74String line;75String[] record = new String[2];76int count = 0;77int i = 0;78while ((line = reader.readLine()) != null) {79line = line.trim();80System.out.println(line);81record[i++] = line;82if (i == 2) {83i = 0;84count++;85// check source class name and method86String[] ss = record[0].split("\\s+");87int len = ss.length;88if (!ss[len-2].equals("SourceClassName") ||89!ss[len-1].equals("writeLogRecords")) {90throw new RuntimeException("Unexpected source: " +91ss[len-2] + " " + ss[len-1]);92}9394// check log message95if (!record[1].equals(EXPECTED_LOG)) {96throw new RuntimeException("Unexpected log: " + record[1]);97}98}99}100if (count != 3) {101throw new RuntimeException("Unexpected number of records: " + count);102}103} finally {104in.close();105}106}107}108109110