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.Iterator;
013: import java.util.List;
014: import java.util.ListIterator;
015: import java.util.Vector;
016:
017: import junit.framework.Test;
018:
019: import com.sun.jdi.BooleanValue;
020: import com.sun.jdi.ByteValue;
021: import com.sun.jdi.CharValue;
022: import com.sun.jdi.DoubleValue;
023: import com.sun.jdi.FloatValue;
024: import com.sun.jdi.IntegerValue;
025: import com.sun.jdi.LongValue;
026: import com.sun.jdi.ReferenceType;
027: import com.sun.jdi.ShortValue;
028: import com.sun.jdi.StringReference;
029: import com.sun.jdi.ThreadGroupReference;
030: import com.sun.jdi.ThreadReference;
031:
032: /**
033: * Tests for JDI com.sun.jdi.VirtualMachine
034: * and JDWP VM command set.
035: *
036: * Example of arguments:
037: * -launcher SunVMLauncher -address c:\jdk1.2.2\ -classpath d:\target
038: */
039: public class VirtualMachineTest extends AbstractJDITest {
040:
041: /**
042: * Creates a new test .
043: */
044: public VirtualMachineTest() {
045: super ();
046: }
047:
048: /**
049: * Init the fields that are used by this test only.
050: */
051: public void localSetUp() {
052: }
053:
054: /**
055: * Run all tests and output to standard output.
056: * @param args
057: */
058: public static void main(java.lang.String[] args) {
059: new VirtualMachineTest().runSuite(args);
060: }
061:
062: /**
063: * Gets the name of the test case.
064: * @see junit.framework.TestCase#getName()
065: */
066: public String getName() {
067: return "com.sun.jdi.VirtualMachine";
068: }
069:
070: /**
071: * Don't start the program yet, so that the testNotStarted* tests can run before.
072: */
073: protected void setUp() {
074: launchTargetAndConnectToVM();
075: }
076:
077: /**
078: * Starts the target program.
079: * NB: This method is copied in this class only so that it can be invoked
080: * dynamically.
081: */
082: public void startProgram() {
083: super .startProgram();
084: }
085:
086: /**
087: * Returns all tests
088: */
089: protected Test suite() {
090: JDITestSuite suite = new JDITestSuite(this );
091:
092: // Tests that run before the program is started
093: Vector testNames = getAllMatchingTests("testNotStarted");
094: Iterator iterator = testNames.iterator();
095: while (iterator.hasNext()) {
096: String name = (String) iterator.next();
097: suite.addTest(new JDITestCase(this , name));
098: }
099:
100: // The method that starts the program
101: suite.addTest(new JDITestCase(this , "startProgram"));
102:
103: // Tests that run after the program has started
104: testNames = getAllMatchingTests("testStarted");
105: iterator = testNames.iterator();
106: while (iterator.hasNext()) {
107: String name = (String) iterator.next();
108: suite.addTest(new JDITestCase(this , name));
109: }
110:
111: // All other tests
112: testNames = getAllMatchingTests("testJDI");
113: iterator = testNames.iterator();
114: while (iterator.hasNext()) {
115: String name = (String) iterator.next();
116: suite.addTest(new JDITestCase(this , name));
117: }
118:
119: return suite;
120: }
121:
122: /**
123: * Test JDI canGetBytecodes().
124: */
125: public void testJDICanGetBytecodes() {
126: fVM.canGetBytecodes();
127: }
128:
129: /**
130: * Test JDI canGetCurrentContendedMonitor().
131: */
132: public void testJDICanGetCurrentContendedMonitor() {
133: fVM.canGetCurrentContendedMonitor();
134: }
135:
136: /**
137: * Test JDI canGetMonitorInfo().
138: */
139: public void testJDICanGetMonitorInfo() {
140: fVM.canGetMonitorInfo();
141: }
142:
143: /**
144: * Test JDI canGetOwnedMonitorInfo().
145: */
146: public void testJDICanGetOwnedMonitorInfo() {
147: fVM.canGetOwnedMonitorInfo();
148: }
149:
150: /**
151: * Test JDI canGetSyntheticAttribute().
152: */
153: public void testJDICanGetSyntheticAttribute() {
154: // This is optional functionality, thus this is not a failure
155: fVM.canGetSyntheticAttribute();
156: }
157:
158: /**
159: * Test JDI canWatchFieldAccess().
160: */
161: public void testJDICanWatchFieldAccess() {
162: // This is optional functionality, thus this is not a failure
163: fVM.canWatchFieldAccess();
164: }
165:
166: /**
167: * Test JDI canWatchFieldModification().
168: */
169: public void testJDICanWatchFieldModification() {
170: // This is optional functionality, thus this is not a failure
171: fVM.canWatchFieldModification();
172: }
173:
174: /**
175: * Test JDI eventQueue().
176: */
177: public void testJDIEventQueue() {
178: assertNotNull("1", fVM.eventQueue());
179: }
180:
181: /**
182: * Test JDI eventRequestManager().
183: */
184: public void testJDIEventRequestManager() {
185: assertNotNull("1", fVM.eventRequestManager());
186: }
187:
188: /**
189: * Test JDI mirrorOf(boolean).
190: */
191: public void testJDIMirrorOfBoolean() {
192: boolean value = true;
193: BooleanValue mirror = fVM.mirrorOf(value);
194: assertTrue("1", value == mirror.value());
195: }
196:
197: /**
198: * Test JDI mirrorOf(byte).
199: */
200: public void testJDIMirrorOfByte() {
201: byte value = 1;
202: ByteValue mirror = fVM.mirrorOf(value);
203: assertEquals("1", value, mirror.value());
204: }
205:
206: /**
207: * Test JDI mirrorOf(char).
208: */
209: public void testJDIMirrorOfChar() {
210: char value = 'a';
211: CharValue mirror = fVM.mirrorOf(value);
212: assertEquals("1", value, mirror.value());
213: }
214:
215: /**
216: * Test JDI mirrorOf(double).
217: */
218: public void testJDIMirrorOfDouble() {
219: double value = 12345.6789;
220: DoubleValue mirror = fVM.mirrorOf(value);
221: assertEquals("1", value, mirror.value(), 0);
222: }
223:
224: /**
225: * Test JDI mirrorOf(float).
226: */
227: public void testJDIMirrorOfFloat() {
228: float value = 12345.6789f;
229: FloatValue mirror = fVM.mirrorOf(value);
230: assertEquals("1", value, mirror.value(), 0);
231: }
232:
233: /**
234: * Test JDI mirrorOf(int).
235: */
236: public void testJDIMirrorOfInt() {
237: int value = 12345;
238: IntegerValue mirror = fVM.mirrorOf(value);
239: assertEquals("1", value, mirror.value());
240: }
241:
242: /**
243: * Test JDI mirrorOf(long).
244: */
245: public void testJDIMirrorOfLong() {
246: long value = 1234567890l;
247: LongValue mirror = fVM.mirrorOf(value);
248: assertEquals("1", value, mirror.value());
249: }
250:
251: /**
252: * Test JDI mirrorOf(short).
253: */
254: public void testJDIMirrorOfShort() {
255: short value = (short) 12345;
256: ShortValue mirror = fVM.mirrorOf(value);
257: assertEquals("1", value, mirror.value());
258: }
259:
260: /**
261: * Test JDI mirrorOf(String) and JDWP 'VM - Create String'.
262: */
263: public void testJDIMirrorOfString() {
264: String testString = "Test";
265: StringReference newString = null;
266: newString = fVM.mirrorOf(testString);
267: assertEquals("1", newString.value(), testString);
268: }
269:
270: /**
271: * Test JDI setDebugTraceMode(int).
272: */
273: public void testJDISetDebugTraceMode() {
274: fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_ALL);
275: fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_SENDS);
276: fVM
277: .setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_RECEIVES);
278: fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_NONE);
279:
280: // restore original value
281: fVM.setDebugTraceMode(fVMTraceFlags);
282: }
283:
284: /**
285: * Test JDI getVersion().
286: */
287: public void testJDIVersion() {
288: String version = fVM.version();
289: assertTrue("1", version != null);
290: }
291:
292: /**
293: * Test JDI allClasses() and JDWP 'VM - Get all classes'
294: * while the test program has not been started.
295: */
296: public void testNotStartedAllClasses() {
297: List classes = fVM.allClasses();
298: Iterator iterator = classes.listIterator();
299: int i = 0;
300: while (iterator.hasNext())
301: assertTrue(Integer.toString(i++),
302: iterator.next() instanceof ReferenceType);
303: }
304:
305: /**
306: * Test JDI allThreads() and JDWP 'VM - Get all threads'
307: * while the test program has not been started.
308: */
309: public void testNotStartedAllThreads() {
310: List threads = fVM.allThreads();
311: Iterator iterator = threads.listIterator();
312: int i = 0;
313: while (iterator.hasNext())
314: assertTrue(Integer.toString(i++),
315: iterator.next() instanceof ThreadReference);
316: }
317:
318: /**
319: * Test JDI classesByName() while the test program has not been started.
320: */
321: public void testNotStartedClassesByName() {
322: List classes = fVM.classesByName("java.lang.Object");
323: assertEquals("1", 1, classes.size());
324: }
325:
326: /**
327: * Test JDI allClasses() and JDWP 'VM- Get all classes'
328: * once the test program has been started.
329: */
330: public void testStartedAllClasses() {
331:
332: // The test program has started, the number of classes is != 0
333: List classes = fVM.allClasses();
334: assertTrue("1", classes.size() != 0);
335:
336: // Collect names of received classes
337: String[] names = new String[classes.size()];
338: ListIterator iterator = classes.listIterator();
339: int i = 0;
340: while (iterator.hasNext()) {
341: ReferenceType type = (ReferenceType) iterator.next();
342: names[i++] = type.name();
343: }
344:
345: // Check that they are the expected names
346: String[] expected = new String[] { "java.lang.Object",
347: "java.util.Date",
348: "org.eclipse.debug.jdi.tests.program.Printable",
349: "org.eclipse.debug.jdi.tests.program.MainClass" };
350: for (int j = 0; j < expected.length; j++) {
351: boolean isIncluded = false;
352: iteration: for (int k = 0; k < names.length; k++) {
353: if (names[k].equals(expected[j])) {
354: isIncluded = true;
355: break iteration;
356: }
357: }
358: assertTrue("2." + j, isIncluded);
359: }
360: }
361:
362: /**
363: * Test JDI allThreads() and JDWP 'VM - Get all threads'
364: * once the test program has been started.
365: */
366: public void testStartedAllThreads() {
367:
368: // The test program has started, the number of threads is != 0
369: List threads = fVM.allThreads();
370: assertTrue("1", threads.size() != 0);
371:
372: // Collect names of received threads
373: String[] names = new String[threads.size()];
374: ListIterator iterator = threads.listIterator();
375: int i = 0;
376: while (iterator.hasNext()) {
377: ThreadReference thread = (ThreadReference) iterator.next();
378: names[i++] = thread.name();
379: }
380:
381: // Check that they contain at least the expected names
382: String[] expected = new String[] { "Test Thread" };
383: boolean isIncluded = false;
384: iteration: for (int j = 0; j < expected.length; j++) {
385: for (int k = 0; k < names.length; k++) {
386: if (expected[j].equals(names[k])) {
387: isIncluded = true;
388: break iteration;
389: }
390: }
391: }
392: assertTrue("2", isIncluded);
393: }
394:
395: /**
396: * Test JDI classesByName() once the test program has been started.
397: */
398: public void testStartedClassesByName() {
399:
400: // The test program has started, the number of java.lang.Object is 1
401: List classes = fVM.classesByName("java.lang.Object");
402: assertEquals("1", classes.size(), 1);
403:
404: // Collect names of received classes
405: String[] names = new String[classes.size()];
406: ListIterator iterator = classes.listIterator();
407: int i = 0;
408: while (iterator.hasNext()) {
409: ReferenceType type = (ReferenceType) iterator.next();
410: names[i++] = type.name();
411: }
412:
413: // Check that they are all "java.lang.Object"
414: for (int j = 0; j < names.length; j++) {
415: assertEquals("2." + j, "java.lang.Object", names[j]);
416: }
417: }
418:
419: /**
420: * Test JDI suspend() and resume() once the test program has been started.
421: */
422: public void testStartedSuspendResume() {
423: // Suspend
424: fVM.suspend();
425: ListIterator threads = fVM.allThreads().listIterator();
426: while (threads.hasNext()) {
427: ThreadReference thread = (ThreadReference) threads.next();
428: assertTrue("1." + thread.name(), thread.isSuspended());
429: }
430:
431: // Resume
432: fVM.resume();
433: // Cannot assertTrue that all threads are not suspended because they might have been suspended
434: // by the program itself
435:
436: // Suspend VM and suspend one thread
437: fVM.suspend();
438: threads = fVM.allThreads().listIterator();
439: ThreadReference suspended = getThread();
440: suspended.suspend();
441: while (threads.hasNext()) {
442: ThreadReference thread = (ThreadReference) threads.next();
443: assertTrue("2." + thread.name(), thread.isSuspended());
444: }
445:
446: // Resume VM and ensure that the one thread that was suspended is still suspended
447: fVM.resume();
448: assertTrue("3", suspended.isSuspended());
449: }
450:
451: /**
452: * Test JDI topLevelThreadGroups().
453: */
454: public void testStartedTopLevelThreadGroups() {
455: List topLevelThreadGroups = fVM.topLevelThreadGroups();
456: assertEquals("1", 1, topLevelThreadGroups.size());
457: assertTrue(
458: "2",
459: topLevelThreadGroups.get(0) instanceof ThreadGroupReference);
460: }
461: }
|