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.ArrayList;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import java.util.List;
016: import java.util.ListIterator;
017: import java.util.Map;
018: import java.util.Vector;
019:
020: import junit.framework.Test;
021:
022: import com.sun.jdi.ClassNotLoadedException;
023: import com.sun.jdi.ClassType;
024: import com.sun.jdi.Field;
025: import com.sun.jdi.IncompatibleThreadStateException;
026: import com.sun.jdi.IntegerValue;
027: import com.sun.jdi.InvalidTypeException;
028: import com.sun.jdi.InvocationException;
029: import com.sun.jdi.Method;
030: import com.sun.jdi.ObjectReference;
031: import com.sun.jdi.ReferenceType;
032: import com.sun.jdi.ThreadReference;
033: import com.sun.jdi.Value;
034: import com.sun.jdi.event.ThreadStartEvent;
035:
036: /**
037: * Tests for JDI com.sun.jdi.ObjectReference
038: * and JDWP Object command set.
039: */
040: public class ObjectReferenceTest extends AbstractJDITest {
041:
042: private ObjectReference fObject;
043:
044: /**
045: * Creates a new test.
046: */
047: public ObjectReferenceTest() {
048: super ();
049: }
050:
051: /**
052: * Init the fields that are used by this test only.
053: */
054: public void localSetUp() {
055: // Make sure the object is in expected state (eg. it has not entered a monitor)
056: waitUntilReady();
057:
058: // Get static field "fObject"
059: fObject = getObjectReference();
060: }
061:
062: /**
063: * Make sure the test leaves the VM in the same state it found it.
064: */
065: public void localTearDown() {
066: // The test has resumed and suspended the Test Thread. Make sure this
067: // thread is suspended at the right location
068: waitUntilReady();
069: }
070:
071: /**
072: * Run all tests and output to standard output.
073: * @param args
074: */
075: public static void main(java.lang.String[] args) {
076: new ObjectReferenceTest().runSuite(args);
077: }
078:
079: /**
080: * Gets the name of the test case.
081: * @see junit.framework.TestCase#getName()
082: */
083: public String getName() {
084: return "com.sun.jdi.ObjectReference";
085: }
086:
087: /**
088: * Returns all tests
089: */
090: protected Test suite() {
091: JDITestSuite suite = (JDITestSuite) super .suite();
092: Vector testNames = getAllMatchingTests("testLast");
093: Iterator iterator = testNames.iterator();
094: while (iterator.hasNext()) {
095: String name = (String) iterator.next();
096: suite.addTest(new JDITestCase(this , name));
097: }
098: return suite;
099: }
100:
101: /**
102: * Test JDI disableCollection(). enableCollection() and isCollected().
103: */
104: public void testJDIDisableEnableCollection() {
105: assertTrue("1", !fObject.isCollected());
106: fObject.disableCollection();
107: fObject.enableCollection();
108: }
109:
110: /**
111: * Test JDI entryCount().
112: */
113: public void testJDIEntryCount() {
114: if (fVM.canGetMonitorInfo()) {
115: // Ensure we're in a good state
116: fVM.resume();
117: waitUntilReady();
118:
119: try {
120: assertEquals("1", 1, fObject.entryCount());
121: } catch (IncompatibleThreadStateException e) {
122: assertTrue("2", false);
123: }
124: }
125: }
126:
127: /**
128: * Test JDI equals() and hashCode().
129: */
130: public void testJDIEquality() {
131: assertTrue("1", fObject.equals(fObject));
132: ObjectReference other = getThread();
133: assertTrue("2", !fObject.equals(other));
134: assertTrue("3", !fObject.equals(new Object()));
135: assertTrue("4", !fObject.equals(null));
136: assertTrue("5", fObject.hashCode() != other.hashCode());
137: }
138:
139: /**
140: * Test JDI getValue(Field), getValues(List) and setValue(Field,Value)
141: * and JDWP 'Object - Get Fields Values' and 'Object - Set Fields Values'.
142: */
143: public void testJDIGetSetValues() {
144: // setup
145: ReferenceType type = fObject.referenceType();
146: List fields = type.fields();
147: ListIterator iterator = fields.listIterator();
148: List instanceFields = new LinkedList();
149: while (iterator.hasNext()) {
150: Field field = (Field) iterator.next();
151: if (!field.isStatic())
152: instanceFields.add(field);
153: }
154: Field field = (Field) instanceFields.get(4);
155: assertEquals("1", "fChar", field.name());
156:
157: // getValues(List)
158: Map values = fObject.getValues(instanceFields);
159: assertTrue("2", values.size() == 7);
160: Value value = (Value) values.get(field);
161: assertEquals("3", value, fVM.mirrorOf('a'));
162:
163: // setValue(Field,Value)
164: Value newValue = fVM.mirrorOf('b');
165: try {
166: fObject.setValue(field, newValue);
167: } catch (ClassNotLoadedException e) {
168: assertTrue("4.1", false);
169: } catch (InvalidTypeException e) {
170: assertTrue("4.2", false);
171: }
172:
173: // getValue(Field)
174: assertEquals("5", fObject.getValue(field), newValue);
175:
176: // test set and get null value.
177: field = (Field) instanceFields.get(5);
178:
179: assertEquals("6", "fString2", field.name());
180: try {
181: fObject.setValue(field, null);
182: } catch (ClassNotLoadedException e) {
183: assertTrue("7.1", false);
184: } catch (InvalidTypeException e) {
185: assertTrue("7.2", false);
186: }
187:
188: // getValue(Field)
189: assertEquals("8", fObject.getValue(field), null);
190:
191: // test get final value.
192: field = (Field) instanceFields.get(6);
193: assertEquals("9", "fString3", field.name());
194:
195: // The value is null and should be because it's final
196: //assertEquals("10", fVM.mirrorOf("HEY"), fObject.getValue(field));
197:
198: }
199:
200: /**
201: * Test JDI invokeMethod.
202: */
203: public void testJDIInvokeMethod() {
204: // Make sure the entire VM is not suspended before we start a new thread
205: // (otherwise this new thread will start suspended and we will never get the
206: // ThreadStart event)
207: fVM.resume();
208: waitUntilReady();
209:
210: ThreadStartEvent event = (ThreadStartEvent) triggerAndWait(fVM
211: .eventRequestManager().createThreadStartRequest(),
212: "ThreadStartEvent", false);
213: ThreadReference thread = event.thread();
214: ClassType ct = (ClassType) fObject.referenceType();
215: Method inv = ct.concreteMethodByName("invoke3",
216: "(Ljava/lang/String;Ljava/lang/Object;)I");
217: List args = new ArrayList();
218: args.add(fVM.mirrorOf("888"));
219: args.add(null);
220: Exception oops = null;
221: Value val = null;
222: try {
223: val = fObject.invokeMethod(thread, inv, args, 0);
224: } catch (ClassNotLoadedException exc) {
225: oops = exc;
226: } catch (IncompatibleThreadStateException exc) {
227: oops = exc;
228: } catch (InvalidTypeException exc) {
229: oops = exc;
230: } catch (InvocationException exc) {
231: oops = exc;
232: }
233: assertTrue("1", oops == null);
234: assertEquals("2", val == null ? 0 : ((IntegerValue) val)
235: .value(), 888);
236: }
237:
238: /**
239: * Test JDI invokeMethod - failure.
240: */
241: public void testJDIInvokeMethodFail() {
242: // Make sure the entire VM is not suspended before we start a new thread
243: // (otherwise this new thread will start suspended and we will never get the
244: // ThreadStart event)
245: fVM.resume();
246: waitUntilReady();
247:
248: ThreadStartEvent event = (ThreadStartEvent) triggerAndWait(fVM
249: .eventRequestManager().createThreadStartRequest(),
250: "ThreadStartEvent", false);
251: ThreadReference thread = event.thread();
252: ClassType ct = (ClassType) fObject.referenceType();
253: Method inv = ct.concreteMethodByName("invoke4", "()J");
254: Exception good = null, oops = null;
255: try {
256: fObject.invokeMethod(thread, inv, new ArrayList(), 0);
257: } catch (ClassNotLoadedException exc) {
258: oops = exc;
259: } catch (IncompatibleThreadStateException exc) {
260: oops = exc;
261: } catch (InvalidTypeException exc) {
262: oops = exc;
263: } catch (InvocationException exc) {
264: good = exc;
265: }
266: assertTrue("1", oops == null);
267: assertTrue("2", good != null);
268: }
269:
270: /**
271: * Test JDI owningThread().
272: */
273: public void testJDIOwningThread() {
274: if (fVM.canGetMonitorInfo()) {
275: // Ensure we're in a good state
276: fVM.resume();
277: waitUntilReady();
278:
279: try {
280: assertEquals("1", getThread(), fObject.owningThread());
281: } catch (IncompatibleThreadStateException e) {
282: assertTrue("2", false);
283: }
284: }
285: }
286:
287: /**
288: * Test JDI referenceType() and JDWP 'Type - Get type'.
289: */
290: public void testJDIReferenceType() {
291: ReferenceType type = fObject.referenceType();
292: assertEquals("1", type.name(),
293: "org.eclipse.debug.jdi.tests.program.MainClass");
294: }
295:
296: /**
297: * Test JDI uniqueID().
298: */
299: public void testJDIUniqueID() {
300: fObject.uniqueID();
301: }
302:
303: /**
304: * Test JDI waitingThreads().
305: */
306: public void testJDIWaitingThreads() {
307: if (fVM.canGetMonitorInfo()) {
308: try {
309: assertEquals("1", 0, fObject.waitingThreads().size());
310: } catch (IncompatibleThreadStateException e) {
311: assertTrue("2", false);
312: }
313: }
314: }
315: }
|