1 /* 2 * Copyright (c) 2012, 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 7159567 27 * @summary Test of configuring a MemoryHandler sub-class handler target via logging.properties 28 * @run main/othervm MemoryHandlerTest 29 */ 30 import java.io.File; 31 import java.io.IOException; 32 import java.util.logging.Handler; 33 import java.util.logging.Level; 34 import java.util.logging.LogManager; 35 import java.util.logging.LogRecord; 36 import java.util.logging.Logger; 37 import java.util.logging.MemoryHandler; 38 39 public class MemoryHandlerTest { 40 41 static final String CFG_FILE_PROP = "java.util.logging.config.file"; 42 static final String LM_PROP_FNAME = "MemoryHandlerTest.props"; 43 static Logger logger; 44 main(String... args)45 public static void main(String... args) throws IOException { 46 // load logging.propertes for the test 47 String tstSrc = System.getProperty("test.src", "."); 48 File fname = new File(tstSrc, LM_PROP_FNAME); 49 String prop = fname.getCanonicalPath(); 50 System.setProperty(CFG_FILE_PROP, prop); 51 LogManager.getLogManager(); 52 // create a logger 53 logger = Logger.getLogger(MemoryHandlerTest.class.getName()); 54 // don't have parent handlers get log messages 55 logger.setUseParentHandlers(false); 56 // 57 // Test 1,2: create a CustomMemoryHandler which in the config has 58 // specified a target of CustomTargetHandler. (1) Make sure that it 59 // is created and (2) that the target handler is loaded. 60 // 61 CustomMemoryHandler cmh = new CustomMemoryHandler(); 62 try { 63 logger.addHandler(cmh); 64 } catch (RuntimeException rte) { 65 throw new RuntimeException( 66 "Test Failed: did not load java.util.logging.ConsoleHandler as expected", 67 rte); 68 } 69 // if we get here and our config has been processed properly, then we 70 // should have loaded our target handler 71 if (CustomTargetHandler.numLoaded !=1) { 72 throw new RuntimeException( 73 "Test failed: did not load CustomTargetHandler as expected"); 74 } 75 // 76 // Test 3: try to add a handler with no target. This should fail with 77 // an exception 78 CustomMemoryHandlerNoTarget cmhnt = null; 79 try { 80 cmhnt = new CustomMemoryHandlerNoTarget(); 81 } catch (RuntimeException re) { 82 // expected -- no target specified 83 System.out.println("Info: " + re.getMessage() + " as expected."); 84 } 85 if (cmhnt != null) { 86 throw new RuntimeException( 87 "Test Failed: erroneously loaded CustomMemoryHandlerNoTarget"); 88 } 89 90 // Test 4: log a message and check that the target handler is actually used 91 logger.log(Level.WARNING, "Unused"); 92 if (CustomTargetHandler.numPublished != 1) { 93 throw new RuntimeException("Test failed: CustomTargetHandler was not used"); 94 } 95 96 // Test 5: make sure that SimpleTargetHandler hasn't been erroneously called 97 if (SimpleTargetHandler.numPublished != 0) { 98 throw new RuntimeException("Test failed: SimpleTargetHandler has been used"); 99 } 100 101 // Test 6: try using SimpleTargetHanlder via standard MemoryHandler 102 // (which has target set to SimpleTargetHandler) 103 MemoryHandler mh = new MemoryHandler(); 104 mh.publish(new LogRecord(Level.INFO, "Unused msg to MemoryHandler")); 105 // see if it made it to the SimpleTargetHandler 106 if (SimpleTargetHandler.numPublished != 1) { 107 throw new RuntimeException("Test failed: SimpleTargetHandler was not used"); 108 } 109 } 110 111 public static class CustomMemoryHandler extends MemoryHandler { 112 } 113 114 public static class CustomMemoryHandlerNoTarget extends MemoryHandler { 115 } 116 117 public static class CustomTargetHandler extends Handler { 118 119 public static int numPublished; 120 public static int numLoaded; 121 CustomTargetHandler()122 public CustomTargetHandler() { 123 numLoaded++; 124 } 125 126 @Override publish(LogRecord unused)127 public void publish(LogRecord unused) { 128 numPublished++; 129 } 130 131 @Override flush()132 public void flush() { 133 } 134 135 @Override close()136 public void close() throws SecurityException { 137 } 138 } 139 140 public static class SimpleTargetHandler extends Handler { 141 public static int numPublished; 142 143 @Override publish(LogRecord unused)144 public void publish(LogRecord unused) { 145 numPublished++; 146 } 147 148 @Override flush()149 public void flush() { 150 } 151 152 @Override close()153 public void close() throws SecurityException { 154 } 155 } 156 } 157