jdk/test/java/lang/System/LoggerFinder/LoggerFinderAPI/LoggerFinderAPI.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 44433 82ca8259333d
child 44911 7cfd28612947
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44433
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     1
/*
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     4
 *
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     8
 *
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    14
 *
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    18
 *
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    21
 * questions.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    22
 */
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    23
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    25
import java.io.IOException;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    26
import java.io.OutputStream;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    27
import java.io.PrintStream;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    28
import java.lang.System.Logger;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    29
import java.lang.System.Logger.Level;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    30
import java.lang.System.LoggerFinder;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    31
import java.util.Enumeration;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    32
import java.util.Locale;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    33
import java.util.Objects;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    34
import java.util.ResourceBundle;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    35
import java.util.concurrent.ConcurrentHashMap;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    36
// Can't use testng because testng requires java.logging
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    37
//import org.testng.annotations.Test;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    38
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    39
/**
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    40
 * @test
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    41
 * @bug 8177835
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    42
 * @summary Checks that the DefaultLoggerFinder and LoggingProviderImpl
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    43
 *          implementations of the System.LoggerFinder conform to the
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    44
 *          LoggerFinder specification, in particular with respect to
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    45
 *          throwing NullPointerException. The test uses --limit-module
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    46
 *          to force the selection of one or the other.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    47
 * @author danielfuchs
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    48
 * @build LoggerFinderAPI
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    49
 * @run main/othervm --limit-modules java.base,java.logging
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    50
 *          -Djava.util.logging.SimpleFormatter.format=LOG-%4$s:-[%2$s]-%5$s%6$s%n
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    51
 *          LoggerFinderAPI
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    52
 * @run main/othervm -Djdk.system.logger.format=LOG-%4$s:-[%2$s]-%5$s%6$s%n
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    53
 *          --limit-modules java.base
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    54
 *          LoggerFinderAPI
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    55
 */
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    56
public class LoggerFinderAPI {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    57
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    58
    // Simplified log format string. No white space for main/othervm line
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    59
    static final String TEST_FORMAT = "LOG-%4$s:-[%2$s]-%5$s%6$s%n";
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    60
    static final String JDK_FORMAT_PROP_KEY = "jdk.system.logger.format";
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    61
    static final String JUL_FORMAT_PROP_KEY =
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    62
        "java.util.logging.SimpleFormatter.format";
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    63
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    64
    static class RecordStream extends OutputStream {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    65
        static final Object LOCK = new Object[0];
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    66
        final PrintStream out;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    67
        final PrintStream err;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    68
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    69
        boolean record;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    70
        RecordStream(PrintStream out, PrintStream err) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    71
            this.out = out;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    72
            this.err = err;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    73
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    74
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    75
        @Override
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    76
        public void write(int i) throws IOException {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    77
            if (record) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    78
                bos.write(i);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    79
                out.write(i);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    80
            } else {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    81
                err.write(i);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    82
            }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    83
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    84
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    85
        void startRecording() {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    86
            out.flush();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    87
            err.flush();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    88
            bos.reset();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    89
            record = true;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    90
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    91
        byte[] stopRecording() {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    92
            out.flush();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    93
            err.flush();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    94
            record = false;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    95
            return bos.toByteArray();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    96
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    97
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    98
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
    99
    static final PrintStream ERR = System.err;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   100
    static final PrintStream OUT = System.out;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   101
    static final RecordStream LOG_STREAM = new RecordStream(OUT, ERR);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   102
    static {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   103
        Locale.setDefault(Locale.US);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   104
        PrintStream perr = new PrintStream(LOG_STREAM);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   105
        System.setErr(perr);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   106
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   107
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   108
    public static class MyResourceBundle extends ResourceBundle {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   109
        final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   110
        @Override
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   111
        protected Object handleGetObject(String string) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   112
            return map.computeIfAbsent(string, s -> "[localized] " + s);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   113
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   114
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   115
        @Override
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   116
        public Enumeration<String> getKeys() {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   117
            return map.keys();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   118
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   119
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   120
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   121
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   122
    public static void main(String[] args) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   123
        // Set on the command line, to ensure that the test will fail if
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   124
        // the 'wrong' provider gets selected.
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   125
        // System.setProperty(JDK_FORMAT_PROP_KEY, TEST_FORMAT);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   126
        // System.setProperty(JUL_FORMAT_PROP_KEY, TEST_FORMAT);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   127
        LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   128
        System.out.println("LoggerFinder is " + finder.getClass().getName());
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   129
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   130
        LoggerFinderAPI apiTest = new LoggerFinderAPI();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   131
        for (Object[] params : getLoggerDataProvider()) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   132
            apiTest.testGetLogger((String)params[0],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   133
                                  (String)params[1],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   134
                                  (Module)params[2],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   135
                                  Throwable.class.getClass().cast(params[3]));
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   136
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   137
        for (Object[] params : getLocalizedLoggerDataProvider()) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   138
            apiTest.testGetLocalizedLogger((String)params[0],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   139
                                  (String)params[1],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   140
                                  (ResourceBundle)params[2],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   141
                                  (Module)params[3],
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   142
                                  Throwable.class.getClass().cast(params[4]));
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   143
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   144
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   145
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   146
    //Can't use testng because testng requires java.logging
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   147
    //@Test(dataProvider = "testGetLoggerDataProvider")
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   148
    void testGetLogger(String desc, String name, Module mod, Class<? extends Throwable> thrown) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   149
        try {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   150
            LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   151
            Logger logger = finder.getLogger(name, mod);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   152
            if (thrown != null) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   153
                throw new AssertionError("Exception " + thrown.getName()
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   154
                        + " not thrown for "
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   155
                        + "LoggerFinder.getLogger"
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   156
                        + " with " + desc);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   157
            }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   158
            // Make sure we don't fail if tests are run in parallel
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   159
            synchronized(RecordStream.LOCK) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   160
                LOG_STREAM.startRecording();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   161
                try {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   162
                    logger.log(Level.INFO, "{0} with {1}: PASSED",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   163
                               "LoggerFinder.getLogger",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   164
                               desc);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   165
                } finally {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   166
                    byte[] logged = LOG_STREAM.stopRecording();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   167
                    check(logged, "testGetLogger", desc, null,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   168
                          "LoggerFinder.getLogger");
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   169
                }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   170
            }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   171
        } catch (Throwable x) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   172
            if (thrown != null && thrown.isInstance(x)) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   173
                System.out.printf("Got expected exception for %s with %s: %s\n",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   174
                        "LoggerFinder.getLogger", desc, String.valueOf(x));
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   175
            } else throw x;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   176
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   177
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   178
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   179
    //Can't use testng because testng requires java.logging
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   180
    //@Test(dataProvider = "getLocalizedLoggerDataProvider")
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   181
    void testGetLocalizedLogger(String desc, String name, ResourceBundle bundle,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   182
                                Module mod, Class<? extends Throwable> thrown) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   183
        try {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   184
            LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   185
            Logger logger = finder.getLocalizedLogger(name, bundle, mod);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   186
            if (thrown != null) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   187
                throw new AssertionError("Exception " + thrown.getName()
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   188
                        + " not thrown for "
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   189
                        + "LoggerFinder.getLocalizedLogger"
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   190
                        + " with " + desc);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   191
            }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   192
            // Make sure we don't fail if tests are run in parallel
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   193
            synchronized(RecordStream.LOCK) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   194
                LOG_STREAM.startRecording();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   195
                try {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   196
                    logger.log(Level.INFO, "{0} with {1}: PASSED",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   197
                              "LoggerFinder.getLocalizedLogger",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   198
                              desc);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   199
                } finally {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   200
                    byte[] logged = LOG_STREAM.stopRecording();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   201
                    check(logged, "testGetLocalizedLogger", desc, bundle,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   202
                          "LoggerFinder.getLocalizedLogger");
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   203
                }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   204
            }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   205
        } catch (Throwable x) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   206
            if (thrown != null && thrown.isInstance(x)) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   207
                System.out.printf("Got expected exception for %s with %s: %s\n",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   208
                        "LoggerFinder.getLocalizedLogger", desc, String.valueOf(x));
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   209
            } else throw x;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   210
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   211
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   212
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   213
    private void check(byte[] logged, String test, String desc,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   214
                       ResourceBundle bundle, String meth) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   215
        String msg = new String(logged);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   216
        String expected = String.format(TEST_FORMAT, null,
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   217
                "LoggerFinderAPI " + test, null, Level.INFO.name(),
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   218
                (bundle==null?"":"[localized] ") + meth + " with " + desc + ": PASSED",
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   219
                "");
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   220
        if (!Objects.equals(msg, expected)) {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   221
            throw new AssertionError("Expected log message not found: "
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   222
                                     + "\n\texpected:  " + expected
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   223
                                     + "\n\tretrieved: " + msg);
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   224
        }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   225
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   226
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   227
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   228
    static final Module MODULE = LoggerFinderAPI.class.getModule();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   229
    static final ResourceBundle BUNDLE = new MyResourceBundle();
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   230
    static final Object[][] GET_LOGGER = {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   231
        {"null name", null, MODULE , NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   232
        {"null module", "foo", null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   233
        {"null name and module", null, null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   234
        {"non null name and module", "foo", MODULE, null},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   235
    };
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   236
    static final Object[][] GET_LOCALIZED_LOGGER = {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   237
        {"null name", null, BUNDLE, MODULE , NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   238
        {"null module", "foo", BUNDLE, null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   239
        {"null name and module", null, BUNDLE, null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   240
        {"non null name and module", "foo", BUNDLE, MODULE, null},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   241
        {"null name and bundle", null, null, MODULE , NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   242
        {"null module and bundle", "foo", null, null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   243
        {"null name and module and bundle", null, null, null, NullPointerException.class},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   244
        {"non null name and module, null bundle", "foo", null, MODULE, null},
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   245
    };
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   246
    public static Object[][] getLoggerDataProvider() {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   247
        return GET_LOGGER;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   248
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   249
    public static Object[][] getLocalizedLoggerDataProvider() {
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   250
        return GET_LOCALIZED_LOGGER;
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   251
    }
82ca8259333d 8177835: System.LoggerFinder#getLogger or getLocalizedLogger does not throw NPE
dfuchs
parents:
diff changeset
   252
}