File: JMXStartStopTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (863 lines) | stat: -rw-r--r-- 27,978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.BindException;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.management.*;
import javax.management.remote.*;
import javax.net.ssl.SSLHandshakeException;

import jdk.testlibrary.ProcessTools;
import jdk.testlibrary.Utils;
import jdk.internal.agent.Agent;
import jdk.internal.agent.AgentConfigurationError;
import jdk.internal.agent.ConnectorAddressLink;

/**
 * @test
 * @bug 7110104
 * @key randomness intermittent
 * @summary Makes sure that enabling/disabling the management agent through JCMD
 *          achieves the desired results
 *
 * @library /lib/testlibrary
 * @modules java.management
 *          java.rmi
 *          jdk.management.agent/jdk.internal.agent
 *
 * @build jdk.testlibrary.* JMXStartStopTest PortAllocator TestApp ManagementAgentJcmd
 * @run main/othervm/timeout=600 -XX:+UsePerfData JMXStartStopTest
 */
public class JMXStartStopTest {
    private static final String TEST_APP_NAME = "TestApp";

    private static final String TEST_SRC = System.getProperty("test.src");

    private static final boolean verbose = false;

    private static ManagementAgentJcmd jcmd = new ManagementAgentJcmd(TEST_APP_NAME, verbose);

    private static void dbg_print(String msg) {
        if (verbose) {
            System.out.println("DBG: " + msg);
        }
    }

    private static int listMBeans(MBeanServerConnection server,
            ObjectName pattern,
            QueryExp query)
            throws Exception {

        Set<ObjectName> names = server.queryNames(pattern,query);
        for (ObjectName name : names) {
            MBeanInfo info = server.getMBeanInfo(name);
            dbg_print("Got MBean: " + name);

            MBeanAttributeInfo[] attrs = info.getAttributes();
            if (attrs == null)
                continue;
            for (MBeanAttributeInfo attr : attrs) {
                if (attr.isReadable()) {
                    server.getAttribute(name, attr.getName());
                }
            }
        }
        return names.size();
    }

    private static void testConnectLocal(long pid)
            throws Exception {

        String jmxUrlStr = null;

        try {
            jmxUrlStr = ConnectorAddressLink.importFrom((int)pid);
            dbg_print("Local Service URL: " +jmxUrlStr);
            if ( jmxUrlStr == null ) {
                throw new Exception("No Service URL. Local agent not started?");
            }

            JMXServiceURL url = new JMXServiceURL(jmxUrlStr);

            JMXConnector c = JMXConnectorFactory.connect(url, null);

            MBeanServerConnection conn = c.getMBeanServerConnection();
            ObjectName pattern = new ObjectName("java.lang:type=Memory,*");

            int count = listMBeans(conn,pattern,null);
            if (count == 0)
                throw new Exception("Expected at least one matching "+
                                    "MBean for "+pattern);

        } catch (IOException e) {
            dbg_print("Cannot find process : " + pid);
            throw e;
        }
    }

    private static void testNoConnect(int port) throws Exception {
        testNoConnect(port, 0);
    }

    private static void testNoConnect(int port, int rmiPort) throws Exception {
        try {
            testConnect(port, rmiPort);
            throw new Exception("Didn't expect the management agent running");
        } catch (Exception e) {
            Throwable t = e;
            while (t != null) {
                if (t instanceof RemoteException ||
                    t instanceof SSLHandshakeException ||
                    t instanceof ConnectException) {
                    break;
                }
                t = t.getCause();
            }
            if (t == null) {
                throw new Exception("Unexpected exception", e);
            }
        }
    }

    private static void testConnect(int port) throws Exception {
        testConnect(port, 0);
    }

    private static void testConnect(int port, int rmiPort) throws Exception {
        EOFException lastException = null;
        // factor adjusted timeout (5 seconds) for the RMI to become available
        long timeout = System.currentTimeMillis() + Utils.adjustTimeout(5000);
        do {
            try {
                doTestConnect(port, rmiPort);
                lastException = null;
            } catch (EOFException e) {
                lastException = e;
                System.out.println("Error establishing RMI connection. Retrying in 500ms.");
                Thread.sleep(500);
            }
        } while (lastException != null && System.currentTimeMillis() < timeout);

        if (lastException != null) {
            // didn't manage to get the RMI running in time
            // rethrow the exception
            throw lastException;
        }
    }

    private static void doTestConnect(int port, int rmiPort) throws Exception {
        dbg_print("RmiRegistry lookup...");

        dbg_print("Using port: " + port);

        dbg_print("Using rmi port: " + rmiPort);

        Registry registry = LocateRegistry.getRegistry(port);

        // "jmxrmi"
        String[] relist = registry.list();
        for (int i = 0; i < relist.length; ++i) {
            dbg_print("Got registry: " + relist[i]);
        }

        String jmxUrlStr = (rmiPort != 0) ?
            String.format(
                        "service:jmx:rmi://localhost:%d/jndi/rmi://localhost:%d/jmxrmi",
                        rmiPort,
                port) :
            String.format(
                        "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
                        port);

        JMXServiceURL url = new JMXServiceURL(jmxUrlStr);

        JMXConnector c = JMXConnectorFactory.connect(url, null);

        MBeanServerConnection conn = c.getMBeanServerConnection();
        ObjectName pattern = new ObjectName("java.lang:type=Memory,*");

        int count = listMBeans(conn,pattern,null);
        if (count == 0)
            throw new Exception("Expected at least one matching " +
                                "MBean for " + pattern);
    }

    private static class Failure {
        private final Throwable cause;
        private final String msg;

        public Failure(Throwable cause, String msg) {
            this.cause = cause;
            this.msg = msg;
        }

        public Failure(String msg) {
            this(null, msg);
        }

        public Throwable getCause() {
            return cause;
        }

        public String getMsg() {
            return msg;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 97 * hash + Objects.hashCode(this.cause);
            hash = 97 * hash + Objects.hashCode(this.msg);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Failure other = (Failure) obj;
            if (!Objects.equals(this.cause, other.cause)) {
                return false;
            }
            if (!Objects.equals(this.msg, other.msg)) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            if (cause != null) {
                return msg + "\n" + cause;
            } else {
                return msg;
            }
        }
    }

    private static List<Failure> failures = new ArrayList<>();

    public static void main(String args[]) throws Exception {
        for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
            if (m.getName().startsWith("test_")) {
                long t1 = System.currentTimeMillis();
                try {
                    boolean retry = false;
                    do {
                        try {
                            m.invoke(null);
                            retry = false;
                        } catch (InvocationTargetException e) {
                            if (e.getCause() instanceof BindException ||
                                e.getCause() instanceof java.rmi.ConnectException) {
                                System.out.println("Failed to allocate ports. Retrying ...");
                                retry = true;
                            } else {
                                throw e;
                            }
                        }
                    } while (retry);
                    System.out.println("=== PASSED");
                } catch (Throwable e) {
                    failures.add(new Failure(e, m.getName() + " failed"));
                } finally {
                    System.out.println("(took " + (System.currentTimeMillis() - t1) + "ms)\n");
                }
            }
        }

        if (!failures.isEmpty()) {
            for(Failure f : failures) {
                System.err.println(f.getMsg());
                f.getCause().printStackTrace(System.err);
            }
            throw new Error();
        }
    }

    private static class TestAppRun {
        private Process p;
        private final ProcessBuilder pb;
        private final String name;
        private final AtomicBoolean started = new AtomicBoolean(false);
        private volatile long pid = -1;

        public TestAppRun(ProcessBuilder pb, String name) {
            this.pb = pb;
            this.name = name;
        }

        public synchronized void start() throws InterruptedException, IOException, TimeoutException {
            if (started.compareAndSet(false, true)) {
                try {
                    AtomicBoolean error = new AtomicBoolean(false);
                    p = ProcessTools.startProcess(
                            TEST_APP_NAME + "{" + name + "}",
                            pb,
                            (line) -> {
                                boolean ok = line.equals("main enter");
                                error.set(line.contains("BindException"));

                                return ok || error.get();
                            }
                    );
                    if (error.get()) {
                        throw new BindException("Starting process failed due to " +
                                                "the requested port not being available");
                    }
                    pid = p.pid();
                } catch (TimeoutException e) {
                    if (p != null) {
                        p.destroy();
                        p.waitFor();
                    }
                    throw e;
                }
            }
        }

        public long getPid() {
            return pid;
        }

        public synchronized void stop()
                throws IOException, InterruptedException {
            if (started.compareAndSet(true, false)) {
                p.getOutputStream().write(0);
                p.getOutputStream().flush();
                int ec = p.waitFor();
                if (ec != 0) {
                    StringBuilder msg = new StringBuilder();
                    msg.append("Test application '").append(name);
                    msg.append("' failed with exit code: ");
                    msg.append(ec);

                    failures.add(new Failure(msg.toString()));
                }
            }
        }
    }

    /**
     * Runs the test application "TestApp"
     * @param name Test run name
     * @param args Additional arguments
     * @return Returns a {@linkplain TestAppRun} instance representing the run
     * @throws IOException
     * @throws InterruptedException
     * @throws TimeoutException
     */
    private static TestAppRun doTest(String name, String ... args)
            throws Exception {
        List<String> pbArgs = new ArrayList<>(Arrays.asList(
                "-cp",
                System.getProperty("test.class.path"),
                "-XX:+UsePerfData"
        ));
        pbArgs.addAll(Arrays.asList(args));
        pbArgs.add(TEST_APP_NAME);

        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
                pbArgs.toArray(new String[pbArgs.size()])
        );
        TestAppRun s = new TestAppRun(pb, name);
        s.start();
        return s;
    }

    static void test_01() throws Exception {
        // Run an app with JMX enabled stop it and
        // restart on other port

        System.out.println("**** Test one ****");
        int ports[] = PortAllocator.allocatePorts(2);

        TestAppRun s = doTest(
                "test_01",
                "-Dcom.sun.management.jmxremote.port=" + ports[0],
                "-Dcom.sun.management.jmxremote.authenticate=false",
                "-Dcom.sun.management.jmxremote.ssl=false");

        try {
            testConnect(ports[0]);

            jcmd.stop();
            testNoConnect(ports[0]);

            jcmd.start("jmxremote.port=" + ports[1]);
            testConnect(ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_02() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd

        System.out.println("**** Test two ****");

        int[] ports = PortAllocator.allocatePorts(1);
        TestAppRun s = doTest("test_02");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[0]);
        } finally {
//            debugPortUsage(pa);
            s.stop();
        }
    }

    static void test_03() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd on one port than on other one

        System.out.println("**** Test three ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_03");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            // Second agent shouldn't start
            jcmd.start(
                "jmxremote.port=" + ports[1],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            // First agent should connect
            testConnect(ports[0]);

            // Second agent should not connect
            testNoConnect(ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_04() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd on one port, specify rmi port explicitly

        System.out.println("**** Test four ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_04");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.rmi.port=" + ports[1],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[0], ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_05() throws Exception {
        // Run an app without JMX enabled, it will enable local server
        // but should leave remote server disabled

        System.out.println("**** Test five ****");
        int[] ports = PortAllocator.allocatePorts(1);
        TestAppRun s = doTest("test_05");
        try {
            jcmd.startLocal();

            testNoConnect(ports[0]);
            testConnectLocal(s.getPid());
        } finally {
            s.stop();
        }
    }

    static void test_06() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd on one port, specify rmi port explicitly
        // attempt to start it again with the same port
        // Check for valid messages in the output

        System.out.println("**** Test six ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_06");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[0], ports[1]);

            final AtomicBoolean checks = new AtomicBoolean(false);
            jcmd.start(
                line -> {
                    if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
                        checks.set(true);
                    }
                },
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            if (!checks.get()) {
                throw new Exception("Starting agent on port " + ports[0] + " should "
                        + "report an invalid agent state");
            }
        } finally {
            s.stop();
        }
    }

    static void test_07() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd on one port, specify rmi port explicitly
        // attempt to start it again with other port
        // Check for valid messages in the output

        System.out.println("**** Test seven ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_07");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[0], ports[1]);

            final AtomicBoolean checks = new AtomicBoolean(false);

            jcmd.start(
                line -> {
                    if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
                        checks.set(true);
                    }
                },
                "jmxremote.port=" + ports[1],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            if (!checks.get()) {
                throw new Exception("Starting agent on poprt " + ports[1] + " should "
                        + "report an invalid agent state");
            }
        } finally {
            s.stop();
        }
    }

    static void test_08() throws Exception {
        // Run an app without JMX enabled
        // start JMX by jcmd on one port, specify rmi port explicitly
        // attempt to stop it twice
        // Check for valid messages in the output

        System.out.println("**** Test eight ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_08");
        try {
            jcmd.start(
                "jmxremote.port=" + ports[0],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[0], ports[1]);

            jcmd.stop();
            jcmd.stop();
        } finally {
            s.stop();
        }
    }

    static void test_09() throws Exception {
        // Run an app without JMX enabled
        // attempt to start JMX using a non-available port
        // Check for valid messages in the output

        System.out.println("**** Test nine ****");

        TestAppRun s = doTest("test_09");

        try (ServerSocket ss = new ServerSocket(0)) {
            int localPort = ss.getLocalPort();
            int[] ports;
            do {
                ports = PortAllocator.allocatePorts(1);
            } while (localPort == ports[0]);

            final AtomicBoolean checks = new AtomicBoolean(false);

            int retryCntr = 1;
            do {
                final AtomicBoolean retry = new AtomicBoolean(false);

                try {
                    jcmd.start(
                        line -> {
                            if (line.contains(Agent.getText(AgentConfigurationError.AGENT_EXCEPTION))) {
                                retry.set(true);
                            }
                        },
                        "jmxremote.port=" + ports[0],
                        "jmxremote.rmi.port=" + localPort,
                        "jmxremote.authenticate=false",
                        "jmxremote.ssl=false"
                    );
                } catch (BindException e) {
                    checks.set(true);
                }
                if (!retry.get()) {
                    break;
                }
                System.out.println("Attempt " + retryCntr + " >>>");
                System.out.println("Unexpected reply from the agent. Retrying in 500ms ...");
                Thread.sleep(500);
            } while (retryCntr++ < 10);

            if (!checks.get()) {
                throw new Exception("Starting agent on port " + ports[0] + " should "
                        + "report port in use");
            }
        } finally {
            s.stop();
        }

    }

    static void test_10() throws Exception {
        // Run an app without JMX enabled, but with some properties set
        // in command line.
        // make sure these properties overridden corectly

        System.out.println("**** Test ten ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest(
                "test_10",
                "-Dcom.sun.management.jmxremote.authenticate=false",
                "-Dcom.sun.management.jmxremote.ssl=true");

        try {
            testNoConnect(ports[0]);
            jcmd.start(
                "jmxremote.port=" + ports[1],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );
            testConnect(ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_11() throws Exception {
        // Run an app with JMX enabled and with some properties set
        // in command line.
        // stop JMX agent and then start it again with different property values
        // make sure these properties overridden corectly

        System.out.println("**** Test eleven ****");
        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest(
                "test_11",
                "-Dcom.sun.management.jmxremote.port=" + ports[0],
                "-Dcom.sun.management.jmxremote.authenticate=false",
                "-Dcom.sun.management.jmxremote.ssl=true");

        try {
            testNoConnect(ports[0]);

            jcmd.stop();

            testNoConnect(ports[0]);

            jcmd.start(
                "jmxremote.port=" + ports[1],
                "jmxremote.authenticate=false",
                "jmxremote.ssl=false"
            );

            testConnect(ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_12() throws Exception {
        // Run an app with JMX enabled and with some properties set
        // in command line.
        // stop JMX agent and then start it again with different property values
        // specifing some property in management config file and some of them
        // in command line
        // make sure these properties overridden corectly

        System.out.println("**** Test twelve ****");

        int[] ports = PortAllocator.allocatePorts(2);
        TestAppRun s = doTest("test_12",
                "-Dcom.sun.management.config.file="
                + TEST_SRC + File.separator + "management_cl.properties",
                "-Dcom.sun.management.jmxremote.authenticate=false"
        );

        try {
            testNoConnect(ports[0]);

            jcmd.stop();

            testNoConnect(ports[0]);

            jcmd.start(
                "config.file=" + TEST_SRC + File.separator
                + "management_jcmd.properties",
                "jmxremote.authenticate=false",
                "jmxremote.port=" + ports[1]
            );

            testConnect(ports[1]);
        } finally {
            s.stop();
        }
    }

    static void test_13() throws Exception {
        // Run an app with JMX enabled and with some properties set
        // in command line.
        // stop JMX agent and then start it again with different property values
        // stop JMX agent again and then start it without property value
        // make sure these properties overridden corectly

        System.out.println("**** Test thirteen ****");
        int[] ports = PortAllocator.allocatePorts(1);
        TestAppRun s = doTest(
                "test_13",
                "-Dcom.sun.management.jmxremote.port=" + ports[0],
                "-Dcom.sun.management.jmxremote.authenticate=false",
                "-Dcom.sun.management.jmxremote.ssl=true");

        try {
            testNoConnect(ports[0]);

            jcmd.stop();
            jcmd.start(
                "jmxremote.ssl=false",
                "jmxremote.port=" + ports[0]
            );
            testConnect(ports[0]);

            jcmd.stop();
            jcmd.start(
                "jmxremote.port=" + ports[0]
            );

            testNoConnect(ports[0]);
        } finally {
            s.stop();
        }
    }

    static void test_14() throws Exception {
        // Run an app with JMX enabled
        // stop remote agent
        // make sure local agent is not affected

        System.out.println("**** Test fourteen ****");
        int[] ports = PortAllocator.allocatePorts(1);
        TestAppRun s = doTest(
                "test_14",
                "-Dcom.sun.management.jmxremote.port=" + ports[0],
                "-Dcom.sun.management.jmxremote.authenticate=false",
                "-Dcom.sun.management.jmxremote.ssl=false");
        try {
            testConnect(ports[0]);
            jcmd.stop();
            testConnectLocal(s.getPid());
        } finally {
            s.stop();
        }
    }

    static void test_15() throws Exception {
        // Run an app with JMX disabled
        // start local agent only

        System.out.println("**** Test fifteen ****");

        int[] ports = PortAllocator.allocatePorts(1);
        TestAppRun s = doTest("test_15");

        try {
            testNoConnect(ports[0]);
            jcmd.startLocal();

            testConnectLocal(s.getPid());

        } finally {
            s.stop();
        }
    }

}