1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.debug.jdi.tests;
import java.util.List;
import com.sun.jdi.ClassNotLoadedException;
import com.sun.jdi.ClassType;
import com.sun.jdi.IncompatibleThreadStateException;
import com.sun.jdi.InvalidTypeException;
import com.sun.jdi.InvocationException;
import com.sun.jdi.Method;
import com.sun.jdi.ObjectReference;
import com.sun.jdi.StackFrame;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.event.ThreadStartEvent;
/**
* Tests for JDI com.sun.jdi.ThreadReference
* and JDWP Thread command set.
*/
public class ThreadReferenceTest extends AbstractJDITest {
private ThreadReference fThread;
/**
* Creates a new test .
*/
public ThreadReferenceTest() {
super();
}
/**
* Init the fields that are used by this test only.
*/
@Override
public void localSetUp() {
// Get thread
fThread = getThread();
}
/**
* Run all tests and output to standard output.
* @param args
*/
public static void main(java.lang.String[] args) {
new ThreadReferenceTest().runSuite(args);
}
/**
* Gets the name of the test case.
* @see junit.framework.TestCase#getName()
*/
@Override
public String getName() {
return "com.sun.jdi.ThreadReference";
}
/**
* Test JDI currentContendedMonitor().
*/
public void testJDICurrentContendedMonitor() {
if (fVM.canGetCurrentContendedMonitor()) {
try {
assertTrue("1", fThread.currentContendedMonitor() == null);
} catch (IncompatibleThreadStateException e) {
assertTrue("2", false);
}
}
}
/**
* Test JDI frame(int).
*/
public void testJDIFrame() {
try {
StackFrame frame = fThread.frame(0);
assertTrue("1", fThread.frames().contains(frame));
} catch (IncompatibleThreadStateException e) {
assertTrue("2", false);
}
}
/**
* Test JDI frameCount.
*/
public void testJDIFrameCount() {
try {
int count = fThread.frameCount();
assertTrue("1", count <= 4);
} catch (IncompatibleThreadStateException e) {
assertTrue("2", false);
}
}
/**
* Test JDI frames() and JDWP 'Thread - Get frames'.
*/
public void testJDIFrames() {
List frames = null;
try {
frames = fThread.frames();
} catch (IncompatibleThreadStateException e) {
assertTrue("1", false);
}
assertTrue("2", frames.size() > 0);
}
/**
* Test JDI interrupt()().
*/
public void testJDIInterrupt() {
assertEquals("1", 1, fThread.suspendCount());
fThread.interrupt();
assertEquals("2", 1, fThread.suspendCount());
}
/**
* Test JDI isAtBreakpoint().
*/
public void testJDIIsAtBreakpoint() {
assertTrue("1", !fThread.isAtBreakpoint());
}
/**
* Test JDI isSuspended().
*/
public void testJDIIsSuspended() {
assertTrue("1", fThread.isSuspended());
}
/**
* Test JDI name() and JDWP 'Thread - Get name'.
*/
public void testJDIName() {
assertEquals("1", "Test Thread", fThread.name());
}
/**
* Test JDI ownedMonitors().
*/
public void testJDIOwnedMonitors() {
if (fVM.canGetOwnedMonitorInfo()) {
waitUntilReady();
try {
assertEquals("1", 1, fThread.ownedMonitors().size());
} catch (IncompatibleThreadStateException e) {
assertTrue("2", false);
}
}
}
/**
* Test JDI status() and JDWP 'Thread - Get status'.
*/
public void testJDIStatus() {
int status = fThread.status();
assertTrue(
"1",
((status == ThreadReference.THREAD_STATUS_RUNNING)
|| (status == ThreadReference.THREAD_STATUS_SLEEPING)
|| (status == ThreadReference.THREAD_STATUS_WAIT)));
}
/**
* Test JDI stop(ObjectReference).
*/
public void testJDIStop() {
// Make sure the entire VM is not suspended before we start a new thread
// (otherwise this new thread will start suspended and we will never get the
// ThreadStart event)
fVM.resume();
// Trigger a thread start event to get a new thread
ThreadStartEvent event =
(ThreadStartEvent) triggerAndWait(fVM
.eventRequestManager()
.createThreadStartRequest(),
"ThreadStartEvent",
false);
ThreadReference thread = event.thread();
// Create a java.lang.Throwable instance in
java.util.List classes = fVM.classesByName("java.lang.Throwable");
assertTrue("1", classes.size() != 0);
ClassType threadDeathClass = (ClassType) classes.get(0);
Method constructor =
threadDeathClass.concreteMethodByName("<init>", "()V");
ObjectReference threadDeath = null;
try {
threadDeath =
threadDeathClass.newInstance(
thread,
constructor,
new java.util.LinkedList(),
ClassType.INVOKE_SINGLE_THREADED);
threadDeath.disableCollection();
// This object is going to be used for the lifetime of the VM.
} catch (ClassNotLoadedException e) {
assertTrue("2", false);
} catch (InvalidTypeException e) {
assertTrue("3", false);
} catch (InvocationException e) {
assertTrue("4", false);
} catch (IncompatibleThreadStateException e) {
assertTrue("5", false);
}
// Stop the thread
try {
thread.stop(threadDeath);
} catch (InvalidTypeException e) {
assertTrue("6", false);
}
waitUntilReady();
}
/**
* Test JDI suspend() and resume()
* and JDWP 'Thread - Suspend' and 'Thread - Resume'.
*/
public void testJDISuspendResume() {
assertEquals("1", 1, fThread.suspendCount());
fThread.resume();
assertTrue("2", !fThread.isSuspended());
fThread.suspend();
assertTrue("3", fThread.isSuspended());
waitUntilReady();
}
/**
* Test JDI threadGroup() and JDWP 'Thread - Get threadGroup'.
*/
public void testJDIThreadGroup() {
assertNotNull("1", fThread.threadGroup());
}
}
|