3888N/A/*
3888N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3888N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3888N/A *
3888N/A * This code is free software; you can redistribute it and/or modify it
3888N/A * under the terms of the GNU General Public License version 2 only, as
3888N/A * published by the Free Software Foundation.
3888N/A *
3888N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3888N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3888N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3888N/A * version 2 for more details (a copy is included in the LICENSE file that
3888N/A * accompanied this code).
3888N/A *
3888N/A * You should have received a copy of the GNU General Public License version
3888N/A * 2 along with this work; if not, write to the Free Software Foundation,
3888N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3888N/A *
3888N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3888N/A * or visit www.oracle.com if you need additional information or have any
3888N/A * questions.
3888N/A */
3888N/A
3888N/A/*
3888N/A * @test
3888N/A * @bug 6381464
3888N/A * @summary Test the custom simple formatter output
3888N/A *
3888N/A * @run main/othervm SimpleFormatterFormat
3888N/A */
3888N/A
3888N/Aimport java.io.*;
3888N/Aimport java.util.logging.*;
3888N/Aimport java.util.regex.*;
3888N/A
3888N/Apublic class SimpleFormatterFormat {
3888N/A private static final String key = "java.util.logging.SimpleFormatter.format";
3888N/A private static final String origFormat = System.getProperty(key);
3888N/A private static final PrintStream err = System.err;
3888N/A public static void main(String[] args) throws Exception {
3888N/A try {
3888N/A File dir = new File(System.getProperty("user.dir", "."));
3888N/A File log = new File(dir, "simpleformat.txt");
3888N/A java.nio.file.Files.deleteIfExists(log.toPath());
3888N/A PrintStream logps = new PrintStream(log);
3888N/A System.setProperty(key, "%3$s:%4$s: %5$s [%1$tc] source: %2$s%6$s%n");
3888N/A writeLogRecords(logps);
3888N/A checkLogRecords(log);
3888N/A } finally {
3888N/A if (origFormat == null) {
3888N/A System.clearProperty(key);
3888N/A } else {
3888N/A System.setProperty(key, origFormat);
3888N/A }
3888N/A System.setErr(err);
3888N/A }
3888N/A }
3888N/A
3888N/A private static String[] loggers = new String[] {
3888N/A "test.foo",
3888N/A "test.foo",
3888N/A "test.bar",
3888N/A "test.bar"
3888N/A };
3888N/A private static String[] messages = new String[] {
3888N/A "severe hello world",
3888N/A "warning lost connection",
3888N/A "info welcome",
3888N/A "warning exception thrown",
3888N/A };
3888N/A private static void writeLogRecords(PrintStream logps) throws Exception {
3888N/A try {
3888N/A System.setErr(logps);
3888N/A
3888N/A Logger foo = Logger.getLogger("test.foo");
3888N/A foo.log(Level.SEVERE, "{0} {1} {2}", new Object[] {"severe", "hello", "world"});
3888N/A foo.warning(messages[1]);
3888N/A
3888N/A Logger bar = Logger.getLogger("test.bar");
3888N/A bar.finest("Dummy message");
3888N/A bar.info(messages[2]);
3888N/A bar.log(Level.WARNING, messages[3], new IllegalArgumentException());
3888N/A
3888N/A } finally {
3888N/A logps.flush();
3888N/A logps.close();
3888N/A System.setErr(err);
3888N/A }
3888N/A }
3888N/A
3888N/A private static void checkLogRecords(File log) throws Exception {
3888N/A System.out.println("Checking log records in file: " + log);
3888N/A Pattern p = Pattern.compile("([\\.a-zA-Z:]+) (.*) \\[.*\\] source: (.*)");
3888N/A
3888N/A try (FileInputStream in = new FileInputStream(log)) {
3888N/A BufferedReader reader = new BufferedReader(new InputStreamReader(in));
3888N/A String line;
3888N/A int i = 0;
3888N/A while (i < messages.length &&
3888N/A (line = reader.readLine()) != null) {
3888N/A String expectedLogger = loggers[i];
3888N/A String expectedMsg = messages[i];
3888N/A i++;
3888N/A
3888N/A line = line.trim();
3888N/A System.out.println(line);
3888N/A
3888N/A Matcher m = p.matcher(line);
3888N/A if (!m.matches()) {
3888N/A throw new RuntimeException("Unexpected output format");
3888N/A }
3888N/A if (m.groupCount() != 3) {
3888N/A throw new RuntimeException("Unexpected group count = " +
3888N/A m.groupCount());
3888N/A }
3888N/A // verify logger name and level
3888N/A String[] ss = m.group(1).split(":");
3888N/A int len = ss.length;
3888N/A if (len != 2) {
3888N/A throw new RuntimeException("Unexpected logger name and level" +
3888N/A m.group(1));
3888N/A }
3888N/A
3888N/A verify(expectedLogger, expectedMsg, ss[0], ss[1], m.group(2), m.group(3));
3888N/A }
3888N/A
3888N/A // expect IllegalArgumentException following it
3888N/A line = reader.readLine().trim();
3888N/A if (!line.equals("java.lang.IllegalArgumentException")) {
3888N/A throw new RuntimeException("Invalid line: " + line);
3888N/A }
3888N/A }
3888N/A }
3888N/A
3888N/A private static void verify(String expectedLogger, String expectedMsg,
3888N/A String logger, String level,
3888N/A String msg, String source) {
3888N/A if (!logger.equals(expectedLogger)) {
3888N/A throw new RuntimeException("Unexpected logger: " + logger);
3888N/A }
3888N/A if (!msg.equals(expectedMsg)) {
3888N/A throw new RuntimeException("Unexpected message: " + msg);
3888N/A }
3888N/A
3888N/A String[] ss = expectedMsg.split("\\s+");
3888N/A String expectedLevel = ss[0].toUpperCase();
3888N/A if (!level.equals(expectedLevel)) {
3888N/A throw new RuntimeException("Unexpected level: " + level);
3888N/A }
3888N/A
3888N/A ss = source.split("\\s+");
3888N/A int len = ss.length;
3888N/A if (!(len == 2 &&
3888N/A ss[0].equals("SimpleFormatterFormat") &&
3888N/A ss[1].equals("writeLogRecords"))) {
3888N/A throw new RuntimeException("Unexpected source: " + source);
3888N/A }
3888N/A }
3888N/A}