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