test/jdk/java/util/logging/IsLoggableHandlerTest.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 53876 8bc3d3eeaa53
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53876
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     1
/*
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     4
 *
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     8
 *
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    14
 *
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    18
 *
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    21
 * questions.
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    22
 */
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    23
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    24
/*
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    25
 * @test
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    26
 * @bug 8216363
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    27
 * @summary Test that Handler.isLoggable(null) returns false
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    28
 * @run main/othervm IsLoggableHandlerTest
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    29
 */
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    30
import java.io.File;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    31
import java.io.IOException;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    32
import java.util.UUID;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    33
import java.util.logging.ConsoleHandler;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    34
import java.util.logging.FileHandler;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    35
import java.util.logging.Handler;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    36
import java.util.logging.Level;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    37
import java.util.logging.LogRecord;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    38
import java.util.logging.MemoryHandler;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    39
import java.util.logging.StreamHandler;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    40
import java.util.stream.Stream;
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    41
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    42
public class IsLoggableHandlerTest {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    43
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    44
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    45
    public static void main(String... args) throws IOException {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    46
        String userDir = System.getProperty("user.dir", ".");
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    47
        File logfile = new File(userDir, "IsLoggableHandlerTest_" + UUID.randomUUID() + ".log");
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    48
        try {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    49
            System.out.println("Dummy logfile: " + logfile.getAbsolutePath());
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    50
            Handler h = new CustomHandler();
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    51
            testIsLoggable(h);
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    52
            testIsLoggable(new MemoryHandler(h, 1, Level.ALL));
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    53
            testIsLoggable(new StreamHandler(System.out, new java.util.logging.SimpleFormatter()));
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    54
            testIsLoggable(new FileHandler(logfile.getAbsolutePath()));
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    55
            testIsLoggable(new ConsoleHandler());
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    56
        } finally {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    57
            if (logfile.canRead()) {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    58
                try {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    59
                    System.out.println("Deleting dummy logfile: " + logfile.getAbsolutePath());
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    60
                    logfile.delete();
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    61
                } catch (Throwable t) {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    62
                    System.out.println("Warning: failed to delete dummy logfile: " + t);
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    63
                    t.printStackTrace();
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    64
                }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    65
            }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    66
        }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    67
    }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    68
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    69
    public static void testIsLoggable(Handler h) {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    70
        System.out.println("Testing " + h.getClass().getName());
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    71
        // should not throw NPE but return false
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    72
        if (h.isLoggable(null)) {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    73
            throw new AssertionError(h.getClass().getName()
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    74
                    + ": null record should not be loggable");
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    75
        }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    76
        h.setLevel(Level.ALL);
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    77
        // should still not throw NPE but return false
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    78
        if (h.isLoggable(null)) {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    79
            throw new AssertionError(h.getClass().getName()
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    80
                    + ": null record should not be loggable");
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    81
        }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    82
        // should not throw NPE
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    83
        h.publish(null);
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    84
    }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    85
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    86
    public static final class CustomHandler extends Handler {
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    87
        @Override
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    88
        public void publish(LogRecord record) { }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    89
        @Override
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    90
        public void flush() { }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    91
        @Override
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    92
        public void close() throws SecurityException { }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    93
    }
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    94
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    95
8bc3d3eeaa53 8216363: NullPointerException in java.util.logging.Handler#isLoggable
dfuchs
parents:
diff changeset
    96
}