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