Path: blob/master/test/jdk/java/lang/management/PlatformLoggingMXBean/PlatformLoggingMXBeanTest.java
41155 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6876135 7024172 706769126* @summary Test PlatformLoggingMXBean27* This test performs similar testing as28* java/util/logging/LoggingMXBeanTest.29*30* @build PlatformLoggingMXBeanTest31* @run main PlatformLoggingMXBeanTest32*/3334import javax.management.*;35import java.lang.management.ManagementFactory;36import java.lang.management.PlatformLoggingMXBean;37import java.util.logging.*;38import java.util.List;3940public class PlatformLoggingMXBeanTest41{42ObjectName objectName = null;43static String LOGGER_NAME_1 = "com.sun.management.Logger1";44static String LOGGER_NAME_2 = "com.sun.management.Logger2";4546// Use Logger instance variables to prevent premature garbage collection47// of weak references.48Logger logger1;49Logger logger2;5051public PlatformLoggingMXBeanTest() throws Exception {52}5354private void runTest(PlatformLoggingMXBean mBean) throws Exception {5556/*57* Create the MBeanServeri, register the PlatformLoggingMXBean58*/59System.out.println( "***************************************************" );60System.out.println( "********** PlatformLoggingMXBean Unit Test **********" );61System.out.println( "***************************************************" );62System.out.println( "" );63System.out.println( "*******************************" );64System.out.println( "*********** Phase 1 ***********" );65System.out.println( "*******************************" );66System.out.println( " Creating MBeanServer " );67System.out.print( " Register PlatformLoggingMXBean: " );68MBeanServer mbs = MBeanServerFactory.createMBeanServer();69String[] list = new String[0];7071try {72objectName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME);73mbs.registerMBean( mBean, objectName );74}75catch ( Exception e ) {76System.out.println( "FAILED" );77throw e;78}79System.out.println( "PASSED" );80System.out.println("");8182/*83* Access our MBean to get the current list of Loggers84*/85System.out.println( "*******************************" );86System.out.println( "*********** Phase 2 ***********" );87System.out.println( "*******************************" );88System.out.println( " Test Logger Name retrieval (getLoggerNames) " );89// check that Level object are returned properly90try {91list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );92}93catch ( Exception e ) {94System.out.println(" : FAILED" );95throw e;96}9798/*99* Dump the list of Loggers already present, if any100*/101Object[] params = new Object[1];102String[] signature = new String[1];103Level l;104105if ( list == null ) {106System.out.println(" : PASSED. No Standard Loggers Present" );107System.out.println("");108}109else {110System.out.println(" : PASSED. There are " + list.length + " Loggers Present" );111System.out.println("");112System.out.println( "*******************************" );113System.out.println( "*********** Phase 2B **********" );114System.out.println( "*******************************" );115System.out.println( " Examine Existing Loggers" );116for ( int i = 0; i < list.length; i++ ) {117try {118params[0] = list[i];119signature[0] = "java.lang.String";120String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );121System.out.println(" : Logger #" + i + " = " + list[i] );122System.out.println(" : Level = " + levelName );123}124catch ( Exception e ) {125System.out.println(" : FAILED" );126throw e;127}128}129System.out.println(" : PASSED" );130}131132/*133* Create two new loggers to the list of Loggers already present134*/135System.out.println("");136System.out.println( "*******************************" );137System.out.println( "*********** Phase 3 ***********" );138System.out.println( "*******************************" );139System.out.println( " Create and test new Loggers" );140logger1 = Logger.getLogger( LOGGER_NAME_1 );141logger2 = Logger.getLogger( LOGGER_NAME_2 );142143// check that Level object are returned properly144try {145list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );146}147catch ( Exception e ) {148System.out.println(" : FAILED" );149throw e;150}151152/*153* Check for the existence of our new Loggers154*/155boolean log1 = false, log2 = false;156157if ( list == null || list.length < 2 ) {158System.out.println(" : FAILED. Could not Detect the presense of the new Loggers" );159throw new RuntimeException(160"Could not Detect the presense of the new Loggers");161}162else {163for ( int i = 0; i < list.length; i++ ) {164if ( list[i].equals( LOGGER_NAME_1 ) ) {165log1 = true;166System.out.println( " : Found new Logger : " + list[i] );167}168if ( list[i].equals( LOGGER_NAME_2 ) ) {169log2 = true;170System.out.println( " : Found new Logger : " + list[i] );171}172}173if ( log1 && log2 )174System.out.println( " : PASSED." );175else {176System.out.println( " : FAILED. Could not Detect the new Loggers." );177throw new RuntimeException(178"Could not Detect the presense of the new Loggers");179}180}181182/*183* Set a new Logging levels and check that it succeeded184*/185System.out.println("");186System.out.println( "*******************************" );187System.out.println( "*********** Phase 4 ***********" );188System.out.println( "*******************************" );189System.out.println( " Set and Check the Logger Level" );190log1 = false;191log2 = false;192193try {194// Set the level of logger1 to ALL195params = new Object[2];196signature = new String[2];197params[0] = LOGGER_NAME_1;198params[1] = Level.ALL.getName();199signature[0] = "java.lang.String";200signature[1] = "java.lang.String";201mbs.invoke( objectName, "setLoggerLevel", params, signature );202203// Set the level of logger2 to FINER204params[0] = LOGGER_NAME_2;205params[1] = Level.FINER.getName();206mbs.invoke( objectName, "setLoggerLevel", params, signature );207208// Okay read back the Level from Logger1. Should be ALL209params = new Object[1];210signature = new String[1];211params[0] = LOGGER_NAME_1;212signature[0] = "java.lang.String";213String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );214l = Level.parse(levelName);215System.out.print(" Logger1: " );216if ( l.equals( l.ALL ) ) {217System.out.println("Level Set to ALL: PASSED" );218log1 = true;219}220else {221System.out.println("Level Set to ALL: FAILED" );222throw new RuntimeException(223"Level Set to ALL but returned " + l.toString());224}225226// Okay read back the Level from Logger2. Should be FINER227params = new Object[1];228signature = new String[1];229params[0] = LOGGER_NAME_2;230signature[0] = "java.lang.String";231levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );232l = Level.parse(levelName);233System.out.print(" Logger2: " );234if ( l.equals( l.FINER ) ) {235System.out.println("Level Set to FINER: PASSED" );236log2 = true;237}238else {239System.out.println("Level Set to FINER: FAILED" );240throw new RuntimeException(241"Level Set to FINER but returned " + l.toString());242}243}244catch ( Exception e ) {245throw e;246}247248System.out.println( "" );249System.out.println( "***************************************************" );250System.out.println( "***************** All Tests Passed ****************" );251System.out.println( "***************************************************" );252}253254public static void main(String[] argv) throws Exception {255PlatformLoggingMXBean mbean =256ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class);257ObjectName objname = mbean.getObjectName();258if (!objname.equals(new ObjectName(LogManager.LOGGING_MXBEAN_NAME))) {259throw new RuntimeException("Invalid ObjectName " + objname);260}261262// check if the PlatformLoggingMXBean is registered in the platform MBeanServer263MBeanServer platformMBS = ManagementFactory.getPlatformMBeanServer();264ObjectName objName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME);265266// We could call mbs.isRegistered(objName) here.267// Calling getMBeanInfo will throw exception if not found.268platformMBS.getMBeanInfo(objName);269270if (!platformMBS.isInstanceOf(objName, "java.lang.management.PlatformLoggingMXBean")) {271throw new RuntimeException(objName + " is of unexpected type");272}273274// test if PlatformLoggingMXBean works properly in a MBeanServer275PlatformLoggingMXBeanTest test = new PlatformLoggingMXBeanTest();276test.runTest(mbean);277}278}279280281