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 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 // 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 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 // verify logger's level and parent 226 for (String logger : loggers1) { 227 if (!mxbean1.getLoggerLevel(logger) 228 .equals(mxbean2.getLoggerLevel(logger))) 229 throw new RuntimeException( 230 "LoggerLevel: unmatched level for " + logger 231 + ", " + mxbean1.getLoggerLevel(logger) 232 + ", " + mxbean2.getLoggerLevel(logger)); 233 234 if (!mxbean1.getParentLoggerName(logger) 235 .equals(mxbean2.getParentLoggerName(logger))) 236 throw new RuntimeException( 237 "ParentLoggerName: unmatched parent logger's name for " + logger); 238 } 239 } 240 } | 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 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 } |