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