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