1 /* 2 * Copyright (c) 2017, 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 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 import java.io.PrintStream; 27 import java.lang.management.ManagementFactory; 28 import java.util.List; 29 import javax.management.MBeanServer; 30 import javax.management.ObjectName; 31 32 public class LoggingTest { 33 34 static class TestStream extends PrintStream { 35 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 36 private volatile boolean recording; TestStream(PrintStream wrapped)37 public TestStream(PrintStream wrapped) { 38 super(wrapped); 39 } 40 startRecording()41 void startRecording() { 42 recording = true; 43 } 44 stopRecording()45 void stopRecording() { 46 recording = false; 47 } 48 49 @Override write(int b)50 public void write(int b) { 51 if (recording) { 52 bos.write(b); 53 } 54 super.write(b); 55 } 56 57 @Override write(byte[] buf, int off, int len)58 public void write(byte[] buf, int off, int len) { 59 if (recording) { 60 bos.write(buf, off, len); 61 } 62 super.write(buf, off, len); 63 } 64 65 @Override write(byte[] buf)66 public void write(byte[] buf) throws IOException { 67 if (recording) { 68 bos.write(buf); 69 } 70 super.write(buf); 71 } 72 73 } 74 run(TestStream ts)75 public void run(TestStream ts) { 76 77 // start recording traces and trigger creation of the platform 78 // MBeanServer to produce some. This won't work if the platform 79 // MBeanServer was already initialized - so it's important to 80 // run this test in its own JVM. 81 ts.startRecording(); 82 MBeanServer platform = ManagementFactory.getPlatformMBeanServer(); 83 ts.stopRecording(); 84 String printed = ts.bos.toString(); 85 ts.bos.reset(); 86 87 // Check that the Platform MBeanServer is emitting the expected 88 // log traces. This can be a bit fragile because debug traces 89 // could be changed without notice - in which case this test will 90 // need to be updated. 91 // For each registered MBean we expect to see three traces. 92 // If the messages logged by the MBeanServer change then these checks 93 // may need to be revisited. 94 List<String> checkTraces = 95 List.of("ObjectName = %s", "name = %s", "JMX.mbean.registered %s"); 96 97 for (ObjectName o : platform.queryNames(ObjectName.WILDCARD, null)) { 98 String n = o.toString(); 99 System.out.println("Checking log for: " + n); 100 for (String check : checkTraces) { 101 String s = String.format(check, n); 102 if (!printed.contains(s)) { 103 throw new RuntimeException("Trace not found: " + s); 104 } 105 } 106 } 107 } 108 109 } 110