0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5007165
0N/A *
0N/A * @summary Basic Test for LoggingMXBean via MBeanServer
0N/A * @author Ron Mann
0N/A *
0N/A * @build LoggingMXBeanTest
0N/A * @run main LoggingMXBeanTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/Aimport java.util.logging.*;
0N/A
0N/A
0N/Apublic class LoggingMXBeanTest
0N/A{
0N/A
0N/A LoggingMXBean mBean;
0N/A ObjectName objectName = null;
0N/A static String LOGGER_NAME_1 = "com.sun.management.Logger1";
0N/A static String LOGGER_NAME_2 = "com.sun.management.Logger2";
0N/A
0N/A public LoggingMXBeanTest() throws Exception {
0N/A
0N/A /*
0N/A * Create the MBeanServeri, register the LoggingMXBean
0N/A */
0N/A System.out.println( "***************************************************" );
0N/A System.out.println( "********** LoggingMXBean Unit Test **********" );
0N/A System.out.println( "***************************************************" );
0N/A System.out.println( "" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( "*********** Phase 1 ***********" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( " Creating MBeanServer " );
0N/A System.out.print( " Register LoggingMXBean: " );
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A String[] list = new String[0];
0N/A
0N/A try {
0N/A objectName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME);
0N/A mBean = LogManager.getLoggingMXBean();
0N/A mbs.registerMBean( mBean, objectName );
0N/A }
0N/A catch ( Exception e ) {
0N/A System.out.println( "FAILED" );
0N/A throw e;
0N/A }
0N/A System.out.println( "PASSED" );
0N/A System.out.println("");
0N/A
0N/A /*
0N/A * Access our MBean to get the current list of Loggers
0N/A */
0N/A System.out.println( "*******************************" );
0N/A System.out.println( "*********** Phase 2 ***********" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( " Test Logger Name retrieval (getLoggerNames) " );
0N/A // check that Level object are returned properly
0N/A try {
0N/A list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );
0N/A }
0N/A catch ( Exception e ) {
0N/A System.out.println(" : FAILED" );
0N/A throw e;
0N/A }
0N/A
0N/A /*
0N/A * Dump the list of Loggers already present, if any
0N/A */
0N/A Object[] params = new Object[1];
0N/A String[] signature = new String[1];
0N/A Level l;
0N/A
0N/A if ( list == null ) {
0N/A System.out.println(" : PASSED. No Standard Loggers Present" );
0N/A System.out.println("");
0N/A }
0N/A else {
0N/A System.out.println(" : PASSED. There are " + list.length + " Loggers Present" );
0N/A System.out.println("");
0N/A System.out.println( "*******************************" );
0N/A System.out.println( "*********** Phase 2B **********" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( " Examine Existing Loggers" );
0N/A for ( int i = 0; i < list.length; i++ ) {
0N/A try {
0N/A params[0] = list[i];
0N/A signature[0] = "java.lang.String";
0N/A String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );
0N/A System.out.println(" : Logger #" + i + " = " + list[i] );
0N/A System.out.println(" : Level = " + levelName );
0N/A }
0N/A catch ( Exception e ) {
0N/A System.out.println(" : FAILED" );
0N/A throw e;
0N/A }
0N/A }
0N/A System.out.println(" : PASSED" );
0N/A }
0N/A
0N/A /*
0N/A * Create two new loggers to the list of Loggers already present
0N/A */
0N/A System.out.println("");
0N/A System.out.println( "*******************************" );
0N/A System.out.println( "*********** Phase 3 ***********" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( " Create and test new Loggers" );
0N/A Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
0N/A Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
0N/A
0N/A // check that Level object are returned properly
0N/A try {
0N/A list = (String[]) mbs.getAttribute( objectName, "LoggerNames" );
0N/A }
0N/A catch ( Exception e ) {
0N/A System.out.println(" : FAILED" );
0N/A throw e;
0N/A }
0N/A
0N/A /*
0N/A * Check for the existence of our new Loggers
0N/A */
0N/A boolean log1 = false, log2 = false;
0N/A
0N/A if ( list == null || list.length < 2 ) {
0N/A System.out.println(" : FAILED. Could not Detect the presense of the new Loggers" );
0N/A throw new RuntimeException(
0N/A "Could not Detect the presense of the new Loggers");
0N/A }
0N/A else {
0N/A for ( int i = 0; i < list.length; i++ ) {
0N/A if ( list[i].equals( LOGGER_NAME_1 ) ) {
0N/A log1 = true;
0N/A System.out.println( " : Found new Logger : " + list[i] );
0N/A }
0N/A if ( list[i].equals( LOGGER_NAME_2 ) ) {
0N/A log2 = true;
0N/A System.out.println( " : Found new Logger : " + list[i] );
0N/A }
0N/A }
0N/A if ( log1 && log2 )
0N/A System.out.println( " : PASSED." );
0N/A else {
0N/A System.out.println( " : FAILED. Could not Detect the new Loggers." );
0N/A throw new RuntimeException(
0N/A "Could not Detect the presense of the new Loggers");
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Set a new Logging levels and check that it succeeded
0N/A */
0N/A System.out.println("");
0N/A System.out.println( "*******************************" );
0N/A System.out.println( "*********** Phase 4 ***********" );
0N/A System.out.println( "*******************************" );
0N/A System.out.println( " Set and Check the Logger Level" );
0N/A log1 = false;
0N/A log2 = false;
0N/A try {
0N/A // Set the level of logger1 to ALL
0N/A params = new Object[2];
0N/A signature = new String[2];
0N/A params[0] = LOGGER_NAME_1;
0N/A params[1] = Level.ALL.getName();
0N/A signature[0] = "java.lang.String";
0N/A signature[1] = "java.lang.String";
0N/A mbs.invoke( objectName, "setLoggerLevel", params, signature );
0N/A
0N/A // Set the level of logger2 to FINER
0N/A params[0] = LOGGER_NAME_2;
0N/A params[1] = Level.FINER.getName();
0N/A mbs.invoke( objectName, "setLoggerLevel", params, signature );
0N/A
0N/A // Okay read back the Level from Logger1. Should be ALL
0N/A params = new Object[1];
0N/A signature = new String[1];
0N/A params[0] = LOGGER_NAME_1;
0N/A signature[0] = "java.lang.String";
0N/A String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );
0N/A l = Level.parse(levelName);
0N/A System.out.print(" Logger1: " );
0N/A if ( l.equals( l.ALL ) ) {
0N/A System.out.println("Level Set to ALL: PASSED" );
0N/A log1 = true;
0N/A }
0N/A else {
0N/A System.out.println("Level Set to ALL: FAILED" );
0N/A throw new RuntimeException(
0N/A "Level Set to ALL but returned " + l.toString());
0N/A }
0N/A
0N/A // Okay read back the Level from Logger2. Should be FINER
0N/A params = new Object[1];
0N/A signature = new String[1];
0N/A params[0] = LOGGER_NAME_2;
0N/A signature[0] = "java.lang.String";
0N/A levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature );
0N/A l = Level.parse(levelName);
0N/A System.out.print(" Logger2: " );
0N/A if ( l.equals( l.FINER ) ) {
0N/A System.out.println("Level Set to FINER: PASSED" );
0N/A log2 = true;
0N/A }
0N/A else {
0N/A System.out.println("Level Set to FINER: FAILED" );
0N/A throw new RuntimeException(
0N/A "Level Set to FINER but returned " + l.toString());
0N/A }
0N/A }
0N/A catch ( Exception e ) {
0N/A throw e;
0N/A }
0N/A
0N/A System.out.println( "" );
0N/A System.out.println( "***************************************************" );
0N/A System.out.println( "***************** All Tests Passed ****************" );
0N/A System.out.println( "***************************************************" );
0N/A }
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A LoggingMXBeanTest p = new LoggingMXBeanTest();
0N/A }
0N/A}