1 /*
   2  * Copyright (c) 2012, 2015, 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.File;
  25 import java.io.IOException;
  26 import java.lang.reflect.InvocationTargetException;
  27 import java.lang.reflect.Method;
  28 import java.net.BindException;
  29 import java.net.ConnectException;
  30 import java.net.ServerSocket;
  31 import java.rmi.RemoteException;
  32 import java.rmi.registry.LocateRegistry;
  33 import java.rmi.registry.Registry;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.List;
  37 import java.util.Objects;
  38 import java.util.Random;
  39 import java.util.Set;
  40 import java.util.concurrent.TimeUnit;
  41 import java.util.concurrent.TimeoutException;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import java.util.function.Consumer;
  44 import java.util.stream.Collectors;
  45 
  46 import javax.management.*;
  47 import javax.management.remote.*;
  48 import javax.net.ssl.SSLHandshakeException;
  49 
  50 import jdk.testlibrary.ProcessTools;
  51 import jdk.testlibrary.JDKToolLauncher;
  52 import sun.management.Agent;
  53 import sun.management.AgentConfigurationError;
  54 
  55 /**
  56  * @test
  57  * @bug 7110104
  58  * @library /lib/testlibrary
  59  * @build jdk.testlibrary.* JMXStartStopTest JMXStartStopDoSomething
  60  * @run main/othervm/timeout=600 -XX:+UsePerfData JMXStartStopTest
  61  * @summary Makes sure that enabling/disabling the management agent through JCMD
  62  *          achieves the desired results
  63  * @key randomness
  64  */
  65 public class JMXStartStopTest {
  66 
  67     private static final String TEST_SRC = System.getProperty("test.src");
  68 
  69     private static final boolean verbose = false;
  70 
  71     /**
  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 
 100     private static void dbg_print(String msg) {
 101         if (verbose) {
 102             System.out.println("DBG: " + msg);
 103         }
 104     }
 105 
 106     private static int listMBeans(MBeanServerConnection server,
 107             ObjectName pattern,
 108             QueryExp query)
 109             throws Exception {
 110 
 111         Set<ObjectName> names = server.queryNames(pattern,query);
 112         for (ObjectName name : names) {
 113             MBeanInfo info = server.getMBeanInfo(name);
 114             dbg_print("Got MBean: " + name);
 115 
 116             MBeanAttributeInfo[] attrs = info.getAttributes();
 117             if (attrs == null)
 118                 continue;
 119             for (MBeanAttributeInfo attr : attrs) {
 120                 if (attr.isReadable()) {
 121                     server.getAttribute(name, attr.getName());
 122                 }
 123             }
 124         }
 125         return names.size();
 126     }
 127 
 128     private static void testConnectLocal(long pid)
 129             throws Exception {
 130 
 131         String jmxUrlStr = null;
 132 
 133         try {
 134             jmxUrlStr = sun.management.ConnectorAddressLink.importFrom((int)pid);
 135             dbg_print("Local Service URL: " +jmxUrlStr);
 136             if ( jmxUrlStr == null ) {
 137                 throw new Exception("No Service URL. Local agent not started?");
 138             }
 139 
 140             JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
 141 
 142             JMXConnector c = JMXConnectorFactory.connect(url, null);
 143 
 144             MBeanServerConnection conn = c.getMBeanServerConnection();
 145             ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
 146 
 147             int count = listMBeans(conn,pattern,null);
 148             if (count == 0)
 149                 throw new Exception("Expected at least one matching "+
 150                                     "MBean for "+pattern);
 151 
 152         } catch (IOException e) {
 153             dbg_print("Cannot find process : " + pid);
 154             throw e;
 155         }
 156     }
 157 
 158     private static void testNoConnect(int port) throws Exception {
 159         testNoConnect(port, 0);
 160     }
 161 
 162     private static void testNoConnect(int port, int rmiPort) throws Exception {
 163         try {
 164             testConnect(port, rmiPort);
 165             throw new Exception("Didn't expect the management agent running");
 166         } catch (Exception e) {
 167             Throwable t = e;
 168             while (t != null) {
 169                 if (t instanceof RemoteException ||
 170                     t instanceof SSLHandshakeException ||
 171                     t instanceof ConnectException) {
 172                     break;
 173                 }
 174                 t = t.getCause();
 175             }
 176             if (t == null) {
 177                 throw new Exception("Unexpected exception", e);
 178             }
 179         }
 180     }
 181 
 182     private static void testConnect(int port) throws Exception {
 183         testConnect(port, 0);
 184     }
 185 
 186     private static void testConnect(int port, int rmiPort) throws Exception {
 187 
 188         dbg_print("RmiRegistry lookup...");
 189 
 190         dbg_print("Using port: " + port);
 191 
 192         dbg_print("Using rmi port: " + rmiPort);
 193 
 194         Registry registry = LocateRegistry.getRegistry(port);
 195 
 196         // "jmxrmi"
 197         String[] relist = registry.list();
 198         for (int i = 0; i < relist.length; ++i) {
 199             dbg_print("Got registry: " + relist[i]);
 200         }
 201 
 202         String jmxUrlStr = (rmiPort != 0) ?
 203             String.format(
 204                         "service:jmx:rmi://localhost:%d/jndi/rmi://localhost:%d/jmxrmi",
 205                         rmiPort,
 206                 port) :
 207             String.format(
 208                         "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
 209                         port);
 210 
 211         JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
 212 
 213         JMXConnector c = JMXConnectorFactory.connect(url, null);
 214 
 215         MBeanServerConnection conn = c.getMBeanServerConnection();
 216         ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
 217 
 218         int count = listMBeans(conn,pattern,null);
 219         if (count == 0)
 220             throw new Exception("Expected at least one matching " +
 221                                 "MBean for " + pattern);
 222     }
 223 
 224     private static class Failure {
 225         private final Throwable cause;
 226         private final String msg;
 227 
 228         public Failure(Throwable cause, String msg) {
 229             this.cause = cause;
 230             this.msg = msg;
 231         }
 232 
 233         public Failure(String msg) {
 234             this(null, msg);
 235         }
 236 
 237         public Throwable getCause() {
 238             return cause;
 239         }
 240 
 241         public String getMsg() {
 242             return msg;
 243         }
 244 
 245         @Override
 246         public int hashCode() {
 247             int hash = 7;
 248             hash = 97 * hash + Objects.hashCode(this.cause);
 249             hash = 97 * hash + Objects.hashCode(this.msg);
 250             return hash;
 251         }
 252 
 253         @Override
 254         public boolean equals(Object obj) {
 255             if (obj == null) {
 256                 return false;
 257             }
 258             if (getClass() != obj.getClass()) {
 259                 return false;
 260             }
 261             final Failure other = (Failure) obj;
 262             if (!Objects.equals(this.cause, other.cause)) {
 263                 return false;
 264             }
 265             if (!Objects.equals(this.msg, other.msg)) {
 266                 return false;
 267             }
 268             return true;
 269         }
 270 
 271         @Override
 272         public String toString() {
 273             if (cause != null) {
 274                 return msg + "\n" + cause;
 275             } else {
 276                 return msg;
 277             }
 278         }
 279     }
 280 
 281     private static List<Failure> failures = new ArrayList<>();
 282 
 283     public static void main(String args[]) throws Exception {
 284         for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
 285             if (m.getName().startsWith("test_")) {
 286                 long t1 = System.currentTimeMillis();
 287                 try {
 288                     boolean retry = false;
 289                     do {
 290                         try {
 291                             m.invoke(null);
 292                             retry = false;
 293                         } catch (InvocationTargetException e) {
 294                             if (e.getCause() instanceof BindException ||
 295                                 e.getCause() instanceof java.rmi.ConnectException) {
 296                                 System.out.println("Failed to allocate ports. Retrying ...");
 297                                 retry = true;
 298                             } else {
 299                                 throw e;
 300                             }
 301                         }
 302                     } while (retry);
 303                     System.out.println("=== PASSED");
 304                 } catch (Throwable e) {
 305                     failures.add(new Failure(e, m.getName() + " failed"));
 306                 } finally {
 307                     System.out.println("(took " + (System.currentTimeMillis() - t1) + "ms)\n");
 308                 }
 309             }
 310         }
 311 
 312         if (!failures.isEmpty()) {
 313             for(Failure f : failures) {
 314                 System.err.println(f.getMsg());
 315                 f.getCause().printStackTrace(System.err);
 316             }
 317             throw new Error();
 318         }
 319     }
 320 
 321     private static class Something {
 322         private Process p;
 323         private final ProcessBuilder pb;
 324         private final String name;
 325         private final AtomicBoolean started = new AtomicBoolean(false);
 326         private volatile long pid = -1;
 327 
 328         public Something(ProcessBuilder pb, String name) {
 329             this.pb = pb;
 330             this.name = name;
 331         }
 332 
 333         public synchronized void start() throws InterruptedException, IOException, TimeoutException {
 334             if (started.compareAndSet(false, true)) {
 335                 try {
 336                     AtomicBoolean error = new AtomicBoolean(false);
 337                     p = ProcessTools.startProcess(
 338                             "JMXStartStopDoSomething{" + name + "}",
 339                             pb,
 340                             (line) -> {
 341                                 boolean ok = line.equals("main enter");
 342                                 error.set(line.contains("BindException"));
 343 
 344                                 return ok || error.get();
 345                             },
 346                             5,
 347                             TimeUnit.SECONDS
 348                     );
 349                     if (error.get()) {
 350                         throw new BindException("Starting process failed due to " +
 351                                                 "the requested port not being available");
 352                     }
 353                     pid = p.getPid();
 354                 } catch (TimeoutException e) {
 355                     p.destroy();
 356                     p.waitFor();
 357                     throw e;
 358                 }
 359             }
 360         }
 361 
 362         public long getPid() {
 363             return pid;
 364         }
 365 
 366         public synchronized void stop()
 367                 throws IOException, InterruptedException {
 368             if (started.compareAndSet(true, false)) {
 369                 p.getOutputStream().write(0);
 370                 p.getOutputStream().flush();
 371                 int ec = p.waitFor();
 372                 if (ec != 0) {
 373                     StringBuilder msg = new StringBuilder();
 374                     msg.append("Test application '").append(name);
 375                     msg.append("' failed with exit code: ");
 376                     msg.append(ec);
 377 
 378                     failures.add(new Failure(msg.toString()));
 379                 }
 380             }
 381         }
 382     }
 383 
 384     /**
 385      * Runs the test application "JMXStartStopDoSomething"
 386      * @param name Test run name
 387      * @param args Additional arguments
 388      * @return Returns a {@linkplain Something} instance representing the run
 389      * @throws IOException
 390      * @throws InterruptedException
 391      * @throws TimeoutException
 392      */
 393     private static Something doSomething(String name, String ... args)
 394             throws Exception {
 395         List<String> pbArgs = new ArrayList<>(Arrays.asList(
 396                 "-cp",
 397                 System.getProperty("test.class.path")
 398         ));
 399         pbArgs.addAll(Arrays.asList(args));
 400         pbArgs.add("JMXStartStopDoSomething");
 401 
 402         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 403                 pbArgs.toArray(new String[pbArgs.size()])
 404         );
 405         Something s = new Something(pb, name);
 406         s.start();
 407         return s;
 408     }
 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 
 484     static void test_01() throws Exception {
 485         // Run an app with JMX enabled stop it and
 486         // restart on other port
 487 
 488         System.out.println("**** Test one ****");
 489         int ports[] = PortAllocator.allocatePorts(2);
 490 
 491         Something s = doSomething(
 492                 "test_01",
 493                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
 494                 "-Dcom.sun.management.jmxremote.authenticate=false",
 495                 "-Dcom.sun.management.jmxremote.ssl=false");
 496 
 497         try {
 498             testConnect(ports[0]);
 499 
 500             jcmd(CMD_STOP);
 501             testNoConnect(ports[0]);
 502 
 503             jcmd(CMD_START, "jmxremote.port=" + ports[1]);
 504             testConnect(ports[1]);
 505         } finally {
 506             s.stop();
 507         }
 508     }
 509 
 510     static void test_02() throws Exception {
 511         // Run an app without JMX enabled
 512         // start JMX by jcmd
 513 
 514         System.out.println("**** Test two ****");
 515 
 516         int[] ports = PortAllocator.allocatePorts(1);
 517         Something s = doSomething("test_02");
 518         try {
 519             jcmd(CMD_START,
 520                     "jmxremote.port=" + ports[0],
 521                     "jmxremote.authenticate=false",
 522                     "jmxremote.ssl=false");
 523 
 524             testConnect(ports[0]);
 525         } finally {
 526 //            debugPortUsage(pa);
 527             s.stop();
 528         }
 529     }
 530 
 531     static void test_03() throws Exception {
 532         // Run an app without JMX enabled
 533         // start JMX by jcmd on one port than on other one
 534 
 535         System.out.println("**** Test three ****");
 536 
 537         int[] ports = PortAllocator.allocatePorts(2);
 538         Something s = doSomething("test_03");
 539         try {
 540             jcmd(CMD_START,
 541                     "jmxremote.port=" + ports[0],
 542                     "jmxremote.authenticate=false",
 543                     "jmxremote.ssl=false");
 544 
 545             // Second agent shouldn't start
 546             jcmd(CMD_START,
 547                     "jmxremote.port=" + ports[1],
 548                     "jmxremote.authenticate=false",
 549                     "jmxremote.ssl=false");
 550 
 551             // First agent should connect
 552             testConnect(ports[0]);
 553 
 554             // Second agent should not connect
 555             testNoConnect(ports[1]);
 556         } finally {
 557             s.stop();
 558         }
 559     }
 560 
 561     static void test_04() throws Exception {
 562         // Run an app without JMX enabled
 563         // start JMX by jcmd on one port, specify rmi port explicitly
 564 
 565         System.out.println("**** Test four ****");
 566 
 567         int[] ports = PortAllocator.allocatePorts(2);
 568         Something s = doSomething("test_04");
 569         try {
 570             jcmd(CMD_START,
 571                     "jmxremote.port=" + ports[0],
 572                     "jmxremote.rmi.port=" + ports[1],
 573                     "jmxremote.authenticate=false",
 574                     "jmxremote.ssl=false");
 575 
 576             testConnect(ports[0], ports[1]);
 577         } finally {
 578             s.stop();
 579         }
 580     }
 581 
 582     static void test_05() throws Exception {
 583         // Run an app without JMX enabled, it will enable local server
 584         // but should leave remote server disabled
 585 
 586         System.out.println("**** Test five ****");
 587         int[] ports = PortAllocator.allocatePorts(1);
 588         Something s = doSomething("test_05");
 589         try {
 590             jcmd(CMD_START_LOCAL);
 591 
 592             testNoConnect(ports[0]);
 593             testConnectLocal(s.getPid());
 594         } finally {
 595             s.stop();
 596         }
 597     }
 598 
 599     static void test_06() throws Exception {
 600         // Run an app without JMX enabled
 601         // start JMX by jcmd on one port, specify rmi port explicitly
 602         // attempt to start it again with the same port
 603         // Check for valid messages in the output
 604 
 605         System.out.println("**** Test six ****");
 606 
 607         int[] ports = PortAllocator.allocatePorts(2);
 608         Something s = doSomething("test_06");
 609         try {
 610             jcmd(CMD_START,
 611                     "jmxremote.port=" + ports[0],
 612                     "jmxremote.authenticate=false",
 613                     "jmxremote.ssl=false");
 614 
 615             testConnect(ports[0], ports[1]);
 616 
 617             final AtomicBoolean checks = new AtomicBoolean(false);
 618             jcmd(
 619                     line -> {
 620                         if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
 621                             checks.set(true);
 622                         }
 623                     },
 624                     CMD_START,
 625                     "jmxremote.port=" + ports[0],
 626                     "jmxremote.authenticate=false",
 627                     "jmxremote.ssl=false");
 628 
 629             if (!checks.get()) {
 630                 throw new Exception("Starting agent on port " + ports[0] + " should "
 631                         + "report an invalid agent state");
 632             }
 633         } finally {
 634             s.stop();
 635         }
 636     }
 637 
 638     static void test_07() throws Exception {
 639         // Run an app without JMX enabled
 640         // start JMX by jcmd on one port, specify rmi port explicitly
 641         // attempt to start it again with other port
 642         // Check for valid messages in the output
 643 
 644         System.out.println("**** Test seven ****");
 645 
 646         int[] ports = PortAllocator.allocatePorts(2);
 647         Something s = doSomething("test_07");
 648         try {
 649             jcmd(CMD_START,
 650                     "jmxremote.port=" + ports[0],
 651                     "jmxremote.authenticate=false",
 652                     "jmxremote.ssl=false");
 653 
 654             testConnect(ports[0], ports[1]);
 655 
 656             final AtomicBoolean checks = new AtomicBoolean(false);
 657 
 658             jcmd(
 659                     line -> {
 660                         if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
 661                             checks.set(true);
 662                         }
 663                     },
 664                     CMD_START,
 665                     "jmxremote.port=" + ports[1],
 666                     "jmxremote.authenticate=false",
 667                     "jmxremote.ssl=false");
 668 
 669             if (!checks.get()) {
 670                 throw new Exception("Starting agent on poprt " + ports[1] + " should "
 671                         + "report an invalid agent state");
 672             }
 673         } finally {
 674             s.stop();
 675         }
 676     }
 677 
 678     static void test_08() throws Exception {
 679         // Run an app without JMX enabled
 680         // start JMX by jcmd on one port, specify rmi port explicitly
 681         // attempt to stop it twice
 682         // Check for valid messages in the output
 683 
 684         System.out.println("**** Test eight ****");
 685 
 686         int[] ports = PortAllocator.allocatePorts(2);
 687         Something s = doSomething("test_08");
 688         try {
 689             jcmd(CMD_START,
 690                     "jmxremote.port=" + ports[0],
 691                     "jmxremote.authenticate=false",
 692                     "jmxremote.ssl=false");
 693 
 694             testConnect(ports[0], ports[1]);
 695 
 696             jcmd(CMD_STOP);
 697             jcmd(CMD_STOP);
 698         } finally {
 699             s.stop();
 700         }
 701     }
 702 
 703     static void test_09() throws Exception {
 704         // Run an app without JMX enabled
 705         // attempt to start JMX using a non-available port
 706         // Check for valid messages in the output
 707 
 708         System.out.println("**** Test nine ****");
 709 
 710         Something s = doSomething("test_09");
 711 
 712         try (ServerSocket ss = new ServerSocket(0)) {
 713             int localPort = ss.getLocalPort();
 714             int[] ports;
 715             do {
 716                 ports = PortAllocator.allocatePorts(1);
 717             } while (localPort == ports[0]);
 718 
 719             final AtomicBoolean checks = new AtomicBoolean(false);
 720 
 721             int retryCntr = 1;
 722             do {
 723                 final AtomicBoolean retry = new AtomicBoolean(false);
 724 
 725                 try {
 726                     jcmd(
 727                         line -> {
 728                             if (line.contains(Agent.getText(AgentConfigurationError.AGENT_EXCEPTION))) {
 729                                 retry.set(true);
 730                             }
 731                         },
 732                         CMD_START,
 733                         "jmxremote.port=" + ports[0],
 734                         "jmxremote.rmi.port=" + localPort,
 735                         "jmxremote.authenticate=false",
 736                         "jmxremote.ssl=false"
 737                     );
 738                 } catch (BindException e) {
 739                     checks.set(true);
 740                 }
 741                 if (!retry.get()) {
 742                     break;
 743                 }
 744                 System.out.println("Attempt " + retryCntr + " >>>");
 745                 System.out.println("Unexpected reply from the agent. Retrying in 500ms ...");
 746                 Thread.sleep(500);
 747             } while (retryCntr++ < 10);
 748 
 749             if (!checks.get()) {
 750                 throw new Exception("Starting agent on port " + ports[0] + " should "
 751                         + "report port in use");
 752             }
 753         } finally {
 754             s.stop();
 755         }
 756 
 757     }
 758 
 759     static void test_10() throws Exception {
 760         // Run an app without JMX enabled, but with some properties set
 761         // in command line.
 762         // make sure these properties overridden corectly
 763 
 764         System.out.println("**** Test ten ****");
 765 
 766         int[] ports = PortAllocator.allocatePorts(2);
 767         Something s = doSomething(
 768                 "test_10",
 769                 "-Dcom.sun.management.jmxremote.authenticate=false",
 770                 "-Dcom.sun.management.jmxremote.ssl=true");
 771 
 772         try {
 773             testNoConnect(ports[0]);
 774             jcmd(
 775                     CMD_START,
 776                     "jmxremote.port=" + ports[1],
 777                     "jmxremote.authenticate=false",
 778                     "jmxremote.ssl=false"
 779             );
 780             testConnect(ports[1]);
 781         } finally {
 782             s.stop();
 783         }
 784     }
 785 
 786     static void test_11() throws Exception {
 787         // Run an app with JMX enabled and with some properties set
 788         // in command line.
 789         // stop JMX agent and then start it again with different property values
 790         // make sure these properties overridden corectly
 791 
 792         System.out.println("**** Test eleven ****");
 793         int[] ports = PortAllocator.allocatePorts(2);
 794         Something s = doSomething(
 795                 "test_11",
 796                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
 797                 "-Dcom.sun.management.jmxremote.authenticate=false",
 798                 "-Dcom.sun.management.jmxremote.ssl=true");
 799 
 800         try {
 801             testNoConnect(ports[0]);
 802 
 803             jcmd(CMD_STOP);
 804 
 805             testNoConnect(ports[0]);
 806 
 807             jcmd(
 808                     CMD_START,
 809                     "jmxremote.port=" + ports[1],
 810                     "jmxremote.authenticate=false",
 811                     "jmxremote.ssl=false"
 812             );
 813 
 814             testConnect(ports[1]);
 815         } finally {
 816             s.stop();
 817         }
 818     }
 819 
 820     static void test_12() throws Exception {
 821         // Run an app with JMX enabled and with some properties set
 822         // in command line.
 823         // stop JMX agent and then start it again with different property values
 824         // specifing some property in management config file and some of them
 825         // in command line
 826         // make sure these properties overridden corectly
 827 
 828         System.out.println("**** Test twelve ****");
 829 
 830         int[] ports = PortAllocator.allocatePorts(2);
 831         Something s = doSomething("test_12",
 832                 "-Dcom.sun.management.config.file="
 833                 + TEST_SRC + File.separator + "management_cl.properties",
 834                 "-Dcom.sun.management.jmxremote.authenticate=false"
 835         );
 836 
 837         try {
 838             testNoConnect(ports[0]);
 839 
 840             jcmd(CMD_STOP);
 841 
 842             testNoConnect(ports[0]);
 843 
 844             jcmd(CMD_START,
 845                     "config.file=" + TEST_SRC + File.separator
 846                     + "management_jcmd.properties",
 847                     "jmxremote.authenticate=false",
 848                     "jmxremote.port=" + ports[1]
 849             );
 850 
 851             testConnect(ports[1]);
 852         } finally {
 853             s.stop();
 854         }
 855     }
 856 
 857     static void test_13() throws Exception {
 858         // Run an app with JMX enabled and with some properties set
 859         // in command line.
 860         // stop JMX agent and then start it again with different property values
 861         // stop JMX agent again and then start it without property value
 862         // make sure these properties overridden corectly
 863 
 864         System.out.println("**** Test thirteen ****");
 865         int[] ports = PortAllocator.allocatePorts(1);
 866         Something s = doSomething(
 867                 "test_13",
 868                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
 869                 "-Dcom.sun.management.jmxremote.authenticate=false",
 870                 "-Dcom.sun.management.jmxremote.ssl=true");
 871 
 872         try {
 873             testNoConnect(ports[0]);
 874 
 875             jcmd(CMD_STOP);
 876             jcmd(CMD_START,
 877                     "jmxremote.ssl=false",
 878                     "jmxremote.port=" + ports[0]
 879             );
 880             testConnect(ports[0]);
 881 
 882             jcmd(CMD_STOP);
 883             jcmd(CMD_START,
 884                     "jmxremote.port=" + ports[0]
 885             );
 886 
 887             testNoConnect(ports[0]);
 888         } finally {
 889             s.stop();
 890         }
 891     }
 892 
 893     static void test_14() throws Exception {
 894         // Run an app with JMX enabled
 895         // stop remote agent
 896         // make sure local agent is not affected
 897 
 898         System.out.println("**** Test fourteen ****");
 899         int[] ports = PortAllocator.allocatePorts(1);
 900         Something s = doSomething(
 901                 "test_14",
 902                 "-Dcom.sun.management.jmxremote.port=" + ports[0],
 903                 "-Dcom.sun.management.jmxremote.authenticate=false",
 904                 "-Dcom.sun.management.jmxremote.ssl=false");
 905         try {
 906             testConnect(ports[0]);
 907             jcmd(CMD_STOP);
 908             testConnectLocal(s.getPid());
 909         } finally {
 910             s.stop();
 911         }
 912     }
 913 
 914     static void test_15() throws Exception {
 915         // Run an app with JMX disabled
 916         // start local agent only
 917 
 918         System.out.println("**** Test fifteen ****");
 919 
 920         int[] ports = PortAllocator.allocatePorts(1);
 921         Something s = doSomething("test_15");
 922 
 923         try {
 924             testNoConnect(ports[0]);
 925             jcmd(CMD_START + "_local");
 926 
 927             testConnectLocal(s.getPid());
 928 
 929         } finally {
 930             s.stop();
 931         }
 932     }
 933 
 934 }