Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 708f1cc63bfd4392dcd6c4c0b6eabf209a51cbae (plain) (blame)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*******************************************************************************
 * 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.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Vector;

import junit.framework.Test;

import com.sun.jdi.ClassNotLoadedException;
import com.sun.jdi.ClassType;
import com.sun.jdi.Field;
import com.sun.jdi.IncompatibleThreadStateException;
import com.sun.jdi.IntegerValue;
import com.sun.jdi.InvalidTypeException;
import com.sun.jdi.InvocationException;
import com.sun.jdi.Method;
import com.sun.jdi.ObjectReference;
import com.sun.jdi.ReferenceType;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.Value;
import com.sun.jdi.event.ThreadStartEvent;

/**
 * Tests for JDI com.sun.jdi.ObjectReference
 * and JDWP Object command set.
 */
public class ObjectReferenceTest extends AbstractJDITest {

	private ObjectReference fObject;

	/**
	 * Creates a new test.
	 */
	public ObjectReferenceTest() {
		super();
	}
	/**
	 * Init the fields that are used by this test only.
	 */
	public void localSetUp() {
		// Make sure the object is in expected state (eg. it has not entered a monitor)
		waitUntilReady();

		// Get static field "fObject"
		fObject = getObjectReference();
	}
	/**
	 * Make sure the test leaves the VM in the same state it found it.
	 */
	public void localTearDown() {
		// The test has resumed and suspended the Test Thread. Make sure this
		// thread is suspended at the right location
		waitUntilReady();
	}
	/**
	 * Run all tests and output to standard output.
	 * @param args
	 */
	public static void main(java.lang.String[] args) {
		new ObjectReferenceTest().runSuite(args);
	}
	/**
	 * Gets the name of the test case.
	 * @see junit.framework.TestCase#getName()
	 */
	public String getName() {
		return "com.sun.jdi.ObjectReference";
	}
	/**
	 * Returns all tests 
	 */
	protected Test suite() {
		JDITestSuite suite = (JDITestSuite) super.suite();
		Vector testNames = getAllMatchingTests("testLast");
		Iterator iterator = testNames.iterator();
		while (iterator.hasNext()) {
			String name = (String) iterator.next();
			suite.addTest(new JDITestCase(this, name));
		}
		return suite;
	}
	/**
	 * Test JDI disableCollection(). enableCollection() and isCollected().
	 */
	public void testJDIDisableEnableCollection() {
		assertTrue("1", !fObject.isCollected());
		fObject.disableCollection();
		fObject.enableCollection();
	}
	/**
	 * Test JDI entryCount().
	 */
	public void testJDIEntryCount() {
		if (fVM.canGetMonitorInfo()) {
			// Ensure we're in a good state
			fVM.resume();
			waitUntilReady();

			try {
				assertEquals("1", 1, fObject.entryCount());
			} catch (IncompatibleThreadStateException e) {
				assertTrue("2", false);
			}
		}
	}
	/**
	 * Test JDI equals() and hashCode().
	 */
	public void testJDIEquality() {
		assertTrue("1", fObject.equals(fObject));
		ObjectReference other = getThread();
		assertTrue("2", !fObject.equals(other));
		assertTrue("3", !fObject.equals(new Object()));
		assertTrue("4", !fObject.equals(null));
		assertTrue("5", fObject.hashCode() != other.hashCode());
	}
	/**
	 * Test JDI getValue(Field), getValues(List) and setValue(Field,Value)
	 * and JDWP 'Object - Get Fields Values' and 'Object - Set Fields Values'.
	 */
	public void testJDIGetSetValues() {
		// setup
		ReferenceType type = fObject.referenceType();
		List fields = type.fields();
		ListIterator iterator = fields.listIterator();
		List instanceFields = new LinkedList();
		while (iterator.hasNext()) {
			Field field = (Field) iterator.next();
			if (!field.isStatic())
				instanceFields.add(field);
		}
		Field field = (Field) instanceFields.get(4);
		assertEquals("1", "fChar", field.name());

		// getValues(List)
		Map values = fObject.getValues(instanceFields);
		assertTrue("2", values.size() == 7);
		Value value = (Value) values.get(field);
		assertEquals("3", value, fVM.mirrorOf('a'));

		// setValue(Field,Value)
		Value newValue = fVM.mirrorOf('b');
		try {
			fObject.setValue(field, newValue);
		} catch (ClassNotLoadedException e) {
			assertTrue("4.1", false);
		} catch (InvalidTypeException e) {
			assertTrue("4.2", false);
		}

		// getValue(Field)
		assertEquals("5", fObject.getValue(field), newValue);

		// test set and get null value.
		field = (Field) instanceFields.get(5);

		assertEquals("6", "fString2", field.name());
		try {
			fObject.setValue(field, null);
		} catch (ClassNotLoadedException e) {
			assertTrue("7.1", false);
		} catch (InvalidTypeException e) {
			assertTrue("7.2", false);
		}

		// getValue(Field)
		assertEquals("8", fObject.getValue(field), null);

		// test get final value.
		field = (Field) instanceFields.get(6);
		assertEquals("9", "fString3", field.name());

		// The value is null and should be because it's final
		//assertEquals("10", fVM.mirrorOf("HEY"), fObject.getValue(field));

	}
	/**
	 * Test JDI invokeMethod.
	 */
	public void testJDIInvokeMethod() {
		// 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();
		waitUntilReady();

		ThreadStartEvent event =
			(ThreadStartEvent) triggerAndWait(fVM
				.eventRequestManager()
				.createThreadStartRequest(),
				"ThreadStartEvent",
				false);
		ThreadReference thread = event.thread();
		ClassType ct = (ClassType) fObject.referenceType();
		Method inv =
			ct.concreteMethodByName("invoke3", "(Ljava/lang/String;Ljava/lang/Object;)I");
		List args = new ArrayList();
		args.add(fVM.mirrorOf("888"));
		args.add(null);
		Exception oops = null;
		Value val = null;
		try {
			val = fObject.invokeMethod(thread, inv, args, 0);
		} catch (ClassNotLoadedException exc) {
			oops = exc;
		} catch (IncompatibleThreadStateException exc) {
			oops = exc;
		} catch (InvalidTypeException exc) {
			oops = exc;
		} catch (InvocationException exc) {
			oops = exc;
		}
		assertTrue("1", oops == null);
		assertEquals("2", val == null ? 0 : ((IntegerValue) val).value(), 888);
	}
	/**
	 * Test JDI invokeMethod - failure.
	 */
	public void testJDIInvokeMethodFail() {
		// 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();
		waitUntilReady();

		ThreadStartEvent event =
			(ThreadStartEvent) triggerAndWait(fVM
				.eventRequestManager()
				.createThreadStartRequest(),
				"ThreadStartEvent",
				false);
		ThreadReference thread = event.thread();
		ClassType ct = (ClassType) fObject.referenceType();
		Method inv = ct.concreteMethodByName("invoke4", "()J");
		Exception good = null, oops = null;
		try {
			fObject.invokeMethod(thread, inv, new ArrayList(), 0);
		} catch (ClassNotLoadedException exc) {
			oops = exc;
		} catch (IncompatibleThreadStateException exc) {
			oops = exc;
		} catch (InvalidTypeException exc) {
			oops = exc;
		} catch (InvocationException exc) {
			good = exc;
		}
		assertTrue("1", oops == null);
		assertTrue("2", good != null);
	}
	/**
	 * Test JDI owningThread().
	 */
	public void testJDIOwningThread() {
		if (fVM.canGetMonitorInfo()) {
			// Ensure we're in a good state
			fVM.resume();
			waitUntilReady();

			try {
				assertEquals("1", getThread(), fObject.owningThread());
			} catch (IncompatibleThreadStateException e) {
				assertTrue("2", false);
			}
		}
	}
	/**
	 * Test JDI referenceType() and JDWP 'Type - Get type'.
	 */
	public void testJDIReferenceType() {
		ReferenceType type = fObject.referenceType();
		assertEquals("1", type.name(), "org.eclipse.debug.jdi.tests.program.MainClass");
	}
	/**
	 * Test JDI uniqueID().
	 */
	public void testJDIUniqueID() {
		fObject.uniqueID();
	}
	/**
	 * Test JDI waitingThreads().
	 */
	public void testJDIWaitingThreads() {
		if (fVM.canGetMonitorInfo()) {
			try {
				assertEquals("1", 0, fObject.waitingThreads().size());
			} catch (IncompatibleThreadStateException e) {
				assertTrue("2", false);
			}
		}
	}
}

Back to the top