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