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