Path: blob/master/test/jdk/sun/management/jmxremote/startstop/JMXStatusTest.java
41153 views
/*1* Copyright (c) 2015, 2018, 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.net.BindException;24import java.util.ArrayList;25import java.util.List;26import java.util.function.Predicate;27import java.util.regex.Pattern;28import org.testng.annotations.*;29import static org.testng.Assert.*;3031import jdk.test.lib.process.ProcessTools;3233/**34* @test35* @bug 8023093 8138748 814239836* @summary Performs a sanity test for the ManagementAgent.status diagnostic command.37* Management agent may be disabled, started (only local connections) and started.38* The test asserts that the expected text is being printed.39*40* @library /test/lib41*42* @build PortAllocator TestApp ManagementAgentJcmd43* JMXStatusTest JMXStatus1Test JMXStatus2Test44* @run testng/othervm -XX:+UsePerfData JMXStatus1Test45* @run testng/othervm -XX:+UsePerfData JMXStatus2Test46*/47abstract public class JMXStatusTest {48private final static String TEST_APP_NAME = "TestApp";4950protected final static Pattern DISABLED_AGENT_STATUS = Pattern.compile(51"Agent\\s*\\: disabled$"52);5354protected final static Pattern LOCAL_AGENT_STATUS = Pattern.compile(55"Agent\\s*\\:\\s*enabled\\n+" +56"Connection Type\\s*\\:\\s*local\\n+" +57"Protocol\\s*\\:\\s*[a-z]+\\n+" +58"Host\\s*\\:\\s*.+\\n+" +59"URL\\s*\\:\\s*service\\:jmx\\:.+\\n+" +60"Properties\\s*\\:\\n+(\\s*\\S+\\s*=\\s*\\S+(\\s+\\[default\\])?\\n*)+",61Pattern.MULTILINE62);6364protected final static Pattern REMOTE_AGENT_STATUS = Pattern.compile(65"Agent\\s*\\: enabled\\n+" +66".*" +67"Connection Type\\s*\\: remote\\n+" +68"Protocol\\s*\\: [a-z]+\\n+" +69"Host\\s*\\: .+\\n+" +70"URL\\s*\\: service\\:jmx\\:.+\\n+" +71"Properties\\s*\\:\\n+(\\s*\\S+\\s*=\\s*\\S+(\\s+\\[default\\])?\\n*)+",72Pattern.MULTILINE | Pattern.DOTALL73);7475private static ProcessBuilder testAppPb;76private Process testApp;7778private ManagementAgentJcmd jcmd;7980abstract protected List<String> getCustomVmArgs();81abstract protected Pattern getDefaultPattern();8283@BeforeTest84public final void setup() throws Exception {85List<String> args = new ArrayList<>();86args.add("-cp");87args.add(System.getProperty("test.class.path"));88args.add("-XX:+UsePerfData");89args.addAll(getCustomVmArgs());90args.add(TEST_APP_NAME);91testAppPb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));9293jcmd = new ManagementAgentJcmd(TEST_APP_NAME, false);94}9596@BeforeMethod97public final void startTestApp() throws Exception {98testApp = ProcessTools.startProcess(99TEST_APP_NAME, testAppPb,100(Predicate<String>)l->l.trim().equals("main enter")101);102}103104@AfterMethod105public final void stopTestApp() throws Exception {106testApp.getOutputStream().write(1);107testApp.getOutputStream().flush();108testApp.waitFor();109testApp = null;110}111112@Test113public final void testAgentLocal() throws Exception {114jcmd.startLocal();115String status = jcmd.status();116117assertStatusMatches(LOCAL_AGENT_STATUS, status);118}119120@Test121public final void testAgentRemote() throws Exception {122while (true) {123try {124int[] ports = PortAllocator.allocatePorts(1);125jcmd.start(126"jmxremote.port=" + ports[0],127"jmxremote.authenticate=false",128"jmxremote.ssl=false"129);130String status = jcmd.status();131132assertStatusMatches(REMOTE_AGENT_STATUS, status);133return;134} catch (BindException e) {135System.out.println("Failed to allocate ports. Retrying ...");136}137}138}139140@Test141public final void testAgentDefault() throws Exception {142String status = jcmd.status();143assertStatusMatches(getDefaultPattern(), status);144}145146protected void assertStatusMatches(Pattern expected, String value) {147assertStatusMatches(expected, value, "");148}149150protected void assertStatusMatches(Pattern expected, String value, String msg) {151int idx = value.indexOf('\n');152if (idx > -1) {153value = value.substring(idx + 1).trim();154assertTrue(expected.matcher(value).find(), msg);155} else {156fail("The management agent status must contain more then one line of text");157}158}159}160161162