Path: blob/master/test/jdk/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java
41155 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 7024172 706769126* @summary Test if proxy for PlatformLoggingMXBean is equivalent27* to proxy for LoggingMXBean28*29* @build LoggingMXBeanTest30* @run main LoggingMXBeanTest31*/3233import java.lang.management.*;34import javax.management.MBeanServer;35import java.util.logging.*;36import java.util.ArrayList;37import java.util.List;38import java.util.Map;39import java.util.HashMap;4041public class LoggingMXBeanTest42{43static final String LOGGER_NAME_1 = "com.sun.management.Logger";44static final String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";45static final String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";4647// These instance variables prevent premature logger garbage collection48// See getLogger() weak reference warnings.49Logger logger1;50Logger logger2;5152static LoggingMXBeanTest test;5354public static void main(String[] argv) throws Exception {55MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();56LoggingMXBean proxy =57ManagementFactory.newPlatformMXBeanProxy(mbs,58LogManager.LOGGING_MXBEAN_NAME,59LoggingMXBean.class);6061// test LoggingMXBean proxy62test = new LoggingMXBeanTest(proxy);6364// check if the attributes implemented by PlatformLoggingMXBean65// and LoggingMXBean return the same value66PlatformLoggingMXBean mxbean =67ManagementFactory.getPlatformMXBean(mbs, PlatformLoggingMXBean.class);6869checkAttributes(proxy, mxbean);70}7172// same verification as in java/util/logging/LoggingMXBeanTest273public LoggingMXBeanTest(LoggingMXBean mbean) throws Exception {7475logger1 = Logger.getLogger( LOGGER_NAME_1 );76logger1.setLevel(Level.FINE);77logger2 = Logger.getLogger( LOGGER_NAME_2 );78logger2.setLevel(null);7980/*81* Check for the existence of our new Loggers82*/83System.out.println("Test Logger Name retrieval (getLoggerNames)");84boolean log1 = false, log2 = false;85List<String> loggers = mbean.getLoggerNames();86if (loggers == null || loggers.size() < 2) {87throw new RuntimeException(88"Could not Detect the presense of the new Loggers");89}9091for (String logger : loggers) {92if (logger.equals(LOGGER_NAME_1)) {93log1 = true;94System.out.println(" : Found new Logger : " + logger);95}96if (logger.equals(LOGGER_NAME_2)) {97log2 = true;98System.out.println(" : Found new Logger : " + logger);99}100}101if ( log1 && log2 )102System.out.println(" : PASSED." );103else {104System.out.println(" : FAILED. Could not Detect the new Loggers." );105throw new RuntimeException(106"Could not Detect the presense of the new Loggers");107}108109System.out.println("Test getLoggerLevel");110String l1 = mbean.getLoggerLevel(LOGGER_NAME_1);111System.out.println(" : Level for Logger " + LOGGER_NAME_1 + " : " + l1);112if (!l1.equals(Level.FINE.getName())) {113throw new RuntimeException(114"Expected level for " + LOGGER_NAME_1 + " = " +115Level.FINE.getName() + " but got " + l1);116}117String l2 = mbean.getLoggerLevel(LOGGER_NAME_2);118System.out.println(" : Level for Logger " + LOGGER_NAME_2 + " : " + l2);119if (!l2.equals("")) {120throw new RuntimeException(121"Expected level for " + LOGGER_NAME_2 + " = \"\"" +122" but got " + l2);123}124String l3 = mbean.getLoggerLevel(UNKNOWN_LOGGER_NAME);125System.out.println(" : Level for unknown logger : " + l3);126if (l3 != null) {127throw new RuntimeException(128"Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +129" but got " + l3);130}131132System.out.println("Test setLoggerLevel");133mbean.setLoggerLevel(LOGGER_NAME_1, "INFO");134System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: INFO");135Level l = logger1.getLevel();136if (l != Level.INFO) {137throw new RuntimeException(138"Expected level for " + LOGGER_NAME_1 + " = " +139Level.INFO + " but got " + l);140}141142mbean.setLoggerLevel(LOGGER_NAME_2, "SEVERE");143System.out.println(" : Set Level for Logger " + LOGGER_NAME_2 + " to: SERVER");144l = logger2.getLevel();145if (l != Level.SEVERE) {146throw new RuntimeException(147"Expected level for " + LOGGER_NAME_2 + " = " +148Level.SEVERE+ " but got " + l);149}150151mbean.setLoggerLevel(LOGGER_NAME_1, null);152System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: null");153l = logger1.getLevel();154if (l != null) {155throw new RuntimeException(156"Expected level for " + LOGGER_NAME_1 + " = null " +157" but got " + l);158}159160boolean iaeCaught = false;161System.out.println(" : Set Level for unknown Logger to: FINE");162try {163mbean.setLoggerLevel(UNKNOWN_LOGGER_NAME, "FINE");164} catch (IllegalArgumentException e) {165// expected166iaeCaught = true;167System.out.println(" : IllegalArgumentException caught as expected");168}169if (!iaeCaught) {170throw new RuntimeException(171"Expected IllegalArgumentException for setting level for " +172UNKNOWN_LOGGER_NAME + " not thrown");173}174iaeCaught = false;175System.out.println(" : Set Level for Logger " + LOGGER_NAME_1 + " to: DUMMY");176try {177mbean.setLoggerLevel(LOGGER_NAME_1, "DUMMY");178} catch (IllegalArgumentException e) {179// expected180iaeCaught = true;181System.out.println(" : IllegalArgumentException caught as expected");182}183if (!iaeCaught) {184throw new RuntimeException(185"Expected IllegalArgumentException for invalid level.");186}187188189System.out.println("Test getParentLoggerName");190String p1 = mbean.getParentLoggerName(LOGGER_NAME_2);191System.out.println(" : Parent Logger for " + LOGGER_NAME_2 + " : " + p1);192if (!p1.equals(LOGGER_NAME_1)) {193throw new RuntimeException(194"Expected parent for " + LOGGER_NAME_2 + " = " +195LOGGER_NAME_1 + " but got " + p1);196}197String p2 = mbean.getParentLoggerName("");198System.out.println(" : Parent Logger for \"\" : " + p2);199if (!p2.equals("")) {200throw new RuntimeException(201"Expected parent for root logger \"\" = \"\"" +202" but got " + p2);203}204String p3 = mbean.getParentLoggerName(UNKNOWN_LOGGER_NAME);205System.out.println(" : Parent Logger for unknown logger : " + p3);206if (p3 != null) {207throw new RuntimeException(208"Expected level for " + UNKNOWN_LOGGER_NAME + " = null" +209" but got " + p3);210}211}212213private static void checkAttributes(LoggingMXBean mxbean1,214PlatformLoggingMXBean mxbean2) {215// verify logger names216List<String> loggers1 = mxbean1.getLoggerNames();217System.out.println("Loggers: " + loggers1);218219// Retrieve the named loggers to prevent them from being220// spontaneously gc'ed.221Map<String, Logger> loggersMap = new HashMap<>();222for (String n : loggers1) {223loggersMap.put(n, Logger.getLogger(n));224}225226List<String> loggers2 = mxbean2.getLoggerNames();227228// loggers1 and loggers2 should be identical - no new logger should229// have been created in between (at least no new logger name)230//231if (loggers1.size() != loggers2.size())232throw new RuntimeException("LoggerNames: unmatched number of entries");233if (!loggers2.containsAll(loggersMap.keySet()))234throw new RuntimeException("LoggerNames: unmatched loggers");235236237// verify logger's level and parent238for (String logger : loggers1) {239String level1 = mxbean1.getLoggerLevel(logger);240String level2 = mxbean2.getLoggerLevel(logger);241if (!java.util.Objects.equals(level1, level2)) {242throw new RuntimeException(243"LoggerLevel: unmatched level for " + logger244+ ", " + level1 + ", " + level2);245}246247if (!mxbean1.getParentLoggerName(logger)248.equals(mxbean2.getParentLoggerName(logger)))249throw new RuntimeException(250"ParentLoggerName: unmatched parent logger's name for " + logger);251}252}253}254255256