jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java
changeset 30365 551470085a1d
parent 30046 cf2c86e1819e
parent 30360 c34ae192d75f
child 30376 2ccf2cf7ea48
equal deleted inserted replaced
30073:989253a902c3 30365:551470085a1d
    33 import java.rmi.registry.Registry;
    33 import java.rmi.registry.Registry;
    34 import java.util.ArrayList;
    34 import java.util.ArrayList;
    35 import java.util.Arrays;
    35 import java.util.Arrays;
    36 import java.util.List;
    36 import java.util.List;
    37 import java.util.Objects;
    37 import java.util.Objects;
    38 import java.util.Random;
       
    39 import java.util.Set;
    38 import java.util.Set;
    40 import java.util.concurrent.TimeUnit;
    39 import java.util.concurrent.TimeUnit;
    41 import java.util.concurrent.TimeoutException;
    40 import java.util.concurrent.TimeoutException;
    42 import java.util.concurrent.atomic.AtomicBoolean;
    41 import java.util.concurrent.atomic.AtomicBoolean;
    43 import java.util.function.Consumer;
       
    44 import java.util.stream.Collectors;
       
    45 
    42 
    46 import javax.management.*;
    43 import javax.management.*;
    47 import javax.management.remote.*;
    44 import javax.management.remote.*;
    48 import javax.net.ssl.SSLHandshakeException;
    45 import javax.net.ssl.SSLHandshakeException;
    49 
    46 
    50 import jdk.testlibrary.ProcessTools;
    47 import jdk.testlibrary.ProcessTools;
    51 import jdk.testlibrary.JDKToolLauncher;
       
    52 import sun.management.Agent;
    48 import sun.management.Agent;
    53 import sun.management.AgentConfigurationError;
    49 import sun.management.AgentConfigurationError;
    54 
    50 
    55 /**
    51 /**
    56  * @test
    52  * @test
    57  * @bug 7110104
    53  * @bug 7110104
    58  * @library /lib/testlibrary
    54  * @library /lib/testlibrary
    59  * @build jdk.testlibrary.* JMXStartStopTest JMXStartStopDoSomething
    55  * @build jdk.testlibrary.* JMXStartStopTest PortAllocator TestApp ManagementAgentJcmd
    60  * @run main/othervm/timeout=600 -XX:+UsePerfData JMXStartStopTest
    56  * @run main/othervm/timeout=600 -XX:+UsePerfData JMXStartStopTest
    61  * @summary Makes sure that enabling/disabling the management agent through JCMD
    57  * @summary Makes sure that enabling/disabling the management agent through JCMD
    62  *          achieves the desired results
    58  *          achieves the desired results
    63  * @key randomness
    59  * @key randomness
    64  */
    60  */
    65 public class JMXStartStopTest {
    61 public class JMXStartStopTest {
       
    62     private static final String TEST_APP_NAME = "TestApp";
    66 
    63 
    67     private static final String TEST_SRC = System.getProperty("test.src");
    64     private static final String TEST_SRC = System.getProperty("test.src");
    68 
    65 
    69     private static final boolean verbose = false;
    66     private static final boolean verbose = false;
    70 
    67 
    71     /**
    68     private static ManagementAgentJcmd jcmd = new ManagementAgentJcmd(TEST_APP_NAME, verbose);
    72      * Dynamically allocates distinct ports from the ephemeral range 49152-65535
       
    73      */
       
    74     private static class PortAllocator {
       
    75 
       
    76         private final static int LOWER_BOUND = 49152;
       
    77         private final static int UPPER_BOUND = 65535;
       
    78 
       
    79         private final static Random RND = new Random(System.currentTimeMillis());
       
    80 
       
    81         private static int[] allocatePorts(final int numPorts) {
       
    82             int[] ports = new int[numPorts];
       
    83             for (int i = 0; i < numPorts; i++) {
       
    84                 int port = -1;
       
    85                 while (port == -1) {
       
    86                     port = RND.nextInt(UPPER_BOUND - LOWER_BOUND + 1) + LOWER_BOUND;
       
    87                     for (int j = 0; j < i; j++) {
       
    88                         if (ports[j] == port) {
       
    89                             port = -1;
       
    90                             break;
       
    91                         }
       
    92                     }
       
    93                 }
       
    94                 ports[i] = port;
       
    95             }
       
    96             return ports;
       
    97         }
       
    98     }
       
    99 
    69 
   100     private static void dbg_print(String msg) {
    70     private static void dbg_print(String msg) {
   101         if (verbose) {
    71         if (verbose) {
   102             System.out.println("DBG: " + msg);
    72             System.out.println("DBG: " + msg);
   103         }
    73         }
   316             }
   286             }
   317             throw new Error();
   287             throw new Error();
   318         }
   288         }
   319     }
   289     }
   320 
   290 
   321     private static class Something {
   291     private static class TestAppRun {
   322         private Process p;
   292         private Process p;
   323         private final ProcessBuilder pb;
   293         private final ProcessBuilder pb;
   324         private final String name;
   294         private final String name;
   325         private final AtomicBoolean started = new AtomicBoolean(false);
   295         private final AtomicBoolean started = new AtomicBoolean(false);
   326         private volatile long pid = -1;
   296         private volatile long pid = -1;
   327 
   297 
   328         public Something(ProcessBuilder pb, String name) {
   298         public TestAppRun(ProcessBuilder pb, String name) {
   329             this.pb = pb;
   299             this.pb = pb;
   330             this.name = name;
   300             this.name = name;
   331         }
   301         }
   332 
   302 
   333         public synchronized void start() throws InterruptedException, IOException, TimeoutException {
   303         public synchronized void start() throws InterruptedException, IOException, TimeoutException {
   334             if (started.compareAndSet(false, true)) {
   304             if (started.compareAndSet(false, true)) {
   335                 try {
   305                 try {
   336                     AtomicBoolean error = new AtomicBoolean(false);
   306                     AtomicBoolean error = new AtomicBoolean(false);
   337                     p = ProcessTools.startProcess(
   307                     p = ProcessTools.startProcess(
   338                             "JMXStartStopDoSomething{" + name + "}",
   308                             TEST_APP_NAME + "{" + name + "}",
   339                             pb,
   309                             pb,
   340                             (line) -> {
   310                             (line) -> {
   341                                 boolean ok = line.equals("main enter");
   311                                 boolean ok = line.equals("main enter");
   342                                 error.set(line.contains("BindException"));
   312                                 error.set(line.contains("BindException"));
   343 
   313 
   344                                 return ok || error.get();
   314                                 return ok || error.get();
   345                             },
   315                             }
   346                             5,
       
   347                             TimeUnit.SECONDS
       
   348                     );
   316                     );
   349                     if (error.get()) {
   317                     if (error.get()) {
   350                         throw new BindException("Starting process failed due to " +
   318                         throw new BindException("Starting process failed due to " +
   351                                                 "the requested port not being available");
   319                                                 "the requested port not being available");
   352                     }
   320                     }
   353                     pid = p.getPid();
   321                     pid = p.getPid();
   354                 } catch (TimeoutException e) {
   322                 } catch (TimeoutException e) {
   355                     p.destroy();
   323                     if (p != null) {
   356                     p.waitFor();
   324                         p.destroy();
       
   325                         p.waitFor();
       
   326                     }
   357                     throw e;
   327                     throw e;
   358                 }
   328                 }
   359             }
   329             }
   360         }
   330         }
   361 
   331 
   380             }
   350             }
   381         }
   351         }
   382     }
   352     }
   383 
   353 
   384     /**
   354     /**
   385      * Runs the test application "JMXStartStopDoSomething"
   355      * Runs the test application "TestApp"
   386      * @param name Test run name
   356      * @param name Test run name
   387      * @param args Additional arguments
   357      * @param args Additional arguments
   388      * @return Returns a {@linkplain Something} instance representing the run
   358      * @return Returns a {@linkplain TestAppRun} instance representing the run
   389      * @throws IOException
   359      * @throws IOException
   390      * @throws InterruptedException
   360      * @throws InterruptedException
   391      * @throws TimeoutException
   361      * @throws TimeoutException
   392      */
   362      */
   393     private static Something doSomething(String name, String ... args)
   363     private static TestAppRun doTest(String name, String ... args)
   394             throws Exception {
   364             throws Exception {
   395         List<String> pbArgs = new ArrayList<>(Arrays.asList(
   365         List<String> pbArgs = new ArrayList<>(Arrays.asList(
   396                 "-cp",
   366                 "-cp",
   397                 System.getProperty("test.class.path")
   367                 System.getProperty("test.class.path"),
       
   368                 "-XX:+UsePerfData"
   398         ));
   369         ));
   399         pbArgs.addAll(Arrays.asList(args));
   370         pbArgs.addAll(Arrays.asList(args));
   400         pbArgs.add("JMXStartStopDoSomething");
   371         pbArgs.add(TEST_APP_NAME);
   401 
   372 
   402         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   373         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   403                 pbArgs.toArray(new String[pbArgs.size()])
   374                 pbArgs.toArray(new String[pbArgs.size()])
   404         );
   375         );
   405         Something s = new Something(pb, name);
   376         TestAppRun s = new TestAppRun(pb, name);
   406         s.start();
   377         s.start();
   407         return s;
   378         return s;
   408     }
   379     }
   409 
       
   410     /**
       
   411      * Run the "jcmd" command
       
   412      *
       
   413      * @param command Command with parameters; space separated string
       
   414      * @throws IOException
       
   415      * @throws InterruptedException
       
   416      */
       
   417     private static void jcmd(String ... command) throws IOException, InterruptedException {
       
   418         if (command.length == 0) {
       
   419             jcmd(null, c->{});
       
   420         } else {
       
   421             jcmd(null, command);
       
   422         }
       
   423     }
       
   424 
       
   425     /**
       
   426      * Run the "jcmd" command
       
   427      *
       
   428      * @param c {@linkplain Consumer} instance
       
   429      * @param command Command with parameters; space separated string
       
   430      * @throws IOException
       
   431      * @throws InterruptedException
       
   432      */
       
   433     private static void jcmd(Consumer<String> c, String ... command) throws IOException, InterruptedException {
       
   434         jcmd("JMXStartStopDoSomething", c, command);
       
   435     }
       
   436 
       
   437     /**
       
   438      * Run the "jcmd" command
       
   439      *
       
   440      * @param target The target application name (or PID)
       
   441      * @param c {@linkplain Consumer} instance
       
   442      * @param command Command with parameters; space separated string
       
   443      * @throws IOException
       
   444      * @throws InterruptedException
       
   445      */
       
   446     private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
       
   447         dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));
       
   448 
       
   449         JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
       
   450         l.addToolArg(target);
       
   451         for (String cmd : command) {
       
   452             l.addToolArg(cmd);
       
   453         }
       
   454 
       
   455         AtomicBoolean portUnavailable = new AtomicBoolean(false);
       
   456         Process p = ProcessTools.startProcess(
       
   457             "jcmd",
       
   458             new ProcessBuilder(l.getCommand()),
       
   459             line -> {
       
   460                 if (line.contains("BindException") ||
       
   461                     line.contains(Agent.getText(AgentConfigurationError.CONNECTOR_SERVER_IO_ERROR))) {
       
   462                     portUnavailable.set(true);
       
   463                 } else {
       
   464                     c.accept(line);
       
   465                 }
       
   466             }
       
   467         );
       
   468 
       
   469         p.waitFor();
       
   470         dbg_print("[jcmd] --------");
       
   471         if (portUnavailable.get()) {
       
   472             String cmd = Arrays.asList(l.getCommand()).stream()
       
   473                     .collect(
       
   474                             Collectors.joining(" ", "", ": Unable to bind address")
       
   475                     );
       
   476             throw new BindException(cmd);
       
   477         }
       
   478     }
       
   479 
       
   480     private static final String CMD_STOP = "ManagementAgent.stop";
       
   481     private static final String CMD_START = "ManagementAgent.start";
       
   482     private static final String CMD_START_LOCAL = "ManagementAgent.start_local";
       
   483 
   380 
   484     static void test_01() throws Exception {
   381     static void test_01() throws Exception {
   485         // Run an app with JMX enabled stop it and
   382         // Run an app with JMX enabled stop it and
   486         // restart on other port
   383         // restart on other port
   487 
   384 
   488         System.out.println("**** Test one ****");
   385         System.out.println("**** Test one ****");
   489         int ports[] = PortAllocator.allocatePorts(2);
   386         int ports[] = PortAllocator.allocatePorts(2);
   490 
   387 
   491         Something s = doSomething(
   388         TestAppRun s = doTest(
   492                 "test_01",
   389                 "test_01",
   493                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   390                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   494                 "-Dcom.sun.management.jmxremote.authenticate=false",
   391                 "-Dcom.sun.management.jmxremote.authenticate=false",
   495                 "-Dcom.sun.management.jmxremote.ssl=false");
   392                 "-Dcom.sun.management.jmxremote.ssl=false");
   496 
   393 
   497         try {
   394         try {
   498             testConnect(ports[0]);
   395             testConnect(ports[0]);
   499 
   396 
   500             jcmd(CMD_STOP);
   397             jcmd.stop();
   501             testNoConnect(ports[0]);
   398             testNoConnect(ports[0]);
   502 
   399 
   503             jcmd(CMD_START, "jmxremote.port=" + ports[1]);
   400             jcmd.start("jmxremote.port=" + ports[1]);
   504             testConnect(ports[1]);
   401             testConnect(ports[1]);
   505         } finally {
   402         } finally {
   506             s.stop();
   403             s.stop();
   507         }
   404         }
   508     }
   405     }
   512         // start JMX by jcmd
   409         // start JMX by jcmd
   513 
   410 
   514         System.out.println("**** Test two ****");
   411         System.out.println("**** Test two ****");
   515 
   412 
   516         int[] ports = PortAllocator.allocatePorts(1);
   413         int[] ports = PortAllocator.allocatePorts(1);
   517         Something s = doSomething("test_02");
   414         TestAppRun s = doTest("test_02");
   518         try {
   415         try {
   519             jcmd(CMD_START,
   416             jcmd.start(
   520                     "jmxremote.port=" + ports[0],
   417                 "jmxremote.port=" + ports[0],
   521                     "jmxremote.authenticate=false",
   418                 "jmxremote.authenticate=false",
   522                     "jmxremote.ssl=false");
   419                 "jmxremote.ssl=false"
       
   420             );
   523 
   421 
   524             testConnect(ports[0]);
   422             testConnect(ports[0]);
   525         } finally {
   423         } finally {
   526 //            debugPortUsage(pa);
   424 //            debugPortUsage(pa);
   527             s.stop();
   425             s.stop();
   533         // start JMX by jcmd on one port than on other one
   431         // start JMX by jcmd on one port than on other one
   534 
   432 
   535         System.out.println("**** Test three ****");
   433         System.out.println("**** Test three ****");
   536 
   434 
   537         int[] ports = PortAllocator.allocatePorts(2);
   435         int[] ports = PortAllocator.allocatePorts(2);
   538         Something s = doSomething("test_03");
   436         TestAppRun s = doTest("test_03");
   539         try {
   437         try {
   540             jcmd(CMD_START,
   438             jcmd.start(
   541                     "jmxremote.port=" + ports[0],
   439                 "jmxremote.port=" + ports[0],
   542                     "jmxremote.authenticate=false",
   440                 "jmxremote.authenticate=false",
   543                     "jmxremote.ssl=false");
   441                 "jmxremote.ssl=false"
       
   442             );
   544 
   443 
   545             // Second agent shouldn't start
   444             // Second agent shouldn't start
   546             jcmd(CMD_START,
   445             jcmd.start(
   547                     "jmxremote.port=" + ports[1],
   446                 "jmxremote.port=" + ports[1],
   548                     "jmxremote.authenticate=false",
   447                 "jmxremote.authenticate=false",
   549                     "jmxremote.ssl=false");
   448                 "jmxremote.ssl=false"
       
   449             );
   550 
   450 
   551             // First agent should connect
   451             // First agent should connect
   552             testConnect(ports[0]);
   452             testConnect(ports[0]);
   553 
   453 
   554             // Second agent should not connect
   454             // Second agent should not connect
   563         // start JMX by jcmd on one port, specify rmi port explicitly
   463         // start JMX by jcmd on one port, specify rmi port explicitly
   564 
   464 
   565         System.out.println("**** Test four ****");
   465         System.out.println("**** Test four ****");
   566 
   466 
   567         int[] ports = PortAllocator.allocatePorts(2);
   467         int[] ports = PortAllocator.allocatePorts(2);
   568         Something s = doSomething("test_04");
   468         TestAppRun s = doTest("test_04");
   569         try {
   469         try {
   570             jcmd(CMD_START,
   470             jcmd.start(
   571                     "jmxremote.port=" + ports[0],
   471                 "jmxremote.port=" + ports[0],
   572                     "jmxremote.rmi.port=" + ports[1],
   472                 "jmxremote.rmi.port=" + ports[1],
   573                     "jmxremote.authenticate=false",
   473                 "jmxremote.authenticate=false",
   574                     "jmxremote.ssl=false");
   474                 "jmxremote.ssl=false"
       
   475             );
   575 
   476 
   576             testConnect(ports[0], ports[1]);
   477             testConnect(ports[0], ports[1]);
   577         } finally {
   478         } finally {
   578             s.stop();
   479             s.stop();
   579         }
   480         }
   583         // Run an app without JMX enabled, it will enable local server
   484         // Run an app without JMX enabled, it will enable local server
   584         // but should leave remote server disabled
   485         // but should leave remote server disabled
   585 
   486 
   586         System.out.println("**** Test five ****");
   487         System.out.println("**** Test five ****");
   587         int[] ports = PortAllocator.allocatePorts(1);
   488         int[] ports = PortAllocator.allocatePorts(1);
   588         Something s = doSomething("test_05");
   489         TestAppRun s = doTest("test_05");
   589         try {
   490         try {
   590             jcmd(CMD_START_LOCAL);
   491             jcmd.startLocal();
   591 
   492 
   592             testNoConnect(ports[0]);
   493             testNoConnect(ports[0]);
   593             testConnectLocal(s.getPid());
   494             testConnectLocal(s.getPid());
   594         } finally {
   495         } finally {
   595             s.stop();
   496             s.stop();
   603         // Check for valid messages in the output
   504         // Check for valid messages in the output
   604 
   505 
   605         System.out.println("**** Test six ****");
   506         System.out.println("**** Test six ****");
   606 
   507 
   607         int[] ports = PortAllocator.allocatePorts(2);
   508         int[] ports = PortAllocator.allocatePorts(2);
   608         Something s = doSomething("test_06");
   509         TestAppRun s = doTest("test_06");
   609         try {
   510         try {
   610             jcmd(CMD_START,
   511             jcmd.start(
   611                     "jmxremote.port=" + ports[0],
   512                 "jmxremote.port=" + ports[0],
   612                     "jmxremote.authenticate=false",
   513                 "jmxremote.authenticate=false",
   613                     "jmxremote.ssl=false");
   514                 "jmxremote.ssl=false"
       
   515             );
   614 
   516 
   615             testConnect(ports[0], ports[1]);
   517             testConnect(ports[0], ports[1]);
   616 
   518 
   617             final AtomicBoolean checks = new AtomicBoolean(false);
   519             final AtomicBoolean checks = new AtomicBoolean(false);
   618             jcmd(
   520             jcmd.start(
   619                     line -> {
   521                 line -> {
   620                         if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
   522                     if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
   621                             checks.set(true);
   523                         checks.set(true);
   622                         }
   524                     }
   623                     },
   525                 },
   624                     CMD_START,
   526                 "jmxremote.port=" + ports[0],
   625                     "jmxremote.port=" + ports[0],
   527                 "jmxremote.authenticate=false",
   626                     "jmxremote.authenticate=false",
   528                 "jmxremote.ssl=false"
   627                     "jmxremote.ssl=false");
   529             );
   628 
   530 
   629             if (!checks.get()) {
   531             if (!checks.get()) {
   630                 throw new Exception("Starting agent on port " + ports[0] + " should "
   532                 throw new Exception("Starting agent on port " + ports[0] + " should "
   631                         + "report an invalid agent state");
   533                         + "report an invalid agent state");
   632             }
   534             }
   642         // Check for valid messages in the output
   544         // Check for valid messages in the output
   643 
   545 
   644         System.out.println("**** Test seven ****");
   546         System.out.println("**** Test seven ****");
   645 
   547 
   646         int[] ports = PortAllocator.allocatePorts(2);
   548         int[] ports = PortAllocator.allocatePorts(2);
   647         Something s = doSomething("test_07");
   549         TestAppRun s = doTest("test_07");
   648         try {
   550         try {
   649             jcmd(CMD_START,
   551             jcmd.start(
   650                     "jmxremote.port=" + ports[0],
   552                 "jmxremote.port=" + ports[0],
   651                     "jmxremote.authenticate=false",
   553                 "jmxremote.authenticate=false",
   652                     "jmxremote.ssl=false");
   554                 "jmxremote.ssl=false"
       
   555             );
   653 
   556 
   654             testConnect(ports[0], ports[1]);
   557             testConnect(ports[0], ports[1]);
   655 
   558 
   656             final AtomicBoolean checks = new AtomicBoolean(false);
   559             final AtomicBoolean checks = new AtomicBoolean(false);
   657 
   560 
   658             jcmd(
   561             jcmd.start(
   659                     line -> {
   562                 line -> {
   660                         if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
   563                     if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
   661                             checks.set(true);
   564                         checks.set(true);
   662                         }
   565                     }
   663                     },
   566                 },
   664                     CMD_START,
   567                 "jmxremote.port=" + ports[1],
   665                     "jmxremote.port=" + ports[1],
   568                 "jmxremote.authenticate=false",
   666                     "jmxremote.authenticate=false",
   569                 "jmxremote.ssl=false"
   667                     "jmxremote.ssl=false");
   570             );
   668 
   571 
   669             if (!checks.get()) {
   572             if (!checks.get()) {
   670                 throw new Exception("Starting agent on poprt " + ports[1] + " should "
   573                 throw new Exception("Starting agent on poprt " + ports[1] + " should "
   671                         + "report an invalid agent state");
   574                         + "report an invalid agent state");
   672             }
   575             }
   682         // Check for valid messages in the output
   585         // Check for valid messages in the output
   683 
   586 
   684         System.out.println("**** Test eight ****");
   587         System.out.println("**** Test eight ****");
   685 
   588 
   686         int[] ports = PortAllocator.allocatePorts(2);
   589         int[] ports = PortAllocator.allocatePorts(2);
   687         Something s = doSomething("test_08");
   590         TestAppRun s = doTest("test_08");
   688         try {
   591         try {
   689             jcmd(CMD_START,
   592             jcmd.start(
   690                     "jmxremote.port=" + ports[0],
   593                 "jmxremote.port=" + ports[0],
   691                     "jmxremote.authenticate=false",
   594                 "jmxremote.authenticate=false",
   692                     "jmxremote.ssl=false");
   595                 "jmxremote.ssl=false"
       
   596             );
   693 
   597 
   694             testConnect(ports[0], ports[1]);
   598             testConnect(ports[0], ports[1]);
   695 
   599 
   696             jcmd(CMD_STOP);
   600             jcmd.stop();
   697             jcmd(CMD_STOP);
   601             jcmd.stop();
   698         } finally {
   602         } finally {
   699             s.stop();
   603             s.stop();
   700         }
   604         }
   701     }
   605     }
   702 
   606 
   705         // attempt to start JMX using a non-available port
   609         // attempt to start JMX using a non-available port
   706         // Check for valid messages in the output
   610         // Check for valid messages in the output
   707 
   611 
   708         System.out.println("**** Test nine ****");
   612         System.out.println("**** Test nine ****");
   709 
   613 
   710         Something s = doSomething("test_09");
   614         TestAppRun s = doTest("test_09");
   711 
   615 
   712         try (ServerSocket ss = new ServerSocket(0)) {
   616         try (ServerSocket ss = new ServerSocket(0)) {
   713             int localPort = ss.getLocalPort();
   617             int localPort = ss.getLocalPort();
   714             int[] ports;
   618             int[] ports;
   715             do {
   619             do {
   721             int retryCntr = 1;
   625             int retryCntr = 1;
   722             do {
   626             do {
   723                 final AtomicBoolean retry = new AtomicBoolean(false);
   627                 final AtomicBoolean retry = new AtomicBoolean(false);
   724 
   628 
   725                 try {
   629                 try {
   726                     jcmd(
   630                     jcmd.start(
   727                         line -> {
   631                         line -> {
   728                             if (line.contains(Agent.getText(AgentConfigurationError.AGENT_EXCEPTION))) {
   632                             if (line.contains(Agent.getText(AgentConfigurationError.AGENT_EXCEPTION))) {
   729                                 retry.set(true);
   633                                 retry.set(true);
   730                             }
   634                             }
   731                         },
   635                         },
   732                         CMD_START,
       
   733                         "jmxremote.port=" + ports[0],
   636                         "jmxremote.port=" + ports[0],
   734                         "jmxremote.rmi.port=" + localPort,
   637                         "jmxremote.rmi.port=" + localPort,
   735                         "jmxremote.authenticate=false",
   638                         "jmxremote.authenticate=false",
   736                         "jmxremote.ssl=false"
   639                         "jmxremote.ssl=false"
   737                     );
   640                     );
   762         // make sure these properties overridden corectly
   665         // make sure these properties overridden corectly
   763 
   666 
   764         System.out.println("**** Test ten ****");
   667         System.out.println("**** Test ten ****");
   765 
   668 
   766         int[] ports = PortAllocator.allocatePorts(2);
   669         int[] ports = PortAllocator.allocatePorts(2);
   767         Something s = doSomething(
   670         TestAppRun s = doTest(
   768                 "test_10",
   671                 "test_10",
   769                 "-Dcom.sun.management.jmxremote.authenticate=false",
   672                 "-Dcom.sun.management.jmxremote.authenticate=false",
   770                 "-Dcom.sun.management.jmxremote.ssl=true");
   673                 "-Dcom.sun.management.jmxremote.ssl=true");
   771 
   674 
   772         try {
   675         try {
   773             testNoConnect(ports[0]);
   676             testNoConnect(ports[0]);
   774             jcmd(
   677             jcmd.start(
   775                     CMD_START,
   678                 "jmxremote.port=" + ports[1],
   776                     "jmxremote.port=" + ports[1],
   679                 "jmxremote.authenticate=false",
   777                     "jmxremote.authenticate=false",
   680                 "jmxremote.ssl=false"
   778                     "jmxremote.ssl=false"
       
   779             );
   681             );
   780             testConnect(ports[1]);
   682             testConnect(ports[1]);
   781         } finally {
   683         } finally {
   782             s.stop();
   684             s.stop();
   783         }
   685         }
   789         // stop JMX agent and then start it again with different property values
   691         // stop JMX agent and then start it again with different property values
   790         // make sure these properties overridden corectly
   692         // make sure these properties overridden corectly
   791 
   693 
   792         System.out.println("**** Test eleven ****");
   694         System.out.println("**** Test eleven ****");
   793         int[] ports = PortAllocator.allocatePorts(2);
   695         int[] ports = PortAllocator.allocatePorts(2);
   794         Something s = doSomething(
   696         TestAppRun s = doTest(
   795                 "test_11",
   697                 "test_11",
   796                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   698                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   797                 "-Dcom.sun.management.jmxremote.authenticate=false",
   699                 "-Dcom.sun.management.jmxremote.authenticate=false",
   798                 "-Dcom.sun.management.jmxremote.ssl=true");
   700                 "-Dcom.sun.management.jmxremote.ssl=true");
   799 
   701 
   800         try {
   702         try {
   801             testNoConnect(ports[0]);
   703             testNoConnect(ports[0]);
   802 
   704 
   803             jcmd(CMD_STOP);
   705             jcmd.stop();
   804 
   706 
   805             testNoConnect(ports[0]);
   707             testNoConnect(ports[0]);
   806 
   708 
   807             jcmd(
   709             jcmd.start(
   808                     CMD_START,
   710                 "jmxremote.port=" + ports[1],
   809                     "jmxremote.port=" + ports[1],
   711                 "jmxremote.authenticate=false",
   810                     "jmxremote.authenticate=false",
   712                 "jmxremote.ssl=false"
   811                     "jmxremote.ssl=false"
       
   812             );
   713             );
   813 
   714 
   814             testConnect(ports[1]);
   715             testConnect(ports[1]);
   815         } finally {
   716         } finally {
   816             s.stop();
   717             s.stop();
   826         // make sure these properties overridden corectly
   727         // make sure these properties overridden corectly
   827 
   728 
   828         System.out.println("**** Test twelve ****");
   729         System.out.println("**** Test twelve ****");
   829 
   730 
   830         int[] ports = PortAllocator.allocatePorts(2);
   731         int[] ports = PortAllocator.allocatePorts(2);
   831         Something s = doSomething("test_12",
   732         TestAppRun s = doTest("test_12",
   832                 "-Dcom.sun.management.config.file="
   733                 "-Dcom.sun.management.config.file="
   833                 + TEST_SRC + File.separator + "management_cl.properties",
   734                 + TEST_SRC + File.separator + "management_cl.properties",
   834                 "-Dcom.sun.management.jmxremote.authenticate=false"
   735                 "-Dcom.sun.management.jmxremote.authenticate=false"
   835         );
   736         );
   836 
   737 
   837         try {
   738         try {
   838             testNoConnect(ports[0]);
   739             testNoConnect(ports[0]);
   839 
   740 
   840             jcmd(CMD_STOP);
   741             jcmd.stop();
   841 
   742 
   842             testNoConnect(ports[0]);
   743             testNoConnect(ports[0]);
   843 
   744 
   844             jcmd(CMD_START,
   745             jcmd.start(
   845                     "config.file=" + TEST_SRC + File.separator
   746                 "config.file=" + TEST_SRC + File.separator
   846                     + "management_jcmd.properties",
   747                 + "management_jcmd.properties",
   847                     "jmxremote.authenticate=false",
   748                 "jmxremote.authenticate=false",
   848                     "jmxremote.port=" + ports[1]
   749                 "jmxremote.port=" + ports[1]
   849             );
   750             );
   850 
   751 
   851             testConnect(ports[1]);
   752             testConnect(ports[1]);
   852         } finally {
   753         } finally {
   853             s.stop();
   754             s.stop();
   861         // stop JMX agent again and then start it without property value
   762         // stop JMX agent again and then start it without property value
   862         // make sure these properties overridden corectly
   763         // make sure these properties overridden corectly
   863 
   764 
   864         System.out.println("**** Test thirteen ****");
   765         System.out.println("**** Test thirteen ****");
   865         int[] ports = PortAllocator.allocatePorts(1);
   766         int[] ports = PortAllocator.allocatePorts(1);
   866         Something s = doSomething(
   767         TestAppRun s = doTest(
   867                 "test_13",
   768                 "test_13",
   868                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   769                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   869                 "-Dcom.sun.management.jmxremote.authenticate=false",
   770                 "-Dcom.sun.management.jmxremote.authenticate=false",
   870                 "-Dcom.sun.management.jmxremote.ssl=true");
   771                 "-Dcom.sun.management.jmxremote.ssl=true");
   871 
   772 
   872         try {
   773         try {
   873             testNoConnect(ports[0]);
   774             testNoConnect(ports[0]);
   874 
   775 
   875             jcmd(CMD_STOP);
   776             jcmd.stop();
   876             jcmd(CMD_START,
   777             jcmd.start(
   877                     "jmxremote.ssl=false",
   778                 "jmxremote.ssl=false",
   878                     "jmxremote.port=" + ports[0]
   779                 "jmxremote.port=" + ports[0]
   879             );
   780             );
   880             testConnect(ports[0]);
   781             testConnect(ports[0]);
   881 
   782 
   882             jcmd(CMD_STOP);
   783             jcmd.stop();
   883             jcmd(CMD_START,
   784             jcmd.start(
   884                     "jmxremote.port=" + ports[0]
   785                 "jmxremote.port=" + ports[0]
   885             );
   786             );
   886 
   787 
   887             testNoConnect(ports[0]);
   788             testNoConnect(ports[0]);
   888         } finally {
   789         } finally {
   889             s.stop();
   790             s.stop();
   895         // stop remote agent
   796         // stop remote agent
   896         // make sure local agent is not affected
   797         // make sure local agent is not affected
   897 
   798 
   898         System.out.println("**** Test fourteen ****");
   799         System.out.println("**** Test fourteen ****");
   899         int[] ports = PortAllocator.allocatePorts(1);
   800         int[] ports = PortAllocator.allocatePorts(1);
   900         Something s = doSomething(
   801         TestAppRun s = doTest(
   901                 "test_14",
   802                 "test_14",
   902                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   803                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
   903                 "-Dcom.sun.management.jmxremote.authenticate=false",
   804                 "-Dcom.sun.management.jmxremote.authenticate=false",
   904                 "-Dcom.sun.management.jmxremote.ssl=false");
   805                 "-Dcom.sun.management.jmxremote.ssl=false");
   905         try {
   806         try {
   906             testConnect(ports[0]);
   807             testConnect(ports[0]);
   907             jcmd(CMD_STOP);
   808             jcmd.stop();
   908             testConnectLocal(s.getPid());
   809             testConnectLocal(s.getPid());
   909         } finally {
   810         } finally {
   910             s.stop();
   811             s.stop();
   911         }
   812         }
   912     }
   813     }
   916         // start local agent only
   817         // start local agent only
   917 
   818 
   918         System.out.println("**** Test fifteen ****");
   819         System.out.println("**** Test fifteen ****");
   919 
   820 
   920         int[] ports = PortAllocator.allocatePorts(1);
   821         int[] ports = PortAllocator.allocatePorts(1);
   921         Something s = doSomething("test_15");
   822         TestAppRun s = doTest("test_15");
   922 
   823 
   923         try {
   824         try {
   924             testNoConnect(ports[0]);
   825             testNoConnect(ports[0]);
   925             jcmd(CMD_START + "_local");
   826             jcmd.startLocal();
   926 
   827 
   927             testConnectLocal(s.getPid());
   828             testConnectLocal(s.getPid());
   928 
   829 
   929         } finally {
   830         } finally {
   930             s.stop();
   831             s.stop();