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