Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/PlatformLoggingMXBean/PlatformLoggingMXBeanTest.java
38821 views
/*1* Copyright (c) 2003, 2011, 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*27* @summary Test PlatformLoggingMXBean28* This test performs similar testing as29* java/util/logging/LoggingMXBeanTest.30*31* @build PlatformLoggingMXBeanTest32* @run main PlatformLoggingMXBeanTest33*/3435import javax.management.*;36import java.lang.management.ManagementFactory;37import java.lang.management.PlatformLoggingMXBean;38import java.util.logging.*;39import java.util.List;4041public class PlatformLoggingMXBeanTest42{43ObjectName objectName = null;44static String LOGGER_NAME_1 = "com.sun.management.Logger1";45static String LOGGER_NAME_2 = "com.sun.management.Logger2";4647// Use Logger instance variables to prevent premature garbage collection48// of weak references.49Logger logger1;50Logger logger2;5152public PlatformLoggingMXBeanTest() throws Exception {53}5455private void runTest(PlatformLoggingMXBean mBean) throws Exception {5657/*58* Create the MBeanServeri, register the PlatformLoggingMXBean59*/60System.out.println( "***************************************************" );61System.out.println( "********** PlatformLoggingMXBean Unit Test **********" );62System.out.println( "***************************************************" );63System.out.println( "" );64System.out.println( "*******************************" );65System.out.println( "*********** Phase 1 ***********" );66System.out.println( "*******************************" );67System.out.println( " Creating MBeanServer " );68System.out.print( " Register PlatformLoggingMXBean: " );69MBeanServer mbs = MBeanServerFactory.createMBeanServer();70String[] list = new String[0];7172try {73objectName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME);74mbs.registerMBean( mBean, objectName );75}76catch ( Exception e ) {77System.out.println( "FAILED" );78throw e;79}80System.out.println( "PASSED" );81System.out.println("");8283/*84* Access our MBean to get the current list of Loggers85*/86System.out.println( "*******************************" );87System.out.println( "*********** Phase 2 ***********" );88System.out.println( "*******************************" );89System.out.println( " Test Logger Name retrieval (getLoggerNames) " );90// check that Level object are returned properly91try {92list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );93}94catch ( Exception e ) {95System.out.println(" : FAILED" );96throw e;97}9899/*100* Dump the list of Loggers already present, if any101*/102Object[] params = new Object[1];103String[] signature = new String[1];104Level l;105106if ( list == null ) {107System.out.println(" : PASSED. No Standard Loggers Present" );108System.out.println("");109}110else {111System.out.println(" : PASSED. There are " + list.length + " Loggers Present" );112System.out.println("");113System.out.println( "*******************************" );114System.out.println( "*********** Phase 2B **********" );115System.out.println( "*******************************" );116System.out.println( " Examine Existing Loggers" );117for ( int i = 0; i < list.length; i++ ) {118try {119params[0] = list[i];120signature[0] = "java.lang.String";121String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );122System.out.println(" : Logger #" + i + " = " + list[i] );123System.out.println(" : Level = " + levelName );124}125catch ( Exception e ) {126System.out.println(" : FAILED" );127throw e;128}129}130System.out.println(" : PASSED" );131}132133/*134* Create two new loggers to the list of Loggers already present135*/136System.out.println("");137System.out.println( "*******************************" );138System.out.println( "*********** Phase 3 ***********" );139System.out.println( "*******************************" );140System.out.println( " Create and test new Loggers" );141logger1 = Logger.getLogger( LOGGER_NAME_1 );142logger2 = Logger.getLogger( LOGGER_NAME_2 );143144// check that Level object are returned properly145try {146list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );147}148catch ( Exception e ) {149System.out.println(" : FAILED" );150throw e;151}152153/*154* Check for the existence of our new Loggers155*/156boolean log1 = false, log2 = false;157158if ( list == null || list.length < 2 ) {159System.out.println(" : FAILED. Could not Detect the presense of the new Loggers" );160throw new RuntimeException(161"Could not Detect the presense of the new Loggers");162}163else {164for ( int i = 0; i < list.length; i++ ) {165if ( list[i].equals( LOGGER_NAME_1 ) ) {166log1 = true;167System.out.println( " : Found new Logger : " + list[i] );168}169if ( list[i].equals( LOGGER_NAME_2 ) ) {170log2 = true;171System.out.println( " : Found new Logger : " + list[i] );172}173}174if ( log1 && log2 )175System.out.println( " : PASSED." );176else {177System.out.println( " : FAILED. Could not Detect the new Loggers." );178throw new RuntimeException(179"Could not Detect the presense of the new Loggers");180}181}182183/*184* Set a new Logging levels and check that it succeeded185*/186System.out.println("");187System.out.println( "*******************************" );188System.out.println( "*********** Phase 4 ***********" );189System.out.println( "*******************************" );190System.out.println( " Set and Check the Logger Level" );191log1 = false;192log2 = false;193194try {195// Set the level of logger1 to ALL196params = new Object[2];197signature = new String[2];198params[0] = LOGGER_NAME_1;199params[1] = Level.ALL.getName();200signature[0] = "java.lang.String";201signature[1] = "java.lang.String";202mbs.invoke( objectName, "setLoggerLevel", params, signature );203204// Set the level of logger2 to FINER205params[0] = LOGGER_NAME_2;206params[1] = Level.FINER.getName();207mbs.invoke( objectName, "setLoggerLevel", params, signature );208209// Okay read back the Level from Logger1. Should be ALL210params = new Object[1];211signature = new String[1];212params[0] = LOGGER_NAME_1;213signature[0] = "java.lang.String";214String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );215l = Level.parse(levelName);216System.out.print(" Logger1: " );217if ( l.equals( l.ALL ) ) {218System.out.println("Level Set to ALL: PASSED" );219log1 = true;220}221else {222System.out.println("Level Set to ALL: FAILED" );223throw new RuntimeException(224"Level Set to ALL but returned " + l.toString());225}226227// Okay read back the Level from Logger2. Should be FINER228params = new Object[1];229signature = new String[1];230params[0] = LOGGER_NAME_2;231signature[0] = "java.lang.String";232levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );233l = Level.parse(levelName);234System.out.print(" Logger2: " );235if ( l.equals( l.FINER ) ) {236System.out.println("Level Set to FINER: PASSED" );237log2 = true;238}239else {240System.out.println("Level Set to FINER: FAILED" );241throw new RuntimeException(242"Level Set to FINER but returned " + l.toString());243}244}245catch ( Exception e ) {246throw e;247}248249System.out.println( "" );250System.out.println( "***************************************************" );251System.out.println( "***************** All Tests Passed ****************" );252System.out.println( "***************************************************" );253}254255public static void main(String[] argv) throws Exception {256PlatformLoggingMXBean mbean =257ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class);258ObjectName objname = mbean.getObjectName();259if (!objname.equals(new ObjectName(LogManager.LOGGING_MXBEAN_NAME))) {260throw new RuntimeException("Invalid ObjectName " + objname);261}262263// check if the PlatformLoggingMXBean is registered in the platform MBeanServer264MBeanServer platformMBS = ManagementFactory.getPlatformMBeanServer();265ObjectName objName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME);266267// We could call mbs.isRegistered(objName) here.268// Calling getMBeanInfo will throw exception if not found.269platformMBS.getMBeanInfo(objName);270271if (!platformMBS.isInstanceOf(objName, "java.lang.management.PlatformLoggingMXBean") ||272!platformMBS.isInstanceOf(objName, "java.util.logging.LoggingMXBean")) {273throw new RuntimeException(objName + " is of unexpected type");274}275276// test if PlatformLoggingMXBean works properly in a MBeanServer277PlatformLoggingMXBeanTest test = new PlatformLoggingMXBeanTest();278test.runTest(mbean);279}280}281282283