jdk/test/java/util/logging/SimpleFormatterFormat.java
author jgish
Thu, 29 Nov 2012 12:28:02 +0000
changeset 14683 38921a46c62d
parent 9014 117374ccf1b7
child 14856 92a1bcf46888
permissions -rw-r--r--
8003380: Compiler warnings in logging test code Summary: Use generics, suppress warnings where appropriate, remove unused imports, etc. Reviewed-by: lancea, chegar
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
/*
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
     2
 * Copyright (c) 2011, 2012 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
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
14683
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
    95
        try (FileInputStream in = new FileInputStream(log);
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
    96
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
38921a46c62d 8003380: Compiler warnings in logging test code
jgish
parents: 9014
diff changeset
    97
                ) {
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    98
            String line;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
    99
            int i = 0;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   100
            while (i < messages.length &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   101
                      (line = reader.readLine()) != null) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   102
                String expectedLogger = loggers[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   103
                String expectedMsg = messages[i];
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   104
                i++;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   105
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   106
                line = line.trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   107
                System.out.println(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   108
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   109
                Matcher m = p.matcher(line);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   110
                if (!m.matches()) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   111
                    throw new RuntimeException("Unexpected output format");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   112
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   113
                if (m.groupCount() != 3) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   114
                    throw new RuntimeException("Unexpected group count = " +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   115
                        m.groupCount());
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   116
                }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   117
                // verify logger name and level
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   118
                String[] ss = m.group(1).split(":");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   119
                int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   120
                if (len != 2) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   121
                    throw new RuntimeException("Unexpected logger name and level" +
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   122
                        m.group(1));
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
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   125
                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
   126
            }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   127
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   128
            // expect IllegalArgumentException following it
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   129
            line = reader.readLine().trim();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   130
            if (!line.equals("java.lang.IllegalArgumentException")) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   131
                throw new RuntimeException("Invalid line: " + line);
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
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   136
    private static void verify(String expectedLogger, String expectedMsg,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   137
                               String logger, String level,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   138
                               String msg, String source) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   139
        if (!logger.equals(expectedLogger)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   140
            throw new RuntimeException("Unexpected logger: " + logger);
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
        if (!msg.equals(expectedMsg)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   143
            throw new RuntimeException("Unexpected message: " + msg);
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
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   146
        String[] ss = expectedMsg.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   147
        String expectedLevel = ss[0].toUpperCase();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   148
        if (!level.equals(expectedLevel)) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   149
            throw new RuntimeException("Unexpected level: " + level);
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
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   152
        ss = source.split("\\s+");
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   153
        int len = ss.length;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   154
        if (!(len == 2 &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   155
              ss[0].equals("SimpleFormatterFormat") &&
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   156
              ss[1].equals("writeLogRecords"))) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   157
            throw new RuntimeException("Unexpected source: " + source);
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
    }
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents:
diff changeset
   160
}