test/com/sun/tools/attach/BasicTests.java

Print this page
rev 9799 : Backed out changeset 2b5b8d82173e


  21  * questions.
  22  */
  23 
  24 import com.sun.tools.attach.*;
  25 import java.net.ServerSocket;
  26 import java.net.Socket;
  27 import java.io.IOException;
  28 import java.util.Properties;
  29 import java.util.List;
  30 import java.io.File;
  31 import jdk.testlibrary.OutputAnalyzer;
  32 import jdk.testlibrary.JDKToolLauncher;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.ProcessThread;
  35 
  36 /*
  37  * @test
  38  * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
  39  * @summary Basic unit tests for the VM attach mechanism.
  40  * @library /lib/testlibrary
  41  * @run build Agent BadAgent RedefineAgent Application Shutdown RedefineDummy RunnerUtil
  42  * @run main BasicTests
  43  *
  44  * This test will perform a number of basic attach tests.
  45  */
  46 public class BasicTests {
  47 
  48     /*
  49      * The actual test is in the nested class TestMain.
  50      * The responsibility of this class is to:
  51      * 1. Build all needed jars.
  52      * 2. Start the Application class in a separate process.
  53      * 3. Find the pid and shutdown port of the running Application.
  54      * 4. Launches the tests in nested class TestMain that will attach to the Application.
  55      * 5. Shut down the Application.
  56      */
  57     public static void main(String args[]) throws Throwable {
  58         final String pidFile = "TestsBasic.Application.pid";
  59         ProcessThread processThread = null;
  60         RunnerUtil.ProcessInfo info = null;
  61         try {


 221             // Now with an agent that does redefine.
 222 
 223             System.out.println(" - Re-attaching to application ...");
 224             vm = VirtualMachine.attach(pid);
 225 
 226             System.out.println(" - Test: End-to-end connection with RedefineAgent");
 227 
 228             ServerSocket ss2 = new ServerSocket(0);
 229             int port2 = ss2.getLocalPort();
 230 
 231             System.out.println(" - Loading RedefineAgent.jar into target VM ...");
 232             vm.loadAgent(redefineagent, Integer.toString(port2));
 233 
 234             System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
 235             Socket s2 = ss2.accept();
 236             System.out.println(" - Connected to RedefineAgent.");
 237 
 238             // Test 6 - list method should list the target VM
 239             System.out.println(" - Test: VirtualMachine.list");
 240             List<VirtualMachineDescriptor> l = VirtualMachine.list();

 241             boolean found = false;
 242             for (VirtualMachineDescriptor vmd: l) {
 243                 if (vmd.id().equals(pid)) {
 244                     found = true;
 245                     break;
 246                 }
 247             }
 248             if (found) {
 249                 System.out.println(" - " + pid + " found.");
 250             } else {
 251                 throw new RuntimeException(pid + " not found in VM list");
 252             }

 253 
 254             // test 7 - basic hashCode/equals tests
 255             System.out.println(" - Test: hashCode/equals");
 256 
 257             VirtualMachine vm1 = VirtualMachine.attach(pid);
 258             VirtualMachine vm2 = VirtualMachine.attach(pid);
 259             if (!vm1.equals(vm2)) {
 260                 throw new RuntimeException("virtual machines are not equal");
 261             }
 262             if (vm.hashCode() != vm.hashCode()) {
 263                 throw new RuntimeException("virtual machine hashCodes not equal");
 264             }
 265             System.out.println(" - hashCode/equals okay");
 266 
 267             // ---
 268             System.out.println(" - Cleaning up...");
 269             s.close();
 270             ss.close();
 271             s2.close();
 272             ss2.close();


  21  * questions.
  22  */
  23 
  24 import com.sun.tools.attach.*;
  25 import java.net.ServerSocket;
  26 import java.net.Socket;
  27 import java.io.IOException;
  28 import java.util.Properties;
  29 import java.util.List;
  30 import java.io.File;
  31 import jdk.testlibrary.OutputAnalyzer;
  32 import jdk.testlibrary.JDKToolLauncher;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.ProcessThread;
  35 
  36 /*
  37  * @test
  38  * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
  39  * @summary Basic unit tests for the VM attach mechanism.
  40  * @library /lib/testlibrary
  41  * @run build Agent BadAgent RedefineAgent Application Shutdown RedefineDummy
  42  * @run main BasicTests
  43  *
  44  * This test will perform a number of basic attach tests.
  45  */
  46 public class BasicTests {
  47 
  48     /*
  49      * The actual test is in the nested class TestMain.
  50      * The responsibility of this class is to:
  51      * 1. Build all needed jars.
  52      * 2. Start the Application class in a separate process.
  53      * 3. Find the pid and shutdown port of the running Application.
  54      * 4. Launches the tests in nested class TestMain that will attach to the Application.
  55      * 5. Shut down the Application.
  56      */
  57     public static void main(String args[]) throws Throwable {
  58         final String pidFile = "TestsBasic.Application.pid";
  59         ProcessThread processThread = null;
  60         RunnerUtil.ProcessInfo info = null;
  61         try {


 221             // Now with an agent that does redefine.
 222 
 223             System.out.println(" - Re-attaching to application ...");
 224             vm = VirtualMachine.attach(pid);
 225 
 226             System.out.println(" - Test: End-to-end connection with RedefineAgent");
 227 
 228             ServerSocket ss2 = new ServerSocket(0);
 229             int port2 = ss2.getLocalPort();
 230 
 231             System.out.println(" - Loading RedefineAgent.jar into target VM ...");
 232             vm.loadAgent(redefineagent, Integer.toString(port2));
 233 
 234             System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
 235             Socket s2 = ss2.accept();
 236             System.out.println(" - Connected to RedefineAgent.");
 237 
 238             // Test 6 - list method should list the target VM
 239             System.out.println(" - Test: VirtualMachine.list");
 240             List<VirtualMachineDescriptor> l = VirtualMachine.list();
 241             if (!l.isEmpty()) {
 242                 boolean found = false;
 243                 for (VirtualMachineDescriptor vmd: l) {
 244                     if (vmd.id().equals(pid)) {
 245                         found = true;
 246                         break;
 247                     }
 248                 }
 249                 if (found) {
 250                     System.out.println(" - " + pid + " found.");
 251                 } else {
 252                     throw new RuntimeException(pid + " not found in VM list");
 253                 }
 254             }
 255 
 256             // test 7 - basic hashCode/equals tests
 257             System.out.println(" - Test: hashCode/equals");
 258 
 259             VirtualMachine vm1 = VirtualMachine.attach(pid);
 260             VirtualMachine vm2 = VirtualMachine.attach(pid);
 261             if (!vm1.equals(vm2)) {
 262                 throw new RuntimeException("virtual machines are not equal");
 263             }
 264             if (vm.hashCode() != vm.hashCode()) {
 265                 throw new RuntimeException("virtual machine hashCodes not equal");
 266             }
 267             System.out.println(" - hashCode/equals okay");
 268 
 269             // ---
 270             System.out.println(" - Cleaning up...");
 271             s.close();
 272             ss.close();
 273             s2.close();
 274             ss2.close();