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