1 /*
   2  * Copyright (c) 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     5007165
  27  * @summary Basic Test for LoggingMXBean (direct access to MXBean)
  28  * @author  Mandy Chung
  29  *
  30  * @build LoggingMXBeanTest2
  31  * @run main LoggingMXBeanTest2
  32  */
  33 
  34 import java.util.logging.*;
  35 import java.util.List;
  36 import java.util.ListIterator;
  37 
  38 public class LoggingMXBeanTest2
  39 {
  40 
  41     static LoggingMXBean mbean = LogManager.getLoggingMXBean();
  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 LoggingMXBeanTest2() throws Exception {
  47 
  48         Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
  49         logger1.setLevel(Level.FINE);
  50         Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
  51         logger2.setLevel(null);
  52 
  53         /*
  54          *  Check for the existence of our new Loggers
  55          */
  56         System.out.println("Test Logger Name retrieval (getLoggerNames)");
  57         boolean log1 = false, log2 = false;
  58         List loggers = mbean.getLoggerNames();
  59         if (loggers == null || loggers.size() < 2) {
  60             throw new RuntimeException(
  61                 "Could not Detect the presense of the new Loggers");
  62         }
  63 
  64         for (ListIterator iter = loggers.listIterator(); iter.hasNext(); ) {
  65             String logger = (String) iter.next();
  66             if (logger.equals(LOGGER_NAME_1)) {
  67                 log1 = true;
  68                 System.out.println("  : Found new Logger : " + logger);
  69             }
  70             if (logger.equals(LOGGER_NAME_2)) {
  71                 log2 = true;
  72                 System.out.println("  : Found new Logger : " + logger);
  73             }
  74         }
  75         if ( log1 && log2 )
  76             System.out.println("  : PASSED." );
  77         else {
  78             System.out.println("  : FAILED.  Could not Detect the new Loggers." );
  79             throw new RuntimeException(
  80                 "Could not Detect the presense of the new Loggers");
  81         }
  82 
  83         System.out.println("Test getLoggerLevel");
  84         String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);
  85         System.out.println("  : Level for Logger " + LOGGER_NAME_1 + " : " + l1);
  86         if (!l1.equals(Level.FINE.getName())) {
  87             throw new RuntimeException(
  88                 "Expected level for " + LOGGER_NAME_1 + " = " +
  89                  Level.FINE.getName() + " but got " + l1);
  90         }
  91         String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);
  92         System.out.println("  : Level for Logger " + LOGGER_NAME_2 + " : " + l2);
  93         if (!l2.equals("")) {
  94             throw new RuntimeException(
  95                 "Expected level for " + LOGGER_NAME_2 + " = \"\"" +
  96                  " but got " + l2);
  97         }
  98         String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);
  99         System.out.println("  : Level for unknown logger : " + l3);
 100         if (l3 != null) {
 101             throw new RuntimeException(
 102                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 103                  " but got " + l3);
 104         }
 105 
 106         System.out.println("Test setLoggerLevel");
 107         mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");
 108         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");
 109         Level l = logger1.getLevel();
 110         if (l != Level.INFO) {
 111             throw new RuntimeException(
 112                 "Expected level for " + LOGGER_NAME_1 + " = " +
 113                  Level.INFO + " but got " + l);
 114         }
 115 
 116         mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");
 117         System.out.println("  : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");
 118         l = logger2.getLevel();
 119         if (l != Level.SEVERE) {
 120             throw new RuntimeException(
 121                 "Expected level for " + LOGGER_NAME_2 + " = " +
 122                  Level.SEVERE+ " but got " + l);
 123         }
 124 
 125         mbean.setLoggerLevel(LOGGER_NAME_1, null);
 126         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: null");
 127         l = logger1.getLevel();
 128         if (l != null) {
 129             throw new RuntimeException(
 130                 "Expected level for " + LOGGER_NAME_1 + " = null " +
 131                  " but got " + l);
 132         }
 133 
 134         boolean iaeCaught = false;
 135         System.out.println("  : Set Level for unknown Logger to: FINE");
 136         try {
 137             mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");
 138         } catch (IllegalArgumentException e) {
 139             // expected
 140             iaeCaught = true;
 141             System.out.println("      : IllegalArgumentException caught as expected");
 142         }
 143         if (!iaeCaught) {
 144             throw new RuntimeException(
 145                 "Expected IllegalArgumentException for setting level for " +
 146                 UNKNOWN_LOGGER_NAME + " not thrown");
 147         }
 148         iaeCaught = false;
 149         System.out.println("  : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");
 150         try {
 151             mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");
 152         } catch (IllegalArgumentException e) {
 153             // expected
 154             iaeCaught = true;
 155             System.out.println("      : IllegalArgumentException caught as expected");
 156         }
 157         if (!iaeCaught) {
 158             throw new RuntimeException(
 159                 "Expected IllegalArgumentException for invalid level.");
 160         }
 161 
 162 
 163         System.out.println("Test getParentLoggerName");
 164         String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);
 165         System.out.println("  : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);
 166         if (!p1.equals(LOGGER_NAME_1)) {
 167             throw new RuntimeException(
 168                 "Expected parent for " + LOGGER_NAME_2 + " = " +
 169                 LOGGER_NAME_1 + " but got " + p1);
 170         }
 171         String p2 = mbean.getParentLoggerName("");
 172         System.out.println("  : Parent Logger for \"\" : " + p2);
 173         if (!p2.equals("")) {
 174             throw new RuntimeException(
 175                 "Expected parent for root logger \"\" = \"\"" +
 176                 " but got " + p2);
 177         }
 178         String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
 179         System.out.println("  : Parent Logger for unknown logger : " + p3);
 180         if (p3 != null) {
 181             throw new RuntimeException(
 182                 "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
 183                  " but got " + p3);
 184         }
 185     }
 186 
 187     public static void main(String[] argv) throws Exception {
 188         LoggingMXBeanTest2 p = new LoggingMXBeanTest2();
 189     }
 190 }