test/jdk/java/util/logging/SimpleFormatterFormat.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
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
/*
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
     2
 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
9014
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
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    26
 * @bug     6381464 8153666
9014
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",
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    63
        "test.bar",
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    64
        "test.bar",
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    65
        "test.bar"
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    66
    };
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    67
    private static String[] messages = new String[] {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    68
        "severe hello world",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    69
        "warning lost connection",
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    70
        "info welcome",
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    71
        "warning beware of traps",
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    72
        "warning { {ok7} }",
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    73
        // keep exception logging as last test case to avoid having
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    74
        // to skip the exception stack trace in the output
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    75
        "warning exception thrown"
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    76
    };
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    77
    private static void writeLogRecords(PrintStream logps) throws Exception {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    78
        try {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    79
            System.setErr(logps);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    80
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    81
            Logger foo = Logger.getLogger("test.foo");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    82
            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
    83
            foo.warning(messages[1]);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    84
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    85
            Logger bar = Logger.getLogger("test.bar");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    86
            bar.finest("Dummy message");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    87
            bar.info(messages[2]);
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    88
            bar.log(Level.WARNING, "{0}", new Object[] { messages[3] });
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    89
            bar.log(Level.WARNING, "warning '{' '{'{7}} }", new Object[] {"ok", "ok1", "ok2", "ok3", "ok4", "ok5", "ok6", "ok7", "ok8", "ok9", "ok10"});
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    90
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    91
            // Keep this one last - as it also prints the exception stack trace...
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
    92
            bar.log(Level.WARNING, messages[messages.length-1], new IllegalArgumentException());
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    93
        } finally {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    94
            logps.flush();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    95
            logps.close();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    96
            System.setErr(err);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    97
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    98
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    99
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   100
    private static void checkLogRecords(File log) throws Exception {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   101
        System.out.println("Checking log records in file: " + log);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   102
        Pattern p = Pattern.compile("([\\.a-zA-Z:]+) (.*) \\[.*\\] source: (.*)");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   103
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
   104
        try (FileInputStream in = new FileInputStream(log);
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
   105
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
   106
                ) {
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   107
            String line;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   108
            int i = 0;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   109
            while (i < messages.length &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   110
                      (line = reader.readLine()) != null) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   111
                String expectedLogger = loggers[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   112
                String expectedMsg = messages[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   113
                i++;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   114
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   115
                line = line.trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   116
                System.out.println(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   117
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   118
                Matcher m = p.matcher(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   119
                if (!m.matches()) {
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 14856
diff changeset
   120
                    throw new RuntimeException("Unexpected output format: " + line);
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   121
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   122
                if (m.groupCount() != 3) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   123
                    throw new RuntimeException("Unexpected group count = " +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   124
                        m.groupCount());
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
                // verify logger name and level
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   127
                String[] ss = m.group(1).split(":");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   128
                int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   129
                if (len != 2) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   130
                    throw new RuntimeException("Unexpected logger name and level" +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   131
                        m.group(1));
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
                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
   135
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   136
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   137
            // expect IllegalArgumentException following it
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   138
            line = reader.readLine().trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   139
            if (!line.equals("java.lang.IllegalArgumentException")) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   140
                throw new RuntimeException("Invalid line: " + line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   141
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   142
        }
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
    private static void verify(String expectedLogger, String expectedMsg,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   146
                               String logger, String level,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   147
                               String msg, String source) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   148
        if (!logger.equals(expectedLogger)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   149
            throw new RuntimeException("Unexpected logger: " + logger);
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
        if (!msg.equals(expectedMsg)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   152
            throw new RuntimeException("Unexpected message: " + msg);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   153
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   154
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   155
        String[] ss = expectedMsg.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   156
        String expectedLevel = ss[0].toUpperCase();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   157
        if (!level.equals(expectedLevel)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   158
            throw new RuntimeException("Unexpected level: " + level);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   159
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   160
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   161
        ss = source.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   162
        int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   163
        if (!(len == 2 &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   164
              ss[0].equals("SimpleFormatterFormat") &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   165
              ss[1].equals("writeLogRecords"))) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   166
            throw new RuntimeException("Unexpected source: " + source);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   167
        }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   168
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   169
}