1 /* 2 * Copyright (c) 2004, 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 5007165 27 * @summary Basic Test for LoggingMXBean (direct access to MXBean) 28 * @author Mandy Chung 29 * 30 * @build LoggingMXBeanTest2 31 * @run main LoggingMXBeanTest2 32 */ 33 34 import java.util.logging.*; 35 import java.util.List; 36 import java.util.ListIterator; 37 38 public class LoggingMXBeanTest2 39 { 40 static LoggingMXBean mbean = LogManager.getLoggingMXBean(); 41 static String LOGGER_NAME_1 = "com.sun.management.Logger"; 42 static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2"; 43 static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown"; 44 static Logger logger1; 45 static Logger logger2; 46 LoggingMXBeanTest2()47 public LoggingMXBeanTest2() throws Exception { 48 49 logger1 = Logger.getLogger( LOGGER_NAME_1 ); 50 logger1.setLevel(Level.FINE); 51 logger2 = Logger.getLogger( LOGGER_NAME_2 ); 52 logger2.setLevel(null); 53 54 /* 55 * Check for the existence of our new Loggers 56 */ 57 System.out.println("Test Logger Name retrieval (getLoggerNames)"); 58 boolean log1 = false, log2 = false; 59 List<String> loggers = mbean.getLoggerNames(); 60 if (loggers == null || loggers.size() < 2) { 61 throw new RuntimeException( 62 "Could not Detect the presense of the new Loggers"); 63 } 64 65 for (ListIterator<String> iter = loggers.listIterator(); iter.hasNext(); ) { 66 String logger = iter.next(); 67 if (logger.equals(LOGGER_NAME_1)) { 68 log1 = true; 69 System.out.println(" : Found new Logger : " + logger); 70 } 71 if (logger.equals(LOGGER_NAME_2)) { 72 log2 = true; 73 System.out.println(" : Found new Logger : " + logger); 74 } 75 } 76 if ( log1 && log2 ) 77 System.out.println(" : PASSED." ); 78 else { 79 System.out.println(" : FAILED. Could not Detect the new Loggers." ); 80 throw new RuntimeException( 81 "Could not Detect the presense of the new Loggers"); 82 } 83 84 System.out.println("Test getLoggerLevel"); 85 String l1 = mbean.getLoggerLevel(LOGGER_NAME_1); 86 System.out.println(" : Level for Logger " + LOGGER_NAME_1 + " : " + l1); 87 if (!l1.equals(Level.FINE.getName())) { 88 throw new RuntimeException( 89 "Expected level for " + LOGGER_NAME_1 + " = " + 90 Level.FINE.getName() + " but got " + l1); 91 } 92 String l2 = mbean.getLoggerLevel(LOGGER_NAME_2); 93 System.out.println(" : Level for Logger " + LOGGER_NAME_2 + " : " + l2); 94 if (!l2.equals("")) { 95 throw new RuntimeException( 96 "Expected level for " + LOGGER_NAME_2 + " = \"\"" + 97 " but got " + l2); 98 } 99 String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME); 100 System.out.println(" : Level for unknown logger : " + l3); 101 if (l3 != null) { 102 throw new RuntimeException( 103 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" + 104 " but got " + l3); 105 } 106 107 System.out.println("Test setLoggerLevel"); 108 mbean.setLoggerLevel(LOGGER_NAME_1, "INFO"); 109 System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO"); 110 Level l = logger1.getLevel(); 111 if (l != Level.INFO) { 112 throw new RuntimeException( 113 "Expected level for " + LOGGER_NAME_1 + " = " + 114 Level.INFO + " but got " + l); 115 } 116 117 mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE"); 118 System.out.println(" : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER"); 119 l = logger2.getLevel(); 120 if (l != Level.SEVERE) { 121 throw new RuntimeException( 122 "Expected level for " + LOGGER_NAME_2 + " = " + 123 Level.SEVERE+ " but got " + l); 124 } 125 126 mbean.setLoggerLevel(LOGGER_NAME_1, null); 127 System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: null"); 128 l = logger1.getLevel(); 129 if (l != null) { 130 throw new RuntimeException( 131 "Expected level for " + LOGGER_NAME_1 + " = null " + 132 " but got " + l); 133 } 134 135 boolean iaeCaught = false; 136 System.out.println(" : Set Level for unknown Logger to: FINE"); 137 try { 138 mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE"); 139 } catch (IllegalArgumentException e) { 140 // expected 141 iaeCaught = true; 142 System.out.println(" : IllegalArgumentException caught as expected"); 143 } 144 if (!iaeCaught) { 145 throw new RuntimeException( 146 "Expected IllegalArgumentException for setting level for " + 147 UNKNOWN_LOGGER_NAME + " not thrown"); 148 } 149 iaeCaught = false; 150 System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY"); 151 try { 152 mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY"); 153 } catch (IllegalArgumentException e) { 154 // expected 155 iaeCaught = true; 156 System.out.println(" : IllegalArgumentException caught as expected"); 157 } 158 if (!iaeCaught) { 159 throw new RuntimeException( 160 "Expected IllegalArgumentException for invalid level."); 161 } 162 163 164 System.out.println("Test getParentLoggerName"); 165 String p1 = mbean.getParentLoggerName(LOGGER_NAME_2); 166 System.out.println(" : Parent Logger for " + LOGGER_NAME_2 + " : " + p1); 167 if (!p1.equals(LOGGER_NAME_1)) { 168 throw new RuntimeException( 169 "Expected parent for " + LOGGER_NAME_2 + " = " + 170 LOGGER_NAME_1 + " but got " + p1); 171 } 172 String p2 = mbean.getParentLoggerName(""); 173 System.out.println(" : Parent Logger for \"\" : " + p2); 174 if (!p2.equals("")) { 175 throw new RuntimeException( 176 "Expected parent for root logger \"\" = \"\"" + 177 " but got " + p2); 178 } 179 String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME); 180 System.out.println(" : Parent Logger for unknown logger : " + p3); 181 if (p3 != null) { 182 throw new RuntimeException( 183 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" + 184 " but got " + p3); 185 } 186 } 187 main(String[] argv)188 public static void main(String[] argv) throws Exception { 189 new LoggingMXBeanTest2(); 190 } 191 } 192