1 /*
   2  * Copyright (c) 2011, 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 7024172
  27  * @summary Test if proxy for PlatformLoggingMXBean is equivalent
  28  *          to proxy for LoggingMXBean
  29  *
  30  * @build LoggingMXBeanTest
  31  * @run main LoggingMXBeanTest
  32  */
  33 
  34 import java.lang.management.*;
  35 import javax.management.MBeanServer;
  36 import java.util.logging.*;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 public class LoggingMXBeanTest
  41 {
  42     static String LOGGER_NAME_1 = "com.sun.management.Logger";
  43     static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
  44     static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
  45 
  46     public static void main(String[] argv) throws Exception {
  47         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  48         LoggingMXBean proxy =
  49             ManagementFactory.newPlatformMXBeanProxy(mbs,
  50                 LogManager.LOGGING_MXBEAN_NAME,
  51                 LoggingMXBean.class);
  52 
  53         // test LoggingMXBean proxy
  54         LoggingMXBeanTest p = new LoggingMXBeanTest(proxy);
  55 
  56         // check if the attributes implemented by PlatformLoggingMXBean
  57         // and LoggingMXBean return the same value
  58         PlatformLoggingMXBean mxbean =
  59             ManagementFactory.getPlatformMXBean(mbs, PlatformLoggingMXBean.class);
  60 
  61         checkAttributes(proxy, mxbean);
  62     }
  63 
  64     // same verification as in java/util/logging/LoggingMXBeanTest2
  65     public LoggingMXBeanTest(LoggingMXBean mbean) throws Exception {
  66 
  67         Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
  68         logger1.setLevel(Level.FINE);
  69         Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
  70         logger2.setLevel(null);
  71 
  72         /*
  73          *  Check for the existence of our new Loggers
  74          */
  75         System.out.println("Test Logger Name retrieval (getLoggerNames)");
  76         boolean log1 = false, log2 = false;
  77         List<String> loggers = mbean.getLoggerNames();
  78         if (loggers == null || loggers.size() < 2) {
  79             throw new RuntimeException(
  80                 "Could not Detect the presense of the new Loggers");
  81         }
  82 
  83         for (String logger : loggers) {
  84             if (logger.equals(LOGGER_NAME_1)) {
  85                 log1 = true;
  86                 System.out.println("  : Found new Logger : " + logger);
  87             }
  88             if (logger.equals(LOGGER_NAME_2)) {
  89                 log2 = true;
  90                 System.out.println("  : Found new Logger : " + logger);
  91             }
  92         }
  93         if ( log1 && log2 )
  94             System.out.println("  : PASSED." );
  95         else {
  96             System.out.println("  : FAILED.  Could not Detect the new Loggers." );
  97             throw new RuntimeException(
  98                 "Could not Detect the presense of the new Loggers");
  99         }
 100 
 101         System.out.println("Test getLoggerLevel");
 102         String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);
 103         System.out.println("  : Level for Logger " + LOGGER_NAME_1 + " : " + l1);
 104         if (!l1.equals(Level.FINE.getName())) {
 105             throw new RuntimeException(
 106                 "Expected level for " + LOGGER_NAME_1 + " = " +
 107                  Level.FINE.getName() + " but got " + l1);
 108         }
 109         String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);
 110         System.out.println("  : Level for Logger " + LOGGER_NAME_2 + " : " + l2);
 111         if (!l2.equals("")) {
 112             throw new RuntimeException(
 113                 "Expected level for " + LOGGER_NAME_2 + " = \"\"" +
 114                  " but got " + l2);
 115         }
 116         String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);
 117         System.out.println("  : Level for unknown logger : " + l3);
 118         if (l3 != null) {
 119             throw new RuntimeException(
 120                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 121                  " but got " + l3);
 122         }
 123 
 124         System.out.println("Test setLoggerLevel");
 125         mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");
 126         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");
 127         Level l = logger1.getLevel();
 128         if (l != Level.INFO) {
 129             throw new RuntimeException(
 130                 "Expected level for " + LOGGER_NAME_1 + " = " +
 131                  Level.INFO + " but got " + l);
 132         }
 133 
 134         mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");
 135         System.out.println("  : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");
 136         l = logger2.getLevel();
 137         if (l != Level.SEVERE) {
 138             throw new RuntimeException(
 139                 "Expected level for " + LOGGER_NAME_2 + " = " +
 140                  Level.SEVERE+ " but got " + l);
 141         }
 142 
 143         mbean.setLoggerLevel(LOGGER_NAME_1, null);
 144         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: null");
 145         l = logger1.getLevel();
 146         if (l != null) {
 147             throw new RuntimeException(
 148                 "Expected level for " + LOGGER_NAME_1 + " = null " +
 149                  " but got " + l);
 150         }
 151 
 152         boolean iaeCaught = false;
 153         System.out.println("  : Set Level for unknown Logger to: FINE");
 154         try {
 155             mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");
 156         } catch (IllegalArgumentException e) {
 157             // expected
 158             iaeCaught = true;
 159             System.out.println("      : IllegalArgumentException caught as expected");
 160         }
 161         if (!iaeCaught) {
 162             throw new RuntimeException(
 163                 "Expected IllegalArgumentException for setting level for " +
 164                 UNKNOWN_LOGGER_NAME + " not thrown");
 165         }
 166         iaeCaught = false;
 167         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");
 168         try {
 169             mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");
 170         } catch (IllegalArgumentException e) {
 171             // expected
 172             iaeCaught = true;
 173             System.out.println("      : IllegalArgumentException caught as expected");
 174         }
 175         if (!iaeCaught) {
 176             throw new RuntimeException(
 177                 "Expected IllegalArgumentException for invalid level.");
 178         }
 179 
 180 
 181         System.out.println("Test getParentLoggerName");
 182         String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);
 183         System.out.println("  : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);
 184         if (!p1.equals(LOGGER_NAME_1)) {
 185             throw new RuntimeException(
 186                 "Expected parent for " + LOGGER_NAME_2 + " = " +
 187                 LOGGER_NAME_1 + " but got " + p1);
 188         }
 189         String p2 = mbean.getParentLoggerName("");
 190         System.out.println("  : Parent Logger for \"\" : " + p2);
 191         if (!p2.equals("")) {
 192             throw new RuntimeException(
 193                 "Expected parent for root logger \"\" = \"\"" +
 194                 " but got " + p2);
 195         }
 196         String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
 197         System.out.println("  : Parent Logger for unknown logger : " + p3);
 198         if (p3 != null) {
 199             throw new RuntimeException(
 200                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 201                  " but got " + p3);
 202         }
 203     }
 204 
 205     private static void checkAttributes(LoggingMXBean mxbean1,
 206                                         PlatformLoggingMXBean mxbean2) {
 207         // verify logger names
 208         List<String> loggers1 = mxbean1.getLoggerNames();
 209         List<String> loggers2 = mxbean2.getLoggerNames();
 210         if (loggers1.size() != loggers2.size())
 211             throw new RuntimeException("LoggerNames: unmatched number of entries");
 212         List<String> loggers3 = new ArrayList<>(loggers1);
 213         loggers3.removeAll(loggers2);
 214         if (loggers3.size() != 0)
 215             throw new RuntimeException("LoggerNames: unmatched loggers");
 216 
 217         // verify logger's level  and parent
 218         for (String logger : loggers1) {
 219             if (!mxbean1.getLoggerLevel(logger)
 220                     .equals(mxbean2.getLoggerLevel(logger)))
 221                 throw new RuntimeException(
 222                     "LoggerLevel: unmatched level for " + logger);
 223             if (!mxbean1.getParentLoggerName(logger)
 224                     .equals(mxbean2.getParentLoggerName(logger)))
 225                 throw new RuntimeException(
 226                     "ParentLoggerName: unmatched parent logger's name for " + logger);
 227         }
 228     }
 229 }