jdk/test/java/util/logging/SimpleFormatterFormat.java
author jlahoda
Wed, 12 Dec 2012 20:26:56 +0100
changeset 14803 88347e495d34
parent 9014 117374ccf1b7
child 14683 38921a46c62d
permissions -rw-r--r--
8004504: ListBuffer could reuse List.nil() as the sentinel element Summary: ListBuffer.last now points to the last elements with client data, or null if none. Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     1
/*
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     4
 *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     8
 *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    13
 * accompanied this code).
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    14
 *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    18
 *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    21
 * questions.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    22
 */
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    23
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    24
/*
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    25
 * @test
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    26
 * @bug     6381464
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    27
 * @summary Test the custom simple formatter output
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    28
 *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    29
 * @run main/othervm SimpleFormatterFormat
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    30
 */
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    31
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    32
import java.io.*;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    33
import java.util.logging.*;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    34
import java.util.regex.*;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    35
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    36
public class SimpleFormatterFormat {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    37
    private static final String key = "java.util.logging.SimpleFormatter.format";
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    38
    private static final String origFormat = System.getProperty(key);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    39
    private static final PrintStream err = System.err;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    41
        try {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    42
            File dir = new File(System.getProperty("user.dir", "."));
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    43
            File log = new File(dir, "simpleformat.txt");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    44
            java.nio.file.Files.deleteIfExists(log.toPath());
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    45
            PrintStream logps = new PrintStream(log);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    46
            System.setProperty(key, "%3$s:%4$s: %5$s [%1$tc] source: %2$s%6$s%n");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    47
            writeLogRecords(logps);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    48
            checkLogRecords(log);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    49
        } finally {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    50
            if (origFormat == null) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    51
                System.clearProperty(key);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    52
            } else {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    53
                System.setProperty(key, origFormat);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    54
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    55
            System.setErr(err);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    56
       }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    57
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    58
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    59
    private static String[] loggers = new String[] {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    60
        "test.foo",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    61
        "test.foo",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    62
        "test.bar",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    63
        "test.bar"
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    64
    };
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    65
    private static String[] messages = new String[] {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    66
        "severe hello world",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    67
        "warning lost connection",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    68
        "info welcome",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    69
        "warning exception thrown",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    70
    };
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    71
    private static void writeLogRecords(PrintStream logps) throws Exception {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    72
        try {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    73
            System.setErr(logps);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    74
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    75
            Logger foo = Logger.getLogger("test.foo");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    76
            foo.log(Level.SEVERE, "{0} {1} {2}", new Object[] {"severe", "hello", "world"});
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    77
            foo.warning(messages[1]);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    78
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    79
            Logger bar = Logger.getLogger("test.bar");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    80
            bar.finest("Dummy message");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    81
            bar.info(messages[2]);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    82
            bar.log(Level.WARNING, messages[3], new IllegalArgumentException());
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    83
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    84
        } finally {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    85
            logps.flush();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    86
            logps.close();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    87
            System.setErr(err);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    88
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    89
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    90
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    91
    private static void checkLogRecords(File log) throws Exception {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    92
        System.out.println("Checking log records in file: " + log);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    93
        Pattern p = Pattern.compile("([\\.a-zA-Z:]+) (.*) \\[.*\\] source: (.*)");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    94
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    95
        try (FileInputStream in = new FileInputStream(log)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    96
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    97
            String line;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    98
            int i = 0;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    99
            while (i < messages.length &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   100
                      (line = reader.readLine()) != null) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   101
                String expectedLogger = loggers[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   102
                String expectedMsg = messages[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   103
                i++;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   104
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   105
                line = line.trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   106
                System.out.println(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   107
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   108
                Matcher m = p.matcher(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   109
                if (!m.matches()) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   110
                    throw new RuntimeException("Unexpected output format");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   111
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   112
                if (m.groupCount() != 3) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   113
                    throw new RuntimeException("Unexpected group count = " +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   114
                        m.groupCount());
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   115
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   116
                // verify logger name and level
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   117
                String[] ss = m.group(1).split(":");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   118
                int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   119
                if (len != 2) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   120
                    throw new RuntimeException("Unexpected logger name and level" +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   121
                        m.group(1));
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   122
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   123
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   124
                verify(expectedLogger, expectedMsg, ss[0], ss[1], m.group(2), m.group(3));
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   125
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   126
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   127
            // expect IllegalArgumentException following it
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   128
            line = reader.readLine().trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   129
            if (!line.equals("java.lang.IllegalArgumentException")) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   130
                throw new RuntimeException("Invalid line: " + line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   131
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   132
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   133
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   134
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   135
    private static void verify(String expectedLogger, String expectedMsg,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   136
                               String logger, String level,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   137
                               String msg, String source) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   138
        if (!logger.equals(expectedLogger)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   139
            throw new RuntimeException("Unexpected logger: " + logger);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   140
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   141
        if (!msg.equals(expectedMsg)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   142
            throw new RuntimeException("Unexpected message: " + msg);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   143
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   144
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   145
        String[] ss = expectedMsg.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   146
        String expectedLevel = ss[0].toUpperCase();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   147
        if (!level.equals(expectedLevel)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   148
            throw new RuntimeException("Unexpected level: " + level);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   149
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   150
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   151
        ss = source.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   152
        int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   153
        if (!(len == 2 &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   154
              ss[0].equals("SimpleFormatterFormat") &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   155
              ss[1].equals("writeLogRecords"))) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   156
            throw new RuntimeException("Unexpected source: " + source);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   157
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   158
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   159
}