jdk/test/sun/util/logging/SourceClassName.java
author weijun
Thu, 08 Sep 2011 09:04:28 +0800
changeset 10435 e9df9d264894
parent 7026 9f2ec5fad124
child 27029 6157b22bd081
permissions -rw-r--r--
7087428: move client tests out of jdk_misc Reviewed-by: alanb, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7026
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     1
/*
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     4
 *
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     8
 *
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    13
 * accompanied this code).
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    14
 *
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    18
 *
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    21
 * questions.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    22
 */
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    23
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    24
/*
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    25
 * @test
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    26
 * @bug     6985460
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    27
 * @summary Test the source class name and method output by the platform
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    28
 *          logger.
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    29
 *
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file SourceClassName.java
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    31
 * @run main/othervm SourceClassName
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    32
 */
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    33
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    34
import java.util.logging.*;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    35
import java.io.*;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    36
import sun.util.logging.PlatformLogger;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    37
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    38
public class SourceClassName {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    40
        File dir = new File(System.getProperty("user.dir", "."));
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    41
        File log = new File(dir, "testlog.txt");
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    42
        PrintStream logps = new PrintStream(log);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    43
        writeLogRecords(logps);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    44
        checkLogRecords(log);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    45
    }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    46
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    47
    private static void writeLogRecords(PrintStream logps) throws Exception {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    48
        PrintStream err = System.err;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    49
        try {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    50
            System.setErr(logps);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    51
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    52
            Object[] params = new Object[] { new Long(1), "string"};
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    53
            PlatformLogger plog = PlatformLogger.getLogger("test.log.foo");
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    54
            plog.severe("Log message {0} {1}", (Object[]) params);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    55
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    56
            // create a java.util.logging.Logger
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    57
            // now java.util.logging.Logger should be created for each platform logger
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    58
            Logger logger = Logger.getLogger("test.log.bar");
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    59
            logger.log(Level.SEVERE, "Log message {0} {1}", params);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    60
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    61
            plog.severe("Log message {0} {1}", (Object[]) params);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    62
        } finally {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    63
            logps.flush();
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    64
            logps.close();
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    65
            System.setErr(err);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    66
        }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    67
    }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    68
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    69
    private static void checkLogRecords(File log) throws Exception {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    70
        System.out.println("Checking log records in file: " + log);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    71
        FileInputStream in = new FileInputStream(log);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    72
        String EXPECTED_LOG = "SEVERE: Log message 1 string";
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    73
        try {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    74
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    75
            String line;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    76
            String[] record = new String[2];
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    77
            int count = 0;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    78
            int i = 0;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    79
            while ((line = reader.readLine()) != null) {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    80
                line = line.trim();
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    81
                System.out.println(line);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    82
                record[i++] = line;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    83
                if (i == 2) {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    84
                    i = 0;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    85
                    count++;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    86
                    // check source class name and method
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    87
                    String[] ss = record[0].split("\\s+");
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    88
                    int len = ss.length;
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    89
                    if (!ss[len-2].equals("SourceClassName") ||
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    90
                        !ss[len-1].equals("writeLogRecords")) {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    91
                        throw new RuntimeException("Unexpected source: " +
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    92
                            ss[len-2] + " " + ss[len-1]);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    93
                    }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    94
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    95
                    // check log message
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    96
                    if (!record[1].equals(EXPECTED_LOG)) {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    97
                        throw new RuntimeException("Unexpected log: " + record[1]);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    98
                    }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
    99
                }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   100
            }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   101
            if (count != 3) {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   102
                throw new RuntimeException("Unexpected number of records: " + count);
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   103
            }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   104
        } finally {
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   105
            in.close();
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   106
        }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   107
    }
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
diff changeset
   108
}