author | dcubed |
Tue, 12 Apr 2011 13:36:38 -0700 | |
changeset 9259 | f63768ad58c5 |
parent 5506 | 202f599c92aa |
child 21968 | 619035df6988 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9259
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, 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"); |
|
9259
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
78 |
System.out.println("INFO: This test will cause error messages " |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
79 |
+ "to appear in the application log about SilverBullet.jar " |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
80 |
+ "not being found and an agent failing to start."); |
2 | 81 |
try { |
82 |
vm.loadAgent(badagent); |
|
9259
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
83 |
throw new RuntimeException( |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
84 |
"AgentInitializationException not thrown as expected!"); |
2 | 85 |
} catch (AgentInitializationException x) { |
9259
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
86 |
System.out.println( |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
87 |
" - AgentInitializationException thrown as expected!"); |
2 | 88 |
} |
89 |
||
90 |
// Test 4 - detach from the VM and attempt a load (should throw IOE) |
|
91 |
System.out.println(" - Test: Detach from VM"); |
|
9259
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
92 |
System.out.println("INFO: This test will cause error messages " |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
93 |
+ "to appear in the application log about a BadAgent including " |
f63768ad58c5
7035555: 4/4 attach/BasicTests.sh needs another tweak for Cygwin
dcubed
parents:
5506
diff
changeset
|
94 |
+ "a RuntimeException and an InvocationTargetException."); |
2 | 95 |
vm.detach(); |
96 |
try { |
|
97 |
vm.loadAgent(agent); |
|
98 |
throw new RuntimeException("loadAgent did not throw an exception!!"); |
|
99 |
} catch (IOException ioe) { |
|
100 |
System.out.println(" - IOException as expected"); |
|
101 |
} |
|
102 |
||
103 |
// Test 5 - functional "end-to-end" test. |
|
104 |
// Create a listener socket. Load Agent.jar into the target VM passing |
|
105 |
// it the port number of our listener. When agent loads it should connect |
|
106 |
// back to the tool. |
|
107 |
||
108 |
System.out.println(" - Re-attaching to application ..."); |
|
109 |
vm = VirtualMachine.attach(pid); |
|
110 |
||
111 |
System.out.println(" - Test: End-to-end connection with agent"); |
|
112 |
||
113 |
ServerSocket ss = new ServerSocket(0); |
|
114 |
int port = ss.getLocalPort(); |
|
115 |
||
116 |
System.out.println(" - Loading Agent.jar into target VM ..."); |
|
117 |
vm.loadAgent(agent, Integer.toString(port)); |
|
118 |
||
119 |
System.out.println(" - Waiting for agent to connect back to tool ..."); |
|
120 |
Socket s = ss.accept(); |
|
121 |
System.out.println(" - Connected to agent."); |
|
122 |
||
123 |
// Test 5b - functional "end-to-end" test. |
|
124 |
// Now with an agent that does redefine. |
|
125 |
||
126 |
System.out.println(" - Re-attaching to application ..."); |
|
127 |
vm = VirtualMachine.attach(pid); |
|
128 |
||
129 |
System.out.println(" - Test: End-to-end connection with RedefineAgent"); |
|
130 |
||
131 |
ServerSocket ss2 = new ServerSocket(0); |
|
132 |
int port2 = ss2.getLocalPort(); |
|
133 |
||
134 |
System.out.println(" - Loading RedefineAgent.jar into target VM ..."); |
|
135 |
vm.loadAgent(redefineagent, Integer.toString(port2)); |
|
136 |
||
137 |
System.out.println(" - Waiting for RedefineAgent to connect back to tool ..."); |
|
138 |
Socket s2 = ss2.accept(); |
|
139 |
System.out.println(" - Connected to RedefineAgent."); |
|
140 |
||
141 |
// Test 6 - list method should list the target VM |
|
142 |
System.out.println(" - Test: VirtualMachine.list"); |
|
143 |
List<VirtualMachineDescriptor> l = VirtualMachine.list(); |
|
144 |
if (!l.isEmpty()) { |
|
145 |
boolean found = false; |
|
146 |
for (VirtualMachineDescriptor vmd: l) { |
|
147 |
if (vmd.id().equals(pid)) { |
|
148 |
found = true; |
|
149 |
break; |
|
150 |
} |
|
151 |
} |
|
152 |
if (found) { |
|
153 |
System.out.println(" - " + pid + " found."); |
|
154 |
} else { |
|
155 |
throw new RuntimeException(pid + " not found in VM list"); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
// test 7 - basic hashCode/equals tests |
|
160 |
System.out.println(" - Test: hashCode/equals"); |
|
161 |
||
162 |
VirtualMachine vm1 = VirtualMachine.attach(pid); |
|
163 |
VirtualMachine vm2 = VirtualMachine.attach(pid); |
|
164 |
if (!vm1.equals(vm2)) { |
|
165 |
throw new RuntimeException("virtual machines are not equal"); |
|
166 |
} |
|
167 |
if (vm.hashCode() != vm.hashCode()) { |
|
168 |
throw new RuntimeException("virtual machine hashCodes not equal"); |
|
169 |
} |
|
170 |
System.out.println(" - hashCode/equals okay"); |
|
171 |
||
172 |
||
173 |
// --- |
|
174 |
System.out.println(" - Cleaning up..."); |
|
175 |
s.close(); |
|
176 |
ss.close(); |
|
177 |
s2.close(); |
|
178 |
ss2.close(); |
|
179 |
} |
|
180 |
} |