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