jdk/test/sun/management/jmxremote/startstop/JMXStatusTest.java
changeset 33267 c9211ad104b5
parent 30820 0d4717a011d3
child 34540 83f41bd1c3c8
equal deleted inserted replaced
33266:348711a3d78d 33267:c9211ad104b5
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import java.net.BindException;
    24 import java.net.BindException;
       
    25 import java.util.ArrayList;
       
    26 import java.util.List;
    25 import java.util.function.Predicate;
    27 import java.util.function.Predicate;
    26 import java.util.regex.Pattern;
    28 import java.util.regex.Pattern;
    27 import org.testng.annotations.*;
    29 import org.testng.annotations.*;
    28 import static org.testng.Assert.*;
    30 import static org.testng.Assert.*;
    29 
    31 
    30 import jdk.testlibrary.ProcessTools;
    32 import jdk.testlibrary.ProcessTools;
    31 
    33 
    32 /**
    34 /**
    33  * @test
    35  * @test
    34  * @bug 8023093
    36  * @bug 8023093 8138748
    35  * @summary Performs a sanity test for the ManagementAgent.status diagnostic command.
    37  * @summary Performs a sanity test for the ManagementAgent.status diagnostic command.
    36  *          Management agent may be disable, started (only local connections) and started.
    38  *          Management agent may be disabled, started (only local connections) and started.
    37  *          The test asserts that the expected text is being printed.
    39  *          The test asserts that the expected text is being printed.
    38  * @library /lib/testlibrary
    40  * @library /lib/testlibrary
    39  * @modules java.management/sun.management
    41  * @modules java.management/sun.management
    40  * @build jdk.testlibrary.* PortAllocator TestApp ManagementAgentJcmd
    42  * @build jdk.testlibrary.* PortAllocator TestApp ManagementAgentJcmd
    41  * @run testng/othervm -XX:+UsePerfData JMXStatusTest
    43  *        JMXStatusTest JMXStatus1Test JMXStatus2Test
       
    44  * @run testng/othervm -XX:+UsePerfData JMXStatus1Test
       
    45  * @run testng/othervm -XX:+UsePerfData JMXStatus2Test
    42  */
    46  */
    43 public class JMXStatusTest {
    47 abstract public class JMXStatusTest {
    44     private final static String TEST_APP_NAME = "TestApp";
    48     private final static String TEST_APP_NAME = "TestApp";
    45 
    49 
    46     private final static Pattern DISABLE_AGENT_STATUS = Pattern.compile(
    50     protected final static Pattern DISABLED_AGENT_STATUS = Pattern.compile(
    47         "Agent\\s*\\: disabled$"
    51         "Agent\\s*\\: disabled$"
    48     );
    52     );
    49 
    53 
    50     private final static Pattern LOCAL_AGENT_STATUS = Pattern.compile(
    54     protected final static Pattern LOCAL_AGENT_STATUS = Pattern.compile(
    51         "Agent\\s*\\:\\s*enabled\\n+" +
    55         "Agent\\s*\\:\\s*enabled\\n+" +
    52         "Connection Type\\s*\\:\\s*local\\n+" +
    56         "Connection Type\\s*\\:\\s*local\\n+" +
    53         "Protocol\\s*\\:\\s*[a-z]+\\n+" +
    57         "Protocol\\s*\\:\\s*[a-z]+\\n+" +
    54         "Host\\s*\\:\\s*.+\\n+" +
    58         "Host\\s*\\:\\s*.+\\n+" +
    55         "URL\\s*\\:\\s*service\\:jmx\\:.+",
    59         "URL\\s*\\:\\s*service\\:jmx\\:.+",
    56         Pattern.MULTILINE
    60         Pattern.MULTILINE
    57     );
    61     );
    58 
    62 
    59     private final static Pattern REMOTE_AGENT_STATUS = Pattern.compile(
    63     protected final static Pattern REMOTE_AGENT_STATUS = Pattern.compile(
    60         "Agent\\s*\\: enabled\\n+" +
    64         "Agent\\s*\\: enabled\\n+" +
       
    65         ".*" +
    61         "Connection Type\\s*\\: remote\\n+" +
    66         "Connection Type\\s*\\: remote\\n+" +
    62         "Protocol\\s*\\: [a-z]+\\n+" +
    67         "Protocol\\s*\\: [a-z]+\\n+" +
    63         "Host\\s*\\: .+\\n+" +
    68         "Host\\s*\\: .+\\n+" +
    64         "URL\\s*\\: service\\:jmx\\:.+\\n+" +
    69         "URL\\s*\\: service\\:jmx\\:.+\\n+" +
    65         "Properties\\s*\\:\\n+(\\s*\\S+\\s*=\\s*\\S+\\n*)+",
    70         "Properties\\s*\\:\\n+(\\s*\\S+\\s*=\\s*\\S+\\n*)+",
    66         Pattern.MULTILINE
    71         Pattern.MULTILINE | Pattern.DOTALL
    67     );
    72     );
    68 
    73 
    69     private static ProcessBuilder testAppPb;
    74     private static ProcessBuilder testAppPb;
    70     private Process testApp;
    75     private Process testApp;
    71 
    76 
    72     private ManagementAgentJcmd jcmd;
    77     private ManagementAgentJcmd jcmd;
    73 
    78 
    74     @BeforeClass
    79     abstract protected List<String> getCustomVmArgs();
    75     public static void setupClass() throws Exception {
    80     abstract protected Pattern getDefaultPattern();
    76         testAppPb = ProcessTools.createJavaProcessBuilder(
       
    77             "-cp", System.getProperty("test.class.path"),
       
    78             "-XX:+UsePerfData",
       
    79             TEST_APP_NAME
       
    80         );
       
    81     }
       
    82 
    81 
    83     @BeforeTest
    82     @BeforeTest
    84     public void setup() {
    83     public final void setup() throws Exception {
       
    84         List<String> args = new ArrayList<>();
       
    85         args.add("-cp");
       
    86         args.add(System.getProperty("test.class.path"));
       
    87         args.add("-XX:+UsePerfData");
       
    88         args.addAll(getCustomVmArgs());
       
    89         args.add(TEST_APP_NAME);
       
    90         testAppPb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
       
    91 
    85         jcmd = new ManagementAgentJcmd(TEST_APP_NAME, false);
    92         jcmd = new ManagementAgentJcmd(TEST_APP_NAME, false);
    86     }
    93     }
    87 
    94 
    88     @BeforeMethod
    95     @BeforeMethod
    89     public void startTestApp() throws Exception {
    96     public final void startTestApp() throws Exception {
    90         testApp = ProcessTools.startProcess(
    97         testApp = ProcessTools.startProcess(
    91             TEST_APP_NAME, testAppPb,
    98             TEST_APP_NAME, testAppPb,
    92             (Predicate<String>)l->l.trim().equals("main enter")
    99             (Predicate<String>)l->l.trim().equals("main enter")
    93         );
   100         );
    94     }
   101     }
    95 
   102 
    96     @AfterMethod
   103     @AfterMethod
    97     public void stopTestApp() throws Exception {
   104     public final void stopTestApp() throws Exception {
    98         testApp.getOutputStream().write(1);
   105         testApp.getOutputStream().write(1);
    99         testApp.getOutputStream().flush();
   106         testApp.getOutputStream().flush();
   100         testApp.waitFor();
   107         testApp.waitFor();
   101         testApp = null;
   108         testApp = null;
   102     }
   109     }
   103 
   110 
   104     @Test
   111     @Test
   105     public void testAgentDisabled() throws Exception {
   112     public final void testAgentLocal() throws Exception {
   106         String status = jcmd.status();
       
   107         assertStatusMatches(DISABLE_AGENT_STATUS, status);
       
   108     }
       
   109 
       
   110     @Test
       
   111     public void testAgentLocal() throws Exception {
       
   112         jcmd.startLocal();
   113         jcmd.startLocal();
   113         String status = jcmd.status();
   114         String status = jcmd.status();
   114 
   115 
   115         assertStatusMatches(LOCAL_AGENT_STATUS, status);
   116         assertStatusMatches(LOCAL_AGENT_STATUS, status);
   116     }
   117     }
   117 
   118 
   118     @Test
   119     @Test
   119     public void testAgentRemote() throws Exception {
   120     public final void testAgentRemote() throws Exception {
   120         while (true) {
   121         while (true) {
   121             try {
   122             try {
   122                 int[] ports = PortAllocator.allocatePorts(1);
   123                 int[] ports = PortAllocator.allocatePorts(1);
   123                 jcmd.start(
   124                 jcmd.start(
   124                     "jmxremote.port=" + ports[0],
   125                     "jmxremote.port=" + ports[0],
   133                 System.out.println("Failed to allocate ports. Retrying ...");
   134                 System.out.println("Failed to allocate ports. Retrying ...");
   134             }
   135             }
   135         }
   136         }
   136     }
   137     }
   137 
   138 
   138     private void assertStatusMatches(Pattern expected, String value) {
   139     @Test
       
   140     public final void testAgentDefault() throws Exception {
       
   141         String status = jcmd.status();
       
   142         assertStatusMatches(getDefaultPattern(), status);
       
   143     }
       
   144 
       
   145     protected void assertStatusMatches(Pattern expected, String value) {
   139         assertStatusMatches(expected, value, "");
   146         assertStatusMatches(expected, value, "");
   140     }
   147     }
   141 
   148 
   142     private void assertStatusMatches(Pattern expected, String value, String msg) {
   149     protected void assertStatusMatches(Pattern expected, String value, String msg) {
   143         int idx = value.indexOf('\n');
   150         int idx = value.indexOf('\n');
   144         if (idx > -1) {
   151         if (idx > -1) {
   145             value = value.substring(idx + 1).trim();
   152             value = value.substring(idx + 1).trim();
   146             assertTrue(expected.matcher(value).find(), msg);
   153             assertTrue(expected.matcher(value).find(), msg);
   147         } else {
   154         } else {