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