jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java
changeset 22975 1a8002758a3d
parent 22349 2aa385c1c4d8
child 23004 09124a80451a
equal deleted inserted replaced
22974:4bf6c0d73bb8 22975:1a8002758a3d
    23 
    23 
    24 import java.io.File;
    24 import java.io.File;
    25 import java.io.IOException;
    25 import java.io.IOException;
    26 import java.lang.reflect.Method;
    26 import java.lang.reflect.Method;
    27 import java.net.ConnectException;
    27 import java.net.ConnectException;
       
    28 import java.net.ServerSocket;
    28 import java.rmi.NoSuchObjectException;
    29 import java.rmi.NoSuchObjectException;
    29 import java.rmi.registry.LocateRegistry;
    30 import java.rmi.registry.LocateRegistry;
    30 import java.rmi.registry.Registry;
    31 import java.rmi.registry.Registry;
    31 import java.util.ArrayList;
    32 import java.util.ArrayList;
    32 import java.util.Arrays;
    33 import java.util.Arrays;
   253     }
   254     }
   254 
   255 
   255     private static List<Failure> failures = new ArrayList<>();
   256     private static List<Failure> failures = new ArrayList<>();
   256 
   257 
   257     public static void main(String args[]) throws Exception {
   258     public static void main(String args[]) throws Exception {
   258         for (int i=0;i<3;i++) {
   259         for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
   259             System.out.println("=== PASS " + i + " ===");
   260             if (m.getName().startsWith("test_")) {
   260             for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
   261                 try {
   261                 if (m.getName().startsWith("test_")) {
   262                     m.invoke(null);
   262                     try {
   263                     System.out.println("=== PASSED\n");
   263                         m.invoke(null);
   264                 } catch (Throwable e) {
   264                         System.out.println("=== PASSED\n");
   265                     failures.add(new Failure(e, m.getName() + " failed"));
   265                     } catch (Throwable e) {
       
   266                         failures.add(new Failure(e, m.getName() + " failed"));
       
   267                     }
       
   268                 }
   266                 }
   269             }
   267             }
   270 
   268         }
   271             if (!failures.isEmpty()) {
   269 
   272                 for(Failure f : failures) {
   270         if (!failures.isEmpty()) {
   273                     System.err.println(f.getMsg());
   271             for(Failure f : failures) {
   274                     f.getCause().printStackTrace(System.err);
   272                 System.err.println(f.getMsg());
   275                 }
   273                 f.getCause().printStackTrace(System.err);
   276                 throw new Error();
   274             }
   277             }
   275             throw new Error();
   278         }
   276         }
   279     }
   277     }
   280 
   278 
   281     /**
   279     /**
   282      * Retrieves the PID of the test application using JCMD
   280      * Retrieves the PID of the test application using JCMD
   369             "-cp",
   367             "-cp",
   370             System.getProperty("test.class.path")
   368             System.getProperty("test.class.path")
   371         ));
   369         ));
   372         pbArgs.addAll(Arrays.asList(args));
   370         pbArgs.addAll(Arrays.asList(args));
   373         pbArgs.add("JMXStartStopDoSomething");
   371         pbArgs.add("JMXStartStopDoSomething");
       
   372 
   374         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   373         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   375             pbArgs.toArray(new String[pbArgs.size()])
   374             pbArgs.toArray(new String[pbArgs.size()])
   376         );
   375         );
   377         Something s = new Something(pb, name);
   376         Something s = new Something(pb, name);
   378         s.start();
   377         s.start();
   594                 "jmxremote.ssl=false");
   593                 "jmxremote.ssl=false");
   595 
   594 
   596             jcmd(CMD_STOP);
   595             jcmd(CMD_STOP);
   597             jcmd(CMD_STOP);
   596             jcmd(CMD_STOP);
   598 
   597 
       
   598             ServerSocket ss = new ServerSocket(0);
       
   599 
   599             jcmd(
   600             jcmd(
   600                 line -> {
   601                 line -> {
   601                     if (line.contains("Port already in use: 22")) {
   602                     if (line.contains("Port already in use: " + ss.getLocalPort())) {
   602                         checks[2] = true;
   603                         checks[2] = true;
   603                     }
   604                     }
   604                 },
   605                 },
   605                 CMD_START,
   606                 CMD_START,
   606                 "jmxremote.port=22",
   607                 "jmxremote.port=" + ss.getLocalPort(),
   607                 "jmxremote.rmi.port=" + port2,
   608                 "jmxremote.rmi.port=" + port2,
   608                 "jmxremote.authenticate=false",
   609                 "jmxremote.authenticate=false",
   609                 "jmxremote.ssl=false");
   610                 "jmxremote.ssl=false");
   610             if (!checks[0]) {
   611             if (!checks[0]) {
   611                 throw new Exception("Starting agent on port " + port1 + " should " +
   612                 throw new Exception("Starting agent on port " + port1 + " should " +
   614             if (!checks[1]) {
   615             if (!checks[1]) {
   615                 throw new Exception("Starting agent on poprt " + port2 + " should " +
   616                 throw new Exception("Starting agent on poprt " + port2 + " should " +
   616                                     "report an invalid agent state");
   617                                     "report an invalid agent state");
   617             }
   618             }
   618             if (!checks[2]) {
   619             if (!checks[2]) {
   619                 throw new Exception("Starting agent on port 22 should " +
   620                 throw new Exception("Starting agent on port " + ss.getLocalPort() + " should " +
   620                                     "report port in use");
   621                                     "report port in use");
   621             }
   622             }
   622         } finally {
   623         } finally {
   623             s.stop();
   624             s.stop();
   624         }
   625         }
   625     }
   626     }
   626 
   627 
   627     private static void test_07() throws Exception {
   628     private static void test_07() throws Exception {
   628         // Run an app without JMX enabled, but with some properties set
   629         // Run an app without JMX enabled, but with some properties set
   629         // in command line.
   630         // in command line.
   630         // make sure these properties overriden corectly
   631         // make sure these properties overridden corectly
   631 
   632 
   632         System.out.println("**** Test seven ****");
   633         System.out.println("**** Test seven ****");
   633 
   634 
   634         Something s = doSomething(
   635         Something s = doSomething(
   635             "test_07",
   636             "test_07",
   652 
   653 
   653     private static void test_08() throws Exception {
   654     private static void test_08() throws Exception {
   654         // Run an app with JMX enabled and with some properties set
   655         // Run an app with JMX enabled and with some properties set
   655         // in command line.
   656         // in command line.
   656         // stop JMX agent and then start it again with different property values
   657         // stop JMX agent and then start it again with different property values
   657         // make sure these properties overriden corectly
   658         // make sure these properties overridden corectly
   658 
   659 
   659         System.out.println("**** Test eight ****");
   660         System.out.println("**** Test eight ****");
   660 
   661 
   661         Something s = doSomething(
   662         Something s = doSomething(
   662             "test_08",
   663             "test_08",
   688         // Run an app with JMX enabled and with some properties set
   689         // Run an app with JMX enabled and with some properties set
   689         // in command line.
   690         // in command line.
   690         // stop JMX agent and then start it again with different property values
   691         // stop JMX agent and then start it again with different property values
   691         // specifing some property in management config file and some of them
   692         // specifing some property in management config file and some of them
   692         // in command line
   693         // in command line
   693         // make sure these properties overriden corectly
   694         // make sure these properties overridden corectly
   694 
   695 
   695         System.out.println("**** Test nine ****");
   696         System.out.println("**** Test nine ****");
   696 
   697 
   697         Something s = doSomething("test_09",
   698         Something s = doSomething("test_09",
   698             "-Dcom.sun.management.config.file=" +
   699             "-Dcom.sun.management.config.file=" +
   723     private static void test_10() throws Exception {
   724     private static void test_10() throws Exception {
   724         // Run an app with JMX enabled and with some properties set
   725         // Run an app with JMX enabled and with some properties set
   725         // in command line.
   726         // in command line.
   726         // stop JMX agent and then start it again with different property values
   727         // stop JMX agent and then start it again with different property values
   727         // stop JMX agent again and then start it without property value
   728         // stop JMX agent again and then start it without property value
   728         // make sure these properties overriden corectly
   729         // make sure these properties overridden corectly
   729 
   730 
   730         System.out.println("**** Test ten ****");
   731         System.out.println("**** Test ten ****");
   731 
   732 
   732         Something s = doSomething(
   733         Something s = doSomething(
   733             "test_10",
   734             "test_10",
   798 
   799 
   799     private static void test_13() throws Exception {
   800     private static void test_13() throws Exception {
   800         // Run an app with -javaagent make sure it works as expected -
   801         // Run an app with -javaagent make sure it works as expected -
   801         // system properties are ignored
   802         // system properties are ignored
   802 
   803 
   803         System.out.println("**** Test fourteen ****");
   804         System.out.println("**** Test thirteen ****");
   804 
   805 
   805         String agent = TEST_JDK + "/jre/lib/management-agent.jar";
   806         String agent = TEST_JDK + "/jre/lib/management-agent.jar";
   806         if (!new File(agent).exists()) {
   807         if (!new File(agent).exists()) {
   807             agent = TEST_JDK + "/lib/management-agent.jar";
   808             agent = TEST_JDK + "/lib/management-agent.jar";
   808         }
   809         }