Path: blob/master/test/jdk/sun/management/LoggingTest/LoggingTest.java
41149 views
/*1* Copyright (c) 2017, 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*/2223import java.io.ByteArrayOutputStream;24import java.io.IOException;25import java.io.PrintStream;26import java.lang.management.ManagementFactory;27import java.util.List;28import javax.management.MBeanServer;29import javax.management.ObjectName;3031public class LoggingTest {3233static class TestStream extends PrintStream {34final ByteArrayOutputStream bos = new ByteArrayOutputStream();35private volatile boolean recording;36public TestStream(PrintStream wrapped) {37super(wrapped);38}3940void startRecording() {41recording = true;42}4344void stopRecording() {45recording = false;46}4748@Override49public void write(int b) {50if (recording) {51bos.write(b);52}53super.write(b);54}5556@Override57public void write(byte[] buf, int off, int len) {58if (recording) {59bos.write(buf, off, len);60}61super.write(buf, off, len);62}6364@Override65public void write(byte[] buf) throws IOException {66if (recording) {67bos.write(buf);68}69super.write(buf);70}7172}7374public void run(TestStream ts) {7576// start recording traces and trigger creation of the platform77// MBeanServer to produce some. This won't work if the platform78// MBeanServer was already initialized - so it's important to79// run this test in its own JVM.80ts.startRecording();81MBeanServer platform = ManagementFactory.getPlatformMBeanServer();82ts.stopRecording();83String printed = ts.bos.toString();84ts.bos.reset();8586// Check that the Platform MBeanServer is emitting the expected87// log traces. This can be a bit fragile because debug traces88// could be changed without notice - in which case this test will89// need to be updated.90// For each registered MBean we expect to see three traces.91// If the messages logged by the MBeanServer change then these checks92// may need to be revisited.93List<String> checkTraces =94List.of("ObjectName = %s", "name = %s", "JMX.mbean.registered %s");9596for (ObjectName o : platform.queryNames(ObjectName.WILDCARD, null)) {97String n = o.toString();98System.out.println("Checking log for: " + n);99for (String check : checkTraces) {100String s = String.format(check, n);101if (!printed.contains(s)) {102throw new RuntimeException("Trace not found: " + s);103}104}105}106}107108}109110111