jdk/test/com/sun/tools/attach/BasicTests.java
author duke
Wed, 05 Jul 2017 16:38:40 +0200
changeset 782 f2e6591ff695
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Unit test for Attach API. Attaches to the given VM and performs a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * unit tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class BasicTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        String pid = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        String agent = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        String badagent = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        String redefineagent = args[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        System.out.println(" - Attaching to application ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        VirtualMachine vm = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        // Test 1 - read the system properties from the target VM and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // check that property is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println(" - Test: system properties in target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Properties props = vm.getSystemProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String value = props.getProperty("attach.test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (value == null || !value.equals("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new RuntimeException("attach.test property not set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println(" - attach.test property set as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Test 1a - read the agent properties from the target VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // By default, the agent property contains "sun.java.command",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // "sun.jvm.flags", and "sun.jvm.args".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Just sanity check - make sure not empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println(" - Test: agent properties in target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        props = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (props == null || props.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new RuntimeException("Agent properties is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.out.println(" - agent properties non-empty as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Test 2 - attempt to load an agent that does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println(" - Test: Load an agent that does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            vm.loadAgent("SilverBullet.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (AgentLoadException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println(" - AgentLoadException thrown as expected!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Test 3 - load an "bad" agent (agentmain throws an exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println(" - Test: Load a bad agent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            vm.loadAgent(badagent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.out.println(" - AgentInitializationException throws as expected!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // Test 4 - detach from the VM and attempt a load (should throw IOE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.out.println(" - Test: Detach from VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            vm.loadAgent(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new RuntimeException("loadAgent did not throw an exception!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println(" - IOException as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Test 5 - functional "end-to-end" test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Create a listener socket. Load Agent.jar into the target VM passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // it the port number of our listener. When agent loads it should connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // back to the tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println(" - Re-attaching to application ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        vm = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println(" - Test: End-to-end connection with agent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println(" - Loading Agent.jar into target VM ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        vm.loadAgent(agent, Integer.toString(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println(" - Waiting for agent to connect back to tool ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println(" - Connected to agent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Test 5b - functional "end-to-end" test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Now with an agent that does redefine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.out.println(" - Re-attaching to application ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        vm = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println(" - Test: End-to-end connection with RedefineAgent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ServerSocket ss2 = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int port2 = ss2.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.out.println(" - Loading RedefineAgent.jar into target VM ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        vm.loadAgent(redefineagent, Integer.toString(port2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Socket s2 = ss2.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println(" - Connected to RedefineAgent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Test 6 - list method should list the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println(" - Test: VirtualMachine.list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        List<VirtualMachineDescriptor> l = VirtualMachine.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (!l.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (VirtualMachineDescriptor vmd: l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (vmd.id().equals(pid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                System.out.println(" - " + pid + " found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                throw new RuntimeException(pid + " not found in VM list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // test 7 - basic hashCode/equals tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println(" - Test: hashCode/equals");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        VirtualMachine vm1 = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        VirtualMachine vm2 = VirtualMachine.attach(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (!vm1.equals(vm2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new RuntimeException("virtual machines are not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (vm.hashCode() != vm.hashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RuntimeException("virtual machine hashCodes not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        System.out.println(" - hashCode/equals okay");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.out.println(" - Cleaning up...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ss2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}