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