1 /* 2 * Copyright (c) 2003, 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 * 28 * @summary Basic Test for LoggingMXBean via MBeanServer 29 * @author Ron Mann 30 * 31 * @build LoggingMXBeanTest 32 * @run main LoggingMXBeanTest 33 */ 34 35 import javax.management.*; 36 import java.util.logging.*; 37 38 39 public class LoggingMXBeanTest 40 { 41 42 LoggingMXBean mBean; 43 ObjectName objectName = null; 44 static String LOGGER_NAME_1 = "com.sun.management.Logger1"; 45 static String LOGGER_NAME_2 = "com.sun.management.Logger2"; 46 static Logger logger1; 47 static Logger logger2; 48 49 public LoggingMXBeanTest() throws Exception { 50 51 /* 52 * Create the MBeanServeri, register the LoggingMXBean 53 */ 54 System.out.println( "***************************************************" ); 55 System.out.println( "********** LoggingMXBean Unit Test **********" ); 56 System.out.println( "***************************************************" ); 57 System.out.println( "" ); 58 System.out.println( "*******************************" ); 59 System.out.println( "*********** Phase 1 ***********" ); 60 System.out.println( "*******************************" ); 61 System.out.println( " Creating MBeanServer " ); 62 System.out.print( " Register LoggingMXBean: " ); 63 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 64 String[] list = new String[0]; 65 66 try { 67 objectName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME); 68 mBean = LogManager.getLoggingMXBean(); 69 mbs.registerMBean( mBean, objectName ); 70 } 71 catch ( Exception e ) { 72 System.out.println( "FAILED" ); 73 throw e; 74 } 75 System.out.println( "PASSED" ); 76 System.out.println(""); 77 78 /* 79 * Access our MBean to get the current list of Loggers 80 */ 81 System.out.println( "*******************************" ); 82 System.out.println( "*********** Phase 2 ***********" ); 83 System.out.println( "*******************************" ); 84 System.out.println( " Test Logger Name retrieval (getLoggerNames) " ); 85 // check that Level object are returned properly 86 try { 87 list = (String[]) mbs.getAttribute( objectName, "LoggerNames" ); 88 } 89 catch ( Exception e ) { 90 System.out.println(" : FAILED" ); 91 throw e; 92 } 93 94 /* 95 * Dump the list of Loggers already present, if any 96 */ 97 Object[] params = new Object[1]; 98 String[] signature = new String[1]; 99 Level l; 100 101 if ( list == null ) { 102 System.out.println(" : PASSED. No Standard Loggers Present" ); 103 System.out.println(""); 104 } 105 else { 106 System.out.println(" : PASSED. There are " + list.length + " Loggers Present" ); 107 System.out.println(""); 108 System.out.println( "*******************************" ); 109 System.out.println( "*********** Phase 2B **********" ); 110 System.out.println( "*******************************" ); 111 System.out.println( " Examine Existing Loggers" ); 112 for ( int i = 0; i < list.length; i++ ) { 113 try { 114 params[0] = list[i]; 115 signature[0] = "java.lang.String"; 116 String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); 117 System.out.println(" : Logger #" + i + " = " + list[i] ); 118 System.out.println(" : Level = " + levelName ); 119 } 120 catch ( Exception e ) { 121 System.out.println(" : FAILED" ); 122 throw e; 123 } 124 } 125 System.out.println(" : PASSED" ); 126 } 127 128 /* 129 * Create two new loggers to the list of Loggers already present 130 */ 131 System.out.println(""); 132 System.out.println( "*******************************" ); 133 System.out.println( "*********** Phase 3 ***********" ); 134 System.out.println( "*******************************" ); 135 System.out.println( " Create and test new Loggers" ); 136 logger1 = Logger.getLogger( LOGGER_NAME_1 ); 137 logger2 = Logger.getLogger( LOGGER_NAME_2 ); 138 139 // check that Level object are returned properly 140 try { 141 list = (String[]) mbs.getAttribute( objectName, "LoggerNames" ); 142 } 143 catch ( Exception e ) { 144 System.out.println(" : FAILED" ); 145 throw e; 146 } 147 148 /* 149 * Check for the existence of our new Loggers 150 */ 151 boolean log1 = false, log2 = false; 152 153 if ( list == null || list.length < 2 ) { 154 System.out.println(" : FAILED. Could not Detect the presense of the new Loggers" ); 155 throw new RuntimeException( 156 "Could not Detect the presense of the new Loggers"); 157 } 158 else { 159 for ( int i = 0; i < list.length; i++ ) { 160 if ( list[i].equals( LOGGER_NAME_1 ) ) { 161 log1 = true; 162 System.out.println( " : Found new Logger : " + list[i] ); 163 } 164 if ( list[i].equals( LOGGER_NAME_2 ) ) { 165 log2 = true; 166 System.out.println( " : Found new Logger : " + list[i] ); 167 } 168 } 169 if ( log1 && log2 ) 170 System.out.println( " : PASSED." ); 171 else { 172 System.out.println( " : FAILED. Could not Detect the new Loggers." ); 173 throw new RuntimeException( 174 "Could not Detect the presense of the new Loggers"); 175 } 176 } 177 178 /* 179 * Set a new Logging levels and check that it succeeded 180 */ 181 System.out.println(""); 182 System.out.println( "*******************************" ); 183 System.out.println( "*********** Phase 4 ***********" ); 184 System.out.println( "*******************************" ); 185 System.out.println( " Set and Check the Logger Level" ); 186 log1 = false; 187 log2 = false; 188 try { 189 // Set the level of logger1 to ALL 190 params = new Object[2]; 191 signature = new String[2]; 192 params[0] = LOGGER_NAME_1; 193 params[1] = Level.ALL.getName(); 194 signature[0] = "java.lang.String"; 195 signature[1] = "java.lang.String"; 196 mbs.invoke( objectName, "setLoggerLevel", params, signature ); 197 198 // Set the level of logger2 to FINER 199 params[0] = LOGGER_NAME_2; 200 params[1] = Level.FINER.getName(); 201 mbs.invoke( objectName, "setLoggerLevel", params, signature ); 202 203 // Okay read back the Level from Logger1. Should be ALL 204 params = new Object[1]; 205 signature = new String[1]; 206 params[0] = LOGGER_NAME_1; 207 signature[0] = "java.lang.String"; 208 String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); 209 l = Level.parse(levelName); 210 System.out.print(" Logger1: " ); 211 if ( l.equals( l.ALL ) ) { 212 System.out.println("Level Set to ALL: PASSED" ); 213 log1 = true; 214 } 215 else { 216 System.out.println("Level Set to ALL: FAILED" ); 217 throw new RuntimeException( 218 "Level Set to ALL but returned " + l.toString()); 219 } 220 221 // Okay read back the Level from Logger2. Should be FINER 222 params = new Object[1]; 223 signature = new String[1]; 224 params[0] = LOGGER_NAME_2; 225 signature[0] = "java.lang.String"; 226 levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); 227 l = Level.parse(levelName); 228 System.out.print(" Logger2: " ); 229 if ( l.equals( l.FINER ) ) { 230 System.out.println("Level Set to FINER: PASSED" ); 231 log2 = true; 232 } 233 else { 234 System.out.println("Level Set to FINER: FAILED" ); 235 throw new RuntimeException( 236 "Level Set to FINER but returned " + l.toString()); 237 } 238 } 239 catch ( Exception e ) { 240 throw e; 241 } 242 243 System.out.println( "" ); 244 System.out.println( "***************************************************" ); 245 System.out.println( "***************** All Tests Passed ****************" ); 246 System.out.println( "***************************************************" ); 247 } 248 249 public static void main(String[] argv) throws Exception { 250 LoggingMXBeanTest p = new LoggingMXBeanTest(); 251 } 252 }