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