0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5007165
0N/A * @summary Basic Test for LoggingMXBean (direct access to MXBean)
0N/A * @author Mandy Chung
0N/A *
0N/A * @build LoggingMXBeanTest2
0N/A * @run main LoggingMXBeanTest2
0N/A */
0N/A
0N/Aimport java.util.logging.*;
0N/Aimport java.util.List;
0N/Aimport java.util.ListIterator;
0N/A
0N/Apublic class LoggingMXBeanTest2
0N/A{
0N/A
0N/A static LoggingMXBean mbean = LogManager.getLoggingMXBean();
0N/A static String LOGGER_NAME_1 = "com.sun.management.Logger";
0N/A static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
0N/A static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
0N/A
0N/A public LoggingMXBeanTest2() throws Exception {
0N/A
0N/A Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
0N/A logger1.setLevel(Level.FINE);
0N/A Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
0N/A logger2.setLevel(null);
0N/A
0N/A /*
0N/A * Check for the existence of our new Loggers
0N/A */
0N/A System.out.println("Test Logger Name retrieval (getLoggerNames)");
0N/A boolean log1 = false, log2 = false;
0N/A List loggers = mbean.getLoggerNames();
0N/A if (loggers == null || loggers.size() < 2) {
0N/A throw new RuntimeException(
0N/A "Could not Detect the presense of the new Loggers");
0N/A }
0N/A
0N/A for (ListIterator iter = loggers.listIterator(); iter.hasNext(); ) {
0N/A String logger = (String) iter.next();
0N/A if (logger.equals(LOGGER_NAME_1)) {
0N/A log1 = true;
0N/A System.out.println(" : Found new Logger : " + logger);
0N/A }
0N/A if (logger.equals(LOGGER_NAME_2)) {
0N/A log2 = true;
0N/A System.out.println(" : Found new Logger : " + logger);
0N/A }
0N/A }
0N/A if ( log1 && log2 )
0N/A System.out.println(" : PASSED." );
0N/A else {
0N/A System.out.println(" : FAILED. Could not Detect the new Loggers." );
0N/A throw new RuntimeException(
0N/A "Could not Detect the presense of the new Loggers");
0N/A }
0N/A
0N/A System.out.println("Test getLoggerLevel");
0N/A String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);
0N/A System.out.println(" : Level for Logger " + LOGGER_NAME_1 + " : " + l1);
0N/A if (!l1.equals(Level.FINE.getName())) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + LOGGER_NAME_1 + " = " +
0N/A Level.FINE.getName() + " but got " + l1);
0N/A }
0N/A String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);
0N/A System.out.println(" : Level for Logger " + LOGGER_NAME_2 + " : " + l2);
0N/A if (!l2.equals("")) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + LOGGER_NAME_2 + " = \"\"" +
0N/A " but got " + l2);
0N/A }
0N/A String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);
0N/A System.out.println(" : Level for unknown logger : " + l3);
0N/A if (l3 != null) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
0N/A " but got " + l3);
0N/A }
0N/A
0N/A System.out.println("Test setLoggerLevel");
0N/A mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");
0N/A System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");
0N/A Level l = logger1.getLevel();
0N/A if (l != Level.INFO) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + LOGGER_NAME_1 + " = " +
0N/A Level.INFO + " but got " + l);
0N/A }
0N/A
0N/A mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");
0N/A System.out.println(" : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");
0N/A l = logger2.getLevel();
0N/A if (l != Level.SEVERE) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + LOGGER_NAME_2 + " = " +
0N/A Level.SEVERE+ " but got " + l);
0N/A }
0N/A
0N/A mbean.setLoggerLevel(LOGGER_NAME_1, null);
0N/A System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: null");
0N/A l = logger1.getLevel();
0N/A if (l != null) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + LOGGER_NAME_1 + " = null " +
0N/A " but got " + l);
0N/A }
0N/A
0N/A boolean iaeCaught = false;
0N/A System.out.println(" : Set Level for unknown Logger to: FINE");
0N/A try {
0N/A mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");
0N/A } catch (IllegalArgumentException e) {
0N/A // expected
0N/A iaeCaught = true;
0N/A System.out.println(" : IllegalArgumentException caught as expected");
0N/A }
0N/A if (!iaeCaught) {
0N/A throw new RuntimeException(
0N/A "Expected IllegalArgumentException for setting level for " +
0N/A UNKNOWN_LOGGER_NAME + " not thrown");
0N/A }
0N/A iaeCaught = false;
0N/A System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");
0N/A try {
0N/A mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");
0N/A } catch (IllegalArgumentException e) {
0N/A // expected
0N/A iaeCaught = true;
0N/A System.out.println(" : IllegalArgumentException caught as expected");
0N/A }
0N/A if (!iaeCaught) {
0N/A throw new RuntimeException(
0N/A "Expected IllegalArgumentException for invalid level.");
0N/A }
0N/A
0N/A
0N/A System.out.println("Test getParentLoggerName");
0N/A String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);
0N/A System.out.println(" : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);
0N/A if (!p1.equals(LOGGER_NAME_1)) {
0N/A throw new RuntimeException(
0N/A "Expected parent for " + LOGGER_NAME_2 + " = " +
0N/A LOGGER_NAME_1 + " but got " + p1);
0N/A }
0N/A String p2 = mbean.getParentLoggerName("");
0N/A System.out.println(" : Parent Logger for \"\" : " + p2);
0N/A if (!p2.equals("")) {
0N/A throw new RuntimeException(
0N/A "Expected parent for root logger \"\" = \"\"" +
0N/A " but got " + p2);
0N/A }
0N/A String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);
0N/A System.out.println(" : Parent Logger for unknown logger : " + p3);
0N/A if (p3 != null) {
0N/A throw new RuntimeException(
0N/A "Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +
0N/A " but got " + p3);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] argv) throws Exception {
0N/A LoggingMXBeanTest2 p = new LoggingMXBeanTest2();
0N/A }
0N/A}