1 /*
   2  * Copyright (c) 2012, 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.Method;
  27 import java.net.ConnectException;
  28 import java.net.ServerSocket;
  29 import java.rmi.NoSuchObjectException;
  30 import java.rmi.registry.LocateRegistry;
  31 import java.rmi.registry.Registry;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.HashMap;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.Objects;
  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.concurrent.atomic.AtomicReference;
  44 import java.util.function.Consumer;
  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 
  53 /**
  54  * @test
  55  * @bug 7110104
  56  * @library /lib/testlibrary
  57  * @build jdk.testlibrary.ProcessTools
  58  * @build jdk.testlibrary.JDKToolLauncher
  59  * @build jdk.testlibrary.Utils
  60  * @build JMXStartStopTest JMXStartStopDoSomething
  61  * @run main/othervm JMXStartStopTest
  62  * @summary Makes sure that enabling/disabling the management agent through
  63  *          JCMD achieves the desired results
  64  */
  65 public class JMXStartStopTest {
  66     private static final String TEST_JDK = System.getProperty("test.jdk");
  67     private static final String TEST_SRC = System.getProperty("test.src");
  68 
  69     private static final boolean verbose = false;
  70 
  71     /**
  72      * Dynamically allocates two distinct ports using {@linkplain java.net.ServerSocket}
  73      * It keeps each of those ports blocked until it is first accessed by its getter
  74      */
  75     private static class PortAllocator {
  76         private final int port1, port2;
  77         private final ServerSocket ss1, ss2;
  78         PortAllocator() {
  79             try {
  80                 ss1 = new ServerSocket(0);
  81                 ss2 = new ServerSocket(0);
  82                 port1 = ss1.getLocalPort();
  83                 port2 = ss2.getLocalPort();
  84             } catch (IOException e) {
  85                 throw new Error("Error while obtaining free ports", e);
  86             }
  87         }
  88 
  89         public int getPort1() {
  90             if (!ss1.isClosed()) {
  91                 try {
  92                     ss1.close();
  93                 } catch (IOException e) {
  94                     // just ignore
  95                 }
  96             }
  97             return port1;
  98         }
  99 
 100         public int getPort2() {
 101             if (!ss2.isClosed()) {
 102                 try {
 103                     ss2.close();
 104                 } catch (IOException e) {
 105                     // just ignore
 106                 }
 107             }
 108             return port2;
 109         }
 110     }
 111 
 112     private static void dbg_print(String msg){
 113         if (verbose) {
 114             System.out.println("DBG: " +msg);
 115         }
 116     }
 117 
 118     private static int listMBeans(MBeanServerConnection server,
 119                                   ObjectName pattern,
 120                                   QueryExp query)
 121     throws Exception {
 122 
 123         Set names = server.queryNames(pattern,query);
 124         for (Iterator i=names.iterator(); i.hasNext(); ) {
 125             ObjectName name = (ObjectName)i.next();
 126             MBeanInfo info = server.getMBeanInfo(name);
 127             dbg_print("Got MBean: " + name);
 128 
 129             MBeanAttributeInfo[] attrs = info.getAttributes();
 130             if (attrs == null)
 131                 continue;
 132             for (MBeanAttributeInfo attr : attrs) {
 133                 if (attr.isReadable()) {
 134                     Object o = server.getAttribute(name, attr.getName());
 135                 }
 136             }
 137         }
 138         return names.size();
 139     }
 140 
 141 
 142     private static void testConnectLocal(int pid)
 143     throws Exception {
 144 
 145         String jmxUrlStr = null;
 146 
 147         try {
 148             jmxUrlStr = sun.management.ConnectorAddressLink.importFrom(pid);
 149             dbg_print("Local Service URL: " +jmxUrlStr);
 150             if ( jmxUrlStr == null ) {
 151                 throw new Exception("No Service URL. Local agent not started?");
 152             }
 153 
 154             JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
 155             Map m = new HashMap();
 156 
 157             JMXConnector c = JMXConnectorFactory.connect(url,m);
 158 
 159             MBeanServerConnection conn = c.getMBeanServerConnection();
 160             ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
 161 
 162             int count = listMBeans(conn,pattern,null);
 163             if (count == 0)
 164                 throw new Exception("Expected at least one matching "+
 165                                     "MBean for "+pattern);
 166 
 167         } catch (IOException e) {
 168             dbg_print("Cannot find process : " + pid);
 169             throw e;
 170         }
 171     }
 172 
 173     private static void testNoConnect(int port) throws Exception {
 174         testNoConnect(port, 0);
 175     }
 176 
 177     private static void testNoConnect(int port, int rmiPort) throws Exception {
 178         try {
 179             testConnect(port, rmiPort);
 180             throw new Exception("Didn't expect the management agent running");
 181         } catch (Exception e) {
 182             Throwable t = e;
 183             while (t != null) {
 184                 if (t instanceof NoSuchObjectException ||
 185                     t instanceof ConnectException ||
 186                     t instanceof SSLHandshakeException) {
 187                     break;
 188                 }
 189                 t = t.getCause();
 190             }
 191             if (t == null) {
 192                 throw new Exception("Unexpected exception", e);
 193             }
 194         }
 195     }
 196 
 197     private static void testConnect(int port) throws Exception {
 198         testConnect(port, 0);
 199     }
 200 
 201     private static void testConnect(int port, int rmiPort) throws Exception {
 202 
 203         dbg_print("RmiRegistry lookup...");
 204 
 205         dbg_print("Using port: " + port);
 206 
 207         dbg_print("Using rmi port: " + rmiPort);
 208 
 209         Registry registry = LocateRegistry.getRegistry(port);
 210 
 211         // "jmxrmi"
 212         String[] relist = registry.list();
 213         for (int i = 0; i < relist.length; ++i) {
 214             dbg_print("Got registry: " + relist[i]);
 215         }
 216 
 217         String jmxUrlStr = (rmiPort != 0) ?
 218             String.format(
 219                 "service:jmx:rmi://localhost:%d/jndi/rmi://localhost:%d/jmxrmi",
 220                 rmiPort,
 221                 port) :
 222             String.format(
 223                 "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
 224                 port);
 225 
 226         JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
 227         Map m = new HashMap();
 228 
 229         JMXConnector c = JMXConnectorFactory.connect(url,m);
 230 
 231         MBeanServerConnection conn = c.getMBeanServerConnection();
 232         ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
 233 
 234         int count = listMBeans(conn,pattern,null);
 235         if (count == 0)
 236             throw new Exception("Expected at least one matching " +
 237                                 "MBean for " + pattern);
 238     }
 239 
 240     private static class Failure {
 241         private final Throwable cause;
 242         private final String msg;
 243 
 244         public Failure(Throwable cause, String msg) {
 245             this.cause = cause;
 246             this.msg = msg;
 247         }
 248 
 249         public Failure(String msg) {
 250             this(null, msg);
 251         }
 252 
 253         public Throwable getCause() {
 254             return cause;
 255         }
 256 
 257         public String getMsg() {
 258             return msg;
 259         }
 260 
 261         @Override
 262         public int hashCode() {
 263             int hash = 7;
 264             hash = 97 * hash + Objects.hashCode(this.cause);
 265             hash = 97 * hash + Objects.hashCode(this.msg);
 266             return hash;
 267         }
 268 
 269         @Override
 270         public boolean equals(Object obj) {
 271             if (obj == null) {
 272                 return false;
 273             }
 274             if (getClass() != obj.getClass()) {
 275                 return false;
 276             }
 277             final Failure other = (Failure) obj;
 278             if (!Objects.equals(this.cause, other.cause)) {
 279                 return false;
 280             }
 281             if (!Objects.equals(this.msg, other.msg)) {
 282                 return false;
 283             }
 284             return true;
 285         }
 286 
 287         @Override
 288         public String toString() {
 289             if (cause != null) {
 290                 return msg + "\n" + cause;
 291             } else {
 292                 return msg;
 293             }
 294         }
 295     }
 296 
 297     private static List<Failure> failures = new ArrayList<>();
 298 
 299     public static void main(String args[]) throws Exception {
 300         for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
 301             if (m.getName().startsWith("test_")) {
 302                 try {
 303                     m.invoke(null);
 304                     System.out.println("=== PASSED\n");
 305                 } catch (Throwable e) {
 306                     failures.add(new Failure(e, m.getName() + " failed"));
 307                 }
 308             }
 309         }
 310 
 311         if (!failures.isEmpty()) {
 312             for(Failure f : failures) {
 313                 System.err.println(f.getMsg());
 314                 f.getCause().printStackTrace(System.err);
 315             }
 316             throw new Error();
 317         }
 318     }
 319 
 320     /**
 321      * Retrieves the PID of the test application using JCMD
 322      * @return The PID of the test application
 323      * @throws InterruptedException
 324      * @throws IOException
 325      */
 326     private static String getPID() throws InterruptedException, IOException {
 327         final AtomicReference<String> pid = new AtomicReference<>();
 328         jcmd(
 329             null,
 330             line -> {
 331                 if (line.endsWith("JMXStartStopDoSomething")) {
 332                     pid.set(line.split(" ")[0]);
 333                 }
 334             }
 335         );
 336         return pid.get();
 337     }
 338 
 339     private static class Something {
 340         private Process p;
 341         private final ProcessBuilder pb;
 342         private final String name;
 343         private final AtomicBoolean started = new AtomicBoolean(false);
 344         private volatile int pid = -1;
 345 
 346         public Something(ProcessBuilder pb, String name) {
 347             this.pb = pb;
 348             this.name = name;
 349         }
 350 
 351         public synchronized void start() throws InterruptedException, IOException, TimeoutException {
 352             if (started.compareAndSet(false, true)) {
 353                 try {
 354                     p = ProcessTools.startProcess(
 355                         "JMXStartStopDoSomething",
 356                         pb,
 357                         (line) -> {
 358                             if (line.toLowerCase().startsWith("pid:")) {
 359                                 pid = Integer.parseInt(line.split("\\:")[1]);
 360                             }
 361                             return line.equals("main enter");
 362                         },
 363                         5,
 364                         TimeUnit.SECONDS
 365                     );
 366                 } catch (TimeoutException e) {
 367                     p.destroy();
 368                     p.waitFor();
 369                     throw e;
 370                 }
 371             }
 372         }
 373 
 374         public int getPid() {
 375             return pid;
 376         }
 377 
 378         public synchronized void stop()
 379             throws IOException, InterruptedException {
 380             if (started.compareAndSet(true, false)) {
 381                 p.getOutputStream().write(0);
 382                 p.getOutputStream().flush();
 383                 int ec = p.waitFor();
 384                 if (ec != 0) {
 385                     StringBuilder msg = new StringBuilder();
 386                     msg.append("Test application '").append(name);
 387                     msg.append("' failed with exit code: ");
 388                     msg.append(ec);
 389 
 390                     failures.add(new Failure(msg.toString()));
 391                 }
 392             }
 393         }
 394     }
 395 
 396     /**
 397      * Runs the test application "JMXStartStopDoSomething"
 398      * @param name Test run name
 399      * @param args Additional arguments
 400      * @return Returns a {@linkplain Something} instance representing the run
 401      * @throws IOException
 402      * @throws InterruptedException
 403      * @throws TimeoutException
 404      */
 405     private static Something doSomething(String name, String ... args)
 406     throws Exception {
 407         List<String> pbArgs = new ArrayList<>(Arrays.asList(
 408             "-cp",
 409             System.getProperty("test.class.path")
 410         ));
 411         pbArgs.addAll(Arrays.asList(args));
 412         pbArgs.add("JMXStartStopDoSomething");
 413 
 414         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 415             pbArgs.toArray(new String[pbArgs.size()])
 416         );
 417         Something s = new Something(pb, name);
 418         s.start();
 419         return s;
 420     }
 421 
 422     /**
 423      * Run the "jcmd" command
 424      *
 425      * @param command Command with parameters; space separated string
 426      * @throws IOException
 427      * @throws InterruptedException
 428      */
 429     private static void jcmd(String ... command) throws IOException, InterruptedException {
 430         if (command.length == 0) {
 431             jcmd(null, (Consumer<String>)null);
 432         } else {
 433             jcmd(null, command);
 434         }
 435     }
 436 
 437     /**
 438      * Run the "jcmd" command
 439      *
 440      * @param c {@linkplain Consumer} instance; may be null
 441      * @param command Command with parameters; space separated string
 442      * @throws IOException
 443      * @throws InterruptedException
 444      */
 445     private static void jcmd(Consumer<String> c, String ... command) throws IOException, InterruptedException {
 446         jcmd("JMXStartStopDoSomething", c, command);
 447     }
 448 
 449     /**
 450      * Run the "jcmd" command
 451      * @param target The target application name (or PID)
 452      * @param c {@linkplain Consumer} instance; may be null
 453      * @param command Command with parameters; space separated string
 454      * @throws IOException
 455      * @throws InterruptedException
 456      */
 457     private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
 458         dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));
 459 
 460         JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
 461         l.addToolArg(target);
 462         for(String cmd : command) {
 463             l.addToolArg(cmd);
 464         }
 465         Process p = ProcessTools.startProcess(
 466             "jcmd",
 467             new ProcessBuilder(l.getCommand()),
 468             c
 469         );
 470 
 471         p.waitFor();
 472         dbg_print("[jcmd] --------");
 473     }
 474 
 475     private static final String CMD_STOP = "ManagementAgent.stop";
 476     private static final String CMD_START= "ManagementAgent.start";
 477     private static final String CMD_START_LOCAL = "ManagementAgent.start_local";
 478 
 479     private static void test_01() throws Exception {
 480         // Run an app with JMX enabled stop it and
 481         // restart on other port
 482 
 483         System.out.println("**** Test one ****");
 484         PortAllocator pa = new PortAllocator();
 485 
 486         Something s = doSomething(
 487             "test_01",
 488             "-Dcom.sun.management.jmxremote.port=" + pa.getPort1(),
 489             "-Dcom.sun.management.jmxremote.authenticate=false",
 490             "-Dcom.sun.management.jmxremote.ssl=false");
 491 
 492         try {
 493             testConnect(pa.getPort1());
 494 
 495             jcmd(CMD_STOP);
 496             testNoConnect(pa.getPort1());
 497 
 498             jcmd(CMD_START, "jmxremote.port=" + pa.getPort2());
 499             testConnect(pa.getPort2());
 500         } finally {
 501             s.stop();
 502         }
 503     }
 504 
 505     private static void test_02() throws Exception {
 506         // Run an app without JMX enabled
 507         // start JMX by jcmd
 508 
 509         System.out.println("**** Test two ****");
 510 
 511         Something s = doSomething("test_02");
 512         PortAllocator pa = new PortAllocator();
 513         try {
 514             jcmd(CMD_START,
 515                 "jmxremote.port=" + pa.getPort1(),
 516                 "jmxremote.authenticate=false",
 517                 "jmxremote.ssl=false");
 518 
 519             testConnect(pa.getPort1());
 520         } finally {
 521 //            debugPortUsage(pa);
 522             s.stop();
 523         }
 524     }
 525 
 526     private static void test_03() throws Exception {
 527         // Run an app without JMX enabled
 528         // start JMX by jcmd on one port than on other one
 529 
 530         System.out.println("**** Test three ****");
 531 
 532         Something s = doSomething("test_03");
 533         PortAllocator pa = new PortAllocator();
 534         try {
 535             jcmd(CMD_START,
 536                 "jmxremote.port=" + pa.getPort1(),
 537                 "jmxremote.authenticate=false",
 538                 "jmxremote.ssl=false");
 539 
 540             // Second agent shouldn't start
 541             jcmd(CMD_START,
 542                 "jmxremote.port=" + pa.getPort2(),
 543                 "jmxremote.authenticate=false",
 544                 "jmxremote.ssl=false");
 545 
 546             // First agent should connect
 547             testConnect(pa.getPort1());
 548 
 549             // Second agent should not connect
 550             testNoConnect(pa.getPort2());
 551         } finally {
 552             s.stop();
 553         }
 554     }
 555 
 556     private static void test_04() throws Exception {
 557         // Run an app without JMX enabled
 558         // start JMX by jcmd on one port, specify rmi port explicitly
 559 
 560         System.out.println("**** Test four ****");
 561 
 562         Something s = doSomething("test_04");
 563         PortAllocator pa = new PortAllocator();
 564         try {
 565             jcmd(CMD_START,
 566                  "jmxremote.port=" + pa.getPort1(),
 567                  "jmxremote.rmi.port=" + pa.getPort2(),
 568                  "jmxremote.authenticate=false",
 569                  "jmxremote.ssl=false");
 570 
 571             testConnect(pa.getPort1(), pa.getPort2());
 572         } finally {
 573             s.stop();
 574         }
 575     }
 576 
 577     private static void test_05() throws Exception {
 578         // Run an app without JMX enabled, it will enable local server
 579         // but should leave remote server disabled
 580 
 581         System.out.println("**** Test five ****");
 582 
 583         Something s = doSomething("test_05");
 584         PortAllocator pa = new PortAllocator();
 585         try {
 586             jcmd(CMD_START_LOCAL);
 587 
 588             testNoConnect(pa.getPort1());
 589             testConnectLocal(s.getPid());
 590         } finally {
 591             s.stop();
 592         }
 593     }
 594 
 595     private static void test_06() throws Exception {
 596         // Run an app without JMX enabled
 597         // start JMX by jcmd on one port, specify rmi port explicitly
 598         // attempt to start it again
 599         // 1) with the same port
 600         // 2) with other port
 601         // 3) attempt to stop it twice
 602         // Check for valid messages in the output
 603 
 604         System.out.println("**** Test six ****");
 605 
 606         Something s = doSomething("test_06");
 607         PortAllocator pa = new PortAllocator();
 608         try {
 609             jcmd(CMD_START,
 610                  "jmxremote.port=" + pa.getPort1(),
 611                  "jmxremote.authenticate=false",
 612                  "jmxremote.ssl=false");
 613 
 614             testConnect(pa.getPort1(), pa.getPort2());
 615 
 616             final boolean[] checks = new boolean[3];
 617             jcmd(
 618                 line -> {
 619                     if (line.equals("java.lang.RuntimeException: Invalid agent state")) {
 620                         checks[0] = true;
 621                     }
 622                 },
 623                 CMD_START,
 624                  "jmxremote.port=" + pa.getPort1(),
 625                  "jmxremote.authenticate=false",
 626                  "jmxremote.ssl=false");
 627 
 628             jcmd(
 629                 line -> {
 630                     if (line.equals("java.lang.RuntimeException: Invalid agent state")) {
 631                         checks[1] = true;
 632                     }
 633                 },
 634                 CMD_START,
 635                 "jmxremote.port=" + pa.getPort2(),
 636                 "jmxremote.authenticate=false",
 637                 "jmxremote.ssl=false");
 638 
 639             jcmd(CMD_STOP);
 640             jcmd(CMD_STOP);
 641 
 642             ServerSocket ss = new ServerSocket(0);
 643 
 644             jcmd(
 645                 line -> {
 646                     if (line.contains("Port already in use: " + ss.getLocalPort())) {
 647                         checks[2] = true;
 648                     }
 649                 },
 650                 CMD_START,
 651                 "jmxremote.port=" + ss.getLocalPort(),
 652                 "jmxremote.rmi.port=" + pa.getPort2(),
 653                 "jmxremote.authenticate=false",
 654                 "jmxremote.ssl=false");
 655 
 656             if (!checks[0]) {
 657                 throw new Exception("Starting agent on port " + pa.getPort1() + " should " +
 658                                     "report an invalid agent state");
 659             }
 660             if (!checks[1]) {
 661                 throw new Exception("Starting agent on poprt " + pa.getPort2() + " should " +
 662                                     "report an invalid agent state");
 663             }
 664             if (!checks[2]) {
 665                 throw new Exception("Starting agent on port " + ss.getLocalPort() + " should " +
 666                                     "report port in use");
 667             }
 668         } finally {
 669             s.stop();
 670         }
 671     }
 672 
 673     private static void test_07() throws Exception {
 674         // Run an app without JMX enabled, but with some properties set
 675         // in command line.
 676         // make sure these properties overridden corectly
 677 
 678         System.out.println("**** Test seven ****");
 679 
 680         Something s = doSomething(
 681             "test_07",
 682             "-Dcom.sun.management.jmxremote.authenticate=false",
 683             "-Dcom.sun.management.jmxremote.ssl=true");
 684         PortAllocator pa = new PortAllocator();
 685 
 686         try {
 687             testNoConnect(pa.getPort1());
 688             jcmd(
 689                 CMD_START,
 690                 "jmxremote.port=" + pa.getPort2(),
 691                 "jmxremote.authenticate=false",
 692                 "jmxremote.ssl=false"
 693             );
 694             testConnect(pa.getPort2());
 695         } finally {
 696             s.stop();
 697         }
 698     }
 699 
 700     private static void test_08() throws Exception {
 701         // Run an app with JMX enabled and with some properties set
 702         // in command line.
 703         // stop JMX agent and then start it again with different property values
 704         // make sure these properties overridden corectly
 705 
 706         System.out.println("**** Test eight ****");
 707         PortAllocator pa = new PortAllocator();
 708 
 709         Something s = doSomething(
 710             "test_08",
 711             "-Dcom.sun.management.jmxremote.port=" + pa.getPort1(),
 712             "-Dcom.sun.management.jmxremote.authenticate=false",
 713             "-Dcom.sun.management.jmxremote.ssl=true");
 714 
 715         try {
 716             testNoConnect(pa.getPort1());
 717 
 718             jcmd(CMD_STOP);
 719 
 720             testNoConnect(pa.getPort1());
 721 
 722             jcmd(
 723                 CMD_START,
 724                 "jmxremote.port=" + pa.getPort2(),
 725                 "jmxremote.authenticate=false",
 726                 "jmxremote.ssl=false"
 727             );
 728 
 729             testConnect(pa.getPort2());
 730         } finally {
 731             s.stop();
 732         }
 733     }
 734 
 735     private static void test_09() throws Exception {
 736         // Run an app with JMX enabled and with some properties set
 737         // in command line.
 738         // stop JMX agent and then start it again with different property values
 739         // specifing some property in management config file and some of them
 740         // in command line
 741         // make sure these properties overridden corectly
 742 
 743         System.out.println("**** Test nine ****");
 744 
 745         Something s = doSomething("test_09",
 746             "-Dcom.sun.management.config.file=" +
 747                 TEST_SRC + File.separator + "management_cl.properties",
 748             "-Dcom.sun.management.jmxremote.authenticate=false"
 749         );
 750         PortAllocator pa = new PortAllocator();
 751 
 752         try {
 753             testNoConnect(pa.getPort1());
 754 
 755             jcmd(CMD_STOP);
 756 
 757             testNoConnect(pa.getPort1());
 758 
 759             jcmd(CMD_START,
 760                 "config.file=" + TEST_SRC + File.separator +
 761                     "management_jcmd.properties",
 762                 "jmxremote.authenticate=false",
 763                 "jmxremote.port=" + pa.getPort2()
 764             );
 765 
 766             testConnect(pa.getPort2());
 767         } finally {
 768             s.stop();
 769         }
 770     }
 771 
 772     private static void test_10() throws Exception {
 773         // Run an app with JMX enabled and with some properties set
 774         // in command line.
 775         // stop JMX agent and then start it again with different property values
 776         // stop JMX agent again and then start it without property value
 777         // make sure these properties overridden corectly
 778 
 779         System.out.println("**** Test ten ****");
 780         PortAllocator pa = new PortAllocator();
 781 
 782         Something s = doSomething(
 783             "test_10",
 784             "-Dcom.sun.management.jmxremote.port=" + pa.getPort1(),
 785             "-Dcom.sun.management.jmxremote.authenticate=false",
 786             "-Dcom.sun.management.jmxremote.ssl=true");
 787 
 788         try {
 789             testNoConnect(pa.getPort1());
 790 
 791             jcmd(CMD_STOP);
 792             jcmd(CMD_START,
 793                 "jmxremote.ssl=false",
 794                 "jmxremote.port=" + pa.getPort1()
 795             );
 796             testConnect(pa.getPort1());
 797 
 798             jcmd(CMD_STOP);
 799             jcmd(CMD_START,
 800                 "jmxremote.port=" + pa.getPort1()
 801             );
 802 
 803             testNoConnect(pa.getPort1());
 804         } finally {
 805             s.stop();
 806         }
 807     }
 808 
 809     private static void test_11() throws Exception {
 810         // Run an app with JMX enabled
 811         // stop remote agent
 812         // make sure local agent is not affected
 813 
 814         System.out.println("**** Test eleven ****");
 815         PortAllocator pa = new PortAllocator();
 816 
 817         Something s = doSomething(
 818             "test_11",
 819             "-Dcom.sun.management.jmxremote.port=" + pa.getPort1(),
 820             "-Dcom.sun.management.jmxremote.authenticate=false",
 821             "-Dcom.sun.management.jmxremote.ssl=false");
 822         try {
 823             testConnect(pa.getPort1());
 824             jcmd(CMD_STOP);
 825             testConnectLocal(s.getPid());
 826         } finally {
 827             s.stop();
 828         }
 829     }
 830 
 831     private static void test_12() throws Exception {
 832         // Run an app with JMX disabled
 833         // start local agent only
 834 
 835         System.out.println("**** Test twelve ****");
 836 
 837         Something s = doSomething("test_12");
 838         PortAllocator pa = new PortAllocator();
 839 
 840         try {
 841             testNoConnect(pa.getPort1());
 842             jcmd(CMD_START + "_local");
 843 
 844             testConnectLocal(s.getPid());
 845 
 846         } finally {
 847             s.stop();
 848         }
 849     }
 850 
 851     private static void test_13() throws Exception {
 852         // Run an app with -javaagent make sure it works as expected -
 853         // system properties are ignored
 854 
 855         System.out.println("**** Test thirteen ****");
 856         PortAllocator pa = new PortAllocator();
 857 
 858         String agent = TEST_JDK + "/jre/lib/management-agent.jar";
 859         if (!new File(agent).exists()) {
 860             agent = TEST_JDK + "/lib/management-agent.jar";
 861         }
 862 
 863         Something s = doSomething("test_14",
 864             "-javaagent:" + agent + "=com.sun.management.jmxremote.port=" +
 865                 pa.getPort1() + ",com.sun.management.jmxremote.authenticate=false",
 866             "-Dcom.sun.management.jmxremote.ssl=false"
 867         );
 868 
 869         try {
 870             testNoConnect(pa.port1);
 871         } finally {
 872             s.stop();
 873         }
 874     }
 875 }