2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
*
|
|
26 |
*
|
|
27 |
* Unit test for Attach API. Attaches to the given VM and performs a number
|
|
28 |
* unit tests.
|
|
29 |
*/
|
|
30 |
import com.sun.tools.attach.*;
|
|
31 |
import java.net.ServerSocket;
|
|
32 |
import java.net.Socket;
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.util.Properties;
|
|
35 |
import java.util.List;
|
|
36 |
|
|
37 |
public class BasicTests {
|
|
38 |
public static void main(String args[]) throws Exception {
|
|
39 |
String pid = args[0];
|
|
40 |
String agent = args[1];
|
|
41 |
String badagent = args[2];
|
|
42 |
String redefineagent = args[3];
|
|
43 |
|
|
44 |
System.out.println(" - Attaching to application ...");
|
|
45 |
VirtualMachine vm = VirtualMachine.attach(pid);
|
|
46 |
|
|
47 |
// Test 1 - read the system properties from the target VM and
|
|
48 |
// check that property is set
|
|
49 |
System.out.println(" - Test: system properties in target VM");
|
|
50 |
Properties props = vm.getSystemProperties();
|
|
51 |
String value = props.getProperty("attach.test");
|
|
52 |
if (value == null || !value.equals("true")) {
|
|
53 |
throw new RuntimeException("attach.test property not set");
|
|
54 |
}
|
|
55 |
System.out.println(" - attach.test property set as expected");
|
|
56 |
|
|
57 |
// Test 1a - read the agent properties from the target VM.
|
|
58 |
// By default, the agent property contains "sun.java.command",
|
|
59 |
// "sun.jvm.flags", and "sun.jvm.args".
|
|
60 |
// Just sanity check - make sure not empty.
|
|
61 |
System.out.println(" - Test: agent properties in target VM");
|
|
62 |
props = vm.getAgentProperties();
|
|
63 |
if (props == null || props.size() == 0) {
|
|
64 |
throw new RuntimeException("Agent properties is empty");
|
|
65 |
}
|
|
66 |
System.out.println(" - agent properties non-empty as expected");
|
|
67 |
|
|
68 |
// Test 2 - attempt to load an agent that does not exist
|
|
69 |
System.out.println(" - Test: Load an agent that does not exist");
|
|
70 |
try {
|
|
71 |
vm.loadAgent("SilverBullet.jar");
|
|
72 |
} catch (AgentLoadException x) {
|
|
73 |
System.out.println(" - AgentLoadException thrown as expected!");
|
|
74 |
}
|
|
75 |
|
|
76 |
// Test 3 - load an "bad" agent (agentmain throws an exception)
|
|
77 |
System.out.println(" - Test: Load a bad agent");
|
|
78 |
try {
|
|
79 |
vm.loadAgent(badagent);
|
|
80 |
} catch (AgentInitializationException x) {
|
|
81 |
System.out.println(" - AgentInitializationException throws as expected!");
|
|
82 |
}
|
|
83 |
|
|
84 |
// Test 4 - detach from the VM and attempt a load (should throw IOE)
|
|
85 |
System.out.println(" - Test: Detach from VM");
|
|
86 |
vm.detach();
|
|
87 |
try {
|
|
88 |
vm.loadAgent(agent);
|
|
89 |
throw new RuntimeException("loadAgent did not throw an exception!!");
|
|
90 |
} catch (IOException ioe) {
|
|
91 |
System.out.println(" - IOException as expected");
|
|
92 |
}
|
|
93 |
|
|
94 |
// Test 5 - functional "end-to-end" test.
|
|
95 |
// Create a listener socket. Load Agent.jar into the target VM passing
|
|
96 |
// it the port number of our listener. When agent loads it should connect
|
|
97 |
// back to the tool.
|
|
98 |
|
|
99 |
System.out.println(" - Re-attaching to application ...");
|
|
100 |
vm = VirtualMachine.attach(pid);
|
|
101 |
|
|
102 |
System.out.println(" - Test: End-to-end connection with agent");
|
|
103 |
|
|
104 |
ServerSocket ss = new ServerSocket(0);
|
|
105 |
int port = ss.getLocalPort();
|
|
106 |
|
|
107 |
System.out.println(" - Loading Agent.jar into target VM ...");
|
|
108 |
vm.loadAgent(agent, Integer.toString(port));
|
|
109 |
|
|
110 |
System.out.println(" - Waiting for agent to connect back to tool ...");
|
|
111 |
Socket s = ss.accept();
|
|
112 |
System.out.println(" - Connected to agent.");
|
|
113 |
|
|
114 |
// Test 5b - functional "end-to-end" test.
|
|
115 |
// Now with an agent that does redefine.
|
|
116 |
|
|
117 |
System.out.println(" - Re-attaching to application ...");
|
|
118 |
vm = VirtualMachine.attach(pid);
|
|
119 |
|
|
120 |
System.out.println(" - Test: End-to-end connection with RedefineAgent");
|
|
121 |
|
|
122 |
ServerSocket ss2 = new ServerSocket(0);
|
|
123 |
int port2 = ss2.getLocalPort();
|
|
124 |
|
|
125 |
System.out.println(" - Loading RedefineAgent.jar into target VM ...");
|
|
126 |
vm.loadAgent(redefineagent, Integer.toString(port2));
|
|
127 |
|
|
128 |
System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
|
|
129 |
Socket s2 = ss2.accept();
|
|
130 |
System.out.println(" - Connected to RedefineAgent.");
|
|
131 |
|
|
132 |
// Test 6 - list method should list the target VM
|
|
133 |
System.out.println(" - Test: VirtualMachine.list");
|
|
134 |
List<VirtualMachineDescriptor> l = VirtualMachine.list();
|
|
135 |
if (!l.isEmpty()) {
|
|
136 |
boolean found = false;
|
|
137 |
for (VirtualMachineDescriptor vmd: l) {
|
|
138 |
if (vmd.id().equals(pid)) {
|
|
139 |
found = true;
|
|
140 |
break;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
if (found) {
|
|
144 |
System.out.println(" - " + pid + " found.");
|
|
145 |
} else {
|
|
146 |
throw new RuntimeException(pid + " not found in VM list");
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
// test 7 - basic hashCode/equals tests
|
|
151 |
System.out.println(" - Test: hashCode/equals");
|
|
152 |
|
|
153 |
VirtualMachine vm1 = VirtualMachine.attach(pid);
|
|
154 |
VirtualMachine vm2 = VirtualMachine.attach(pid);
|
|
155 |
if (!vm1.equals(vm2)) {
|
|
156 |
throw new RuntimeException("virtual machines are not equal");
|
|
157 |
}
|
|
158 |
if (vm.hashCode() != vm.hashCode()) {
|
|
159 |
throw new RuntimeException("virtual machine hashCodes not equal");
|
|
160 |
}
|
|
161 |
System.out.println(" - hashCode/equals okay");
|
|
162 |
|
|
163 |
|
|
164 |
// ---
|
|
165 |
System.out.println(" - Cleaning up...");
|
|
166 |
s.close();
|
|
167 |
ss.close();
|
|
168 |
s2.close();
|
|
169 |
ss2.close();
|
|
170 |
}
|
|
171 |
}
|