001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.debug.jdi.tests;
011:
012: import java.util.List;
013:
014: import com.sun.jdi.ClassNotLoadedException;
015: import com.sun.jdi.ClassType;
016: import com.sun.jdi.IncompatibleThreadStateException;
017: import com.sun.jdi.InvalidTypeException;
018: import com.sun.jdi.InvocationException;
019: import com.sun.jdi.Method;
020: import com.sun.jdi.ObjectReference;
021: import com.sun.jdi.StackFrame;
022: import com.sun.jdi.ThreadReference;
023: import com.sun.jdi.event.ThreadStartEvent;
024:
025: /**
026: * Tests for JDI com.sun.jdi.ThreadReference
027: * and JDWP Thread command set.
028: */
029: public class ThreadReferenceTest extends AbstractJDITest {
030:
031: private ThreadReference fThread;
032:
033: /**
034: * Creates a new test .
035: */
036: public ThreadReferenceTest() {
037: super ();
038: }
039:
040: /**
041: * Init the fields that are used by this test only.
042: */
043: public void localSetUp() {
044: // Get thread
045: fThread = getThread();
046: }
047:
048: /**
049: * Run all tests and output to standard output.
050: * @param args
051: */
052: public static void main(java.lang.String[] args) {
053: new ThreadReferenceTest().runSuite(args);
054: }
055:
056: /**
057: * Gets the name of the test case.
058: * @see junit.framework.TestCase#getName()
059: */
060: public String getName() {
061: return "com.sun.jdi.ThreadReference";
062: }
063:
064: /**
065: * Test JDI currentContendedMonitor().
066: */
067: public void testJDICurrentContendedMonitor() {
068: if (fVM.canGetCurrentContendedMonitor()) {
069: try {
070: assertTrue("1",
071: fThread.currentContendedMonitor() == null);
072: } catch (IncompatibleThreadStateException e) {
073: assertTrue("2", false);
074: }
075: }
076: }
077:
078: /**
079: * Test JDI frame(int).
080: */
081: public void testJDIFrame() {
082: try {
083: StackFrame frame = fThread.frame(0);
084: assertTrue("1", fThread.frames().contains(frame));
085: } catch (IncompatibleThreadStateException e) {
086: assertTrue("2", false);
087: }
088: }
089:
090: /**
091: * Test JDI frameCount.
092: */
093: public void testJDIFrameCount() {
094: try {
095: int count = fThread.frameCount();
096: assertTrue("1", count <= 4);
097: } catch (IncompatibleThreadStateException e) {
098: assertTrue("2", false);
099: }
100: }
101:
102: /**
103: * Test JDI frames() and JDWP 'Thread - Get frames'.
104: */
105: public void testJDIFrames() {
106: List frames = null;
107: try {
108: frames = fThread.frames();
109: } catch (IncompatibleThreadStateException e) {
110: assertTrue("1", false);
111: }
112: assertTrue("2", frames.size() > 0);
113: }
114:
115: /**
116: * Test JDI interrupt()().
117: */
118: public void testJDIInterrupt() {
119: assertEquals("1", 1, fThread.suspendCount());
120: fThread.interrupt();
121: assertEquals("2", 1, fThread.suspendCount());
122: }
123:
124: /**
125: * Test JDI isAtBreakpoint().
126: */
127: public void testJDIIsAtBreakpoint() {
128: assertTrue("1", !fThread.isAtBreakpoint());
129: }
130:
131: /**
132: * Test JDI isSuspended().
133: */
134: public void testJDIIsSuspended() {
135: assertTrue("1", fThread.isSuspended());
136: }
137:
138: /**
139: * Test JDI name() and JDWP 'Thread - Get name'.
140: */
141: public void testJDIName() {
142: assertEquals("1", "Test Thread", fThread.name());
143: }
144:
145: /**
146: * Test JDI ownedMonitors().
147: */
148: public void testJDIOwnedMonitors() {
149: if (fVM.canGetOwnedMonitorInfo()) {
150: waitUntilReady();
151: try {
152: assertEquals("1", 1, fThread.ownedMonitors().size());
153: } catch (IncompatibleThreadStateException e) {
154: assertTrue("2", false);
155: }
156: }
157: }
158:
159: /**
160: * Test JDI status() and JDWP 'Thread - Get status'.
161: */
162: public void testJDIStatus() {
163: int status = fThread.status();
164: assertTrue(
165: "1",
166: ((status == ThreadReference.THREAD_STATUS_RUNNING)
167: || (status == ThreadReference.THREAD_STATUS_SLEEPING) || (status == ThreadReference.THREAD_STATUS_WAIT)));
168: }
169:
170: /**
171: * Test JDI stop(ObjectReference).
172: */
173: public void testJDIStop() {
174: // Make sure the entire VM is not suspended before we start a new thread
175: // (otherwise this new thread will start suspended and we will never get the
176: // ThreadStart event)
177: fVM.resume();
178:
179: // Trigger a thread start event to get a new thread
180: ThreadStartEvent event = (ThreadStartEvent) triggerAndWait(fVM
181: .eventRequestManager().createThreadStartRequest(),
182: "ThreadStartEvent", false);
183: ThreadReference thread = event.thread();
184:
185: // Create a java.lang.Throwable instance in
186: java.util.List classes = fVM
187: .classesByName("java.lang.Throwable");
188: assertTrue("1", classes.size() != 0);
189: ClassType threadDeathClass = (ClassType) classes.get(0);
190: Method constructor = threadDeathClass.concreteMethodByName(
191: "<init>", "()V");
192: ObjectReference threadDeath = null;
193: try {
194: threadDeath = threadDeathClass.newInstance(thread,
195: constructor, new java.util.LinkedList(),
196: ClassType.INVOKE_SINGLE_THREADED);
197: threadDeath.disableCollection();
198: // This object is going to be used for the lifetime of the VM.
199: } catch (ClassNotLoadedException e) {
200: assertTrue("2", false);
201: } catch (InvalidTypeException e) {
202: assertTrue("3", false);
203: } catch (InvocationException e) {
204: assertTrue("4", false);
205: } catch (IncompatibleThreadStateException e) {
206: assertTrue("5", false);
207: }
208:
209: // Stop the thread
210: try {
211: thread.stop(threadDeath);
212: } catch (InvalidTypeException e) {
213: assertTrue("6", false);
214: }
215:
216: waitUntilReady();
217:
218: }
219:
220: /**
221: * Test JDI suspend() and resume()
222: * and JDWP 'Thread - Suspend' and 'Thread - Resume'.
223: */
224: public void testJDISuspendResume() {
225: assertEquals("1", 1, fThread.suspendCount());
226: fThread.resume();
227: assertTrue("2", !fThread.isSuspended());
228: fThread.suspend();
229: assertTrue("3", fThread.isSuspended());
230:
231: waitUntilReady();
232: }
233:
234: /**
235: * Test JDI threadGroup() and JDWP 'Thread - Get threadGroup'.
236: */
237: public void testJDIThreadGroup() {
238: assertNotNull("1", fThread.threadGroup());
239: }
240: }
|