1 /******************************************************************************* 2 * Copyright (c) 2000, 2012 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.debug.jdi.tests; 15 16 import java.util.Iterator; 17 import java.util.List; 18 import java.util.ListIterator; 19 import java.util.Vector; 20 21 import junit.framework.Test; 22 23 import com.sun.jdi.BooleanValue; 24 import com.sun.jdi.ByteValue; 25 import com.sun.jdi.CharValue; 26 import com.sun.jdi.DoubleValue; 27 import com.sun.jdi.FloatValue; 28 import com.sun.jdi.IntegerValue; 29 import com.sun.jdi.LongValue; 30 import com.sun.jdi.ReferenceType; 31 import com.sun.jdi.ShortValue; 32 import com.sun.jdi.StringReference; 33 import com.sun.jdi.ThreadGroupReference; 34 import com.sun.jdi.ThreadReference; 35 36 /** 37 * Tests for JDI com.sun.jdi.VirtualMachine 38 * and JDWP VM command set. 39 * 40 * Example of arguments: 41 * -launcher SunVMLauncher -address c:\jdk1.2.2\ -classpath d:\target 42 */ 43 public class VirtualMachineTest extends AbstractJDITest { 44 45 /** 46 * Creates a new test . 47 */ VirtualMachineTest()48 public VirtualMachineTest() { 49 super(); 50 } 51 /** 52 * Init the fields that are used by this test only. 53 */ 54 @Override localSetUp()55 public void localSetUp() { 56 } 57 /** 58 * Run all tests and output to standard output. 59 * @param args 60 */ main(java.lang.String[] args)61 public static void main(java.lang.String[] args) { 62 new VirtualMachineTest().runSuite(args); 63 } 64 /** 65 * Gets the name of the test case. 66 * @see junit.framework.TestCase#getName() 67 */ 68 @Override getName()69 public String getName() { 70 return "com.sun.jdi.VirtualMachine"; 71 } 72 /** 73 * Don't start the program yet, so that the testNotStarted* tests can run before. 74 */ 75 @Override setUp()76 protected void setUp() { 77 launchTargetAndConnectToVM(); 78 } 79 /** 80 * Starts the target program. 81 * NB: This method is copied in this class only so that it can be invoked 82 * dynamically. 83 */ 84 @Override startProgram()85 public void startProgram() { 86 super.startProgram(); 87 } 88 /** 89 * Returns all tests 90 */ 91 @Override suite()92 protected Test suite() { 93 JDITestSuite suite = new JDITestSuite(this); 94 95 // Tests that run before the program is started 96 Vector<?> testNames = getAllMatchingTests("testNotStarted"); 97 Iterator<?> iterator = testNames.iterator(); 98 while (iterator.hasNext()) { 99 String name = (String) iterator.next(); 100 suite.addTest(new JDITestCase(this, name)); 101 } 102 103 // The method that starts the program 104 suite.addTest(new JDITestCase(this, "startProgram")); 105 106 // Tests that run after the program has started 107 testNames = getAllMatchingTests("testStarted"); 108 iterator = testNames.iterator(); 109 while (iterator.hasNext()) { 110 String name = (String) iterator.next(); 111 suite.addTest(new JDITestCase(this, name)); 112 } 113 114 // All other tests 115 testNames = getAllMatchingTests("testJDI"); 116 iterator = testNames.iterator(); 117 while (iterator.hasNext()) { 118 String name = (String) iterator.next(); 119 suite.addTest(new JDITestCase(this, name)); 120 } 121 122 return suite; 123 } 124 /** 125 * Test JDI canGetBytecodes(). 126 */ testJDICanGetBytecodes()127 public void testJDICanGetBytecodes() { 128 fVM.canGetBytecodes(); 129 } 130 /** 131 * Test JDI canGetCurrentContendedMonitor(). 132 */ testJDICanGetCurrentContendedMonitor()133 public void testJDICanGetCurrentContendedMonitor() { 134 fVM.canGetCurrentContendedMonitor(); 135 } 136 /** 137 * Test JDI canGetMonitorInfo(). 138 */ testJDICanGetMonitorInfo()139 public void testJDICanGetMonitorInfo() { 140 fVM.canGetMonitorInfo(); 141 } 142 /** 143 * Test JDI canGetOwnedMonitorInfo(). 144 */ testJDICanGetOwnedMonitorInfo()145 public void testJDICanGetOwnedMonitorInfo() { 146 fVM.canGetOwnedMonitorInfo(); 147 } 148 /** 149 * Test JDI canGetSyntheticAttribute(). 150 */ testJDICanGetSyntheticAttribute()151 public void testJDICanGetSyntheticAttribute() { 152 // This is optional functionality, thus this is not a failure 153 fVM.canGetSyntheticAttribute(); 154 } 155 /** 156 * Test JDI canWatchFieldAccess(). 157 */ testJDICanWatchFieldAccess()158 public void testJDICanWatchFieldAccess() { 159 // This is optional functionality, thus this is not a failure 160 fVM.canWatchFieldAccess(); 161 } 162 /** 163 * Test JDI canWatchFieldModification(). 164 */ testJDICanWatchFieldModification()165 public void testJDICanWatchFieldModification() { 166 // This is optional functionality, thus this is not a failure 167 fVM.canWatchFieldModification(); 168 } 169 /** 170 * Test JDI eventQueue(). 171 */ testJDIEventQueue()172 public void testJDIEventQueue() { 173 assertNotNull("1", fVM.eventQueue()); 174 } 175 /** 176 * Test JDI eventRequestManager(). 177 */ testJDIEventRequestManager()178 public void testJDIEventRequestManager() { 179 assertNotNull("1", fVM.eventRequestManager()); 180 } 181 /** 182 * Test JDI mirrorOf(boolean). 183 */ testJDIMirrorOfBoolean()184 public void testJDIMirrorOfBoolean() { 185 boolean value = true; 186 BooleanValue mirror = fVM.mirrorOf(value); 187 assertTrue("1", value == mirror.value()); 188 } 189 /** 190 * Test JDI mirrorOf(byte). 191 */ testJDIMirrorOfByte()192 public void testJDIMirrorOfByte() { 193 byte value = 1; 194 ByteValue mirror = fVM.mirrorOf(value); 195 assertEquals("1", value, mirror.value()); 196 } 197 /** 198 * Test JDI mirrorOf(char). 199 */ testJDIMirrorOfChar()200 public void testJDIMirrorOfChar() { 201 char value = 'a'; 202 CharValue mirror = fVM.mirrorOf(value); 203 assertEquals("1", value, mirror.value()); 204 } 205 /** 206 * Test JDI mirrorOf(double). 207 */ testJDIMirrorOfDouble()208 public void testJDIMirrorOfDouble() { 209 double value = 12345.6789; 210 DoubleValue mirror = fVM.mirrorOf(value); 211 assertEquals("1", value, mirror.value(), 0); 212 } 213 /** 214 * Test JDI mirrorOf(float). 215 */ testJDIMirrorOfFloat()216 public void testJDIMirrorOfFloat() { 217 float value = 12345.6789f; 218 FloatValue mirror = fVM.mirrorOf(value); 219 assertEquals("1", value, mirror.value(), 0); 220 } 221 /** 222 * Test JDI mirrorOf(int). 223 */ testJDIMirrorOfInt()224 public void testJDIMirrorOfInt() { 225 int value = 12345; 226 IntegerValue mirror = fVM.mirrorOf(value); 227 assertEquals("1", value, mirror.value()); 228 } 229 /** 230 * Test JDI mirrorOf(long). 231 */ testJDIMirrorOfLong()232 public void testJDIMirrorOfLong() { 233 long value = 1234567890l; 234 LongValue mirror = fVM.mirrorOf(value); 235 assertEquals("1", value, mirror.value()); 236 } 237 /** 238 * Test JDI mirrorOf(short). 239 */ testJDIMirrorOfShort()240 public void testJDIMirrorOfShort() { 241 short value = (short) 12345; 242 ShortValue mirror = fVM.mirrorOf(value); 243 assertEquals("1", value, mirror.value()); 244 } 245 /** 246 * Test JDI mirrorOf(String) and JDWP 'VM - Create String'. 247 */ testJDIMirrorOfString()248 public void testJDIMirrorOfString() { 249 String testString = "Test"; 250 StringReference newString = null; 251 newString = fVM.mirrorOf(testString); 252 assertEquals("1", newString.value(), testString); 253 } 254 /** 255 * Test JDI setDebugTraceMode(int). 256 */ testJDISetDebugTraceMode()257 public void testJDISetDebugTraceMode() { 258 fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_ALL); 259 fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_SENDS); 260 fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_RECEIVES); 261 fVM.setDebugTraceMode(com.sun.jdi.VirtualMachine.TRACE_NONE); 262 263 // restore original value 264 fVM.setDebugTraceMode(fVMTraceFlags); 265 } 266 /** 267 * Test JDI getVersion(). 268 */ testJDIVersion()269 public void testJDIVersion() { 270 String version = fVM.version(); 271 assertTrue("1", version != null); 272 } 273 /** 274 * Test JDI allClasses() and JDWP 'VM - Get all classes' 275 * while the test program has not been started. 276 */ testNotStartedAllClasses()277 public void testNotStartedAllClasses() { 278 List<?> classes = fVM.allClasses(); 279 Iterator<?> iterator = classes.listIterator(); 280 int i = 0; 281 while (iterator.hasNext()) 282 assertTrue(Integer.toString(i++), iterator.next() instanceof ReferenceType); 283 } 284 /** 285 * Test JDI allThreads() and JDWP 'VM - Get all threads' 286 * while the test program has not been started. 287 */ testNotStartedAllThreads()288 public void testNotStartedAllThreads() { 289 List<?> threads = fVM.allThreads(); 290 Iterator<?> iterator = threads.listIterator(); 291 int i = 0; 292 while (iterator.hasNext()) 293 assertTrue(Integer.toString(i++), iterator.next() instanceof ThreadReference); 294 } 295 /** 296 * Test JDI classesByName() while the test program has not been started. 297 */ testNotStartedClassesByName()298 public void testNotStartedClassesByName() { 299 List<?> classes = fVM.classesByName("java.lang.Object"); 300 assertEquals("1", 1, classes.size()); 301 } 302 /** 303 * Test JDI allClasses() and JDWP 'VM- Get all classes' 304 * once the test program has been started. 305 */ testStartedAllClasses()306 public void testStartedAllClasses() { 307 308 // The test program has started, the number of classes is != 0 309 List<?> classes = fVM.allClasses(); 310 assertTrue("1", !classes.isEmpty()); 311 312 // Collect names of received classes 313 String[] names = new String[classes.size()]; 314 ListIterator<?> iterator = classes.listIterator(); 315 int i = 0; 316 while (iterator.hasNext()) { 317 ReferenceType type = (ReferenceType) iterator.next(); 318 names[i++] = type.name(); 319 } 320 321 // Check that they are the expected names 322 String[] expected = 323 new String[] { 324 "java.lang.Object", 325 "java.util.Date", 326 "org.eclipse.debug.jdi.tests.program.Printable", 327 "org.eclipse.debug.jdi.tests.program.MainClass" }; 328 for (int j = 0; j < expected.length; j++) { 329 boolean isIncluded = false; 330 iteration : for (int k = 0; k < names.length; k++) { 331 if (names[k].equals(expected[j])) { 332 isIncluded = true; 333 break iteration; 334 } 335 } 336 assertTrue("2." + j, isIncluded); 337 } 338 } 339 /** 340 * Test JDI allThreads() and JDWP 'VM - Get all threads' 341 * once the test program has been started. 342 */ testStartedAllThreads()343 public void testStartedAllThreads() { 344 345 // The test program has started, the number of threads is != 0 346 List<?> threads = fVM.allThreads(); 347 assertTrue("1", !threads.isEmpty()); 348 349 // Collect names of received threads 350 String[] names = new String[threads.size()]; 351 ListIterator<?> iterator = threads.listIterator(); 352 int i = 0; 353 while (iterator.hasNext()) { 354 ThreadReference thread = (ThreadReference) iterator.next(); 355 names[i++] = thread.name(); 356 } 357 358 // Check that they contain at least the expected names 359 String[] expected = new String[] { "Test Thread" }; 360 boolean isIncluded = false; 361 iteration : for (int j = 0; j < expected.length; j++) { 362 for (int k = 0; k < names.length; k++) { 363 if (expected[j].equals(names[k])) { 364 isIncluded = true; 365 break iteration; 366 } 367 } 368 } 369 assertTrue("2", isIncluded); 370 } 371 /** 372 * Test JDI classesByName() once the test program has been started. 373 */ testStartedClassesByName()374 public void testStartedClassesByName() { 375 376 // The test program has started, the number of java.lang.Object is 1 377 List<?> classes = fVM.classesByName("java.lang.Object"); 378 assertEquals("1", classes.size(), 1); 379 380 // Collect names of received classes 381 String[] names = new String[classes.size()]; 382 ListIterator<?> iterator = classes.listIterator(); 383 int i = 0; 384 while (iterator.hasNext()) { 385 ReferenceType type = (ReferenceType) iterator.next(); 386 names[i++] = type.name(); 387 } 388 389 // Check that they are all "java.lang.Object" 390 for (int j = 0; j < names.length; j++) { 391 assertEquals("2." + j, "java.lang.Object", names[j]); 392 } 393 } 394 /** 395 * Test JDI suspend() and resume() once the test program has been started. 396 */ testStartedSuspendResume()397 public void testStartedSuspendResume() { 398 // Suspend 399 fVM.suspend(); 400 ListIterator<?> threads = fVM.allThreads().listIterator(); 401 while (threads.hasNext()) { 402 ThreadReference thread = (ThreadReference) threads.next(); 403 assertTrue("1." + thread.name(), thread.isSuspended()); 404 } 405 406 // Resume 407 fVM.resume(); 408 // Cannot assertTrue that all threads are not suspended because they might have been suspended 409 // by the program itself 410 411 // Suspend VM and suspend one thread 412 fVM.suspend(); 413 threads = fVM.allThreads().listIterator(); 414 ThreadReference suspended = getThread(); 415 suspended.suspend(); 416 while (threads.hasNext()) { 417 ThreadReference thread = (ThreadReference) threads.next(); 418 assertTrue("2." + thread.name(), thread.isSuspended()); 419 } 420 421 // Resume VM and ensure that the one thread that was suspended is still suspended 422 fVM.resume(); 423 assertTrue("3", suspended.isSuspended()); 424 } 425 /** 426 * Test JDI topLevelThreadGroups(). 427 */ testStartedTopLevelThreadGroups()428 public void testStartedTopLevelThreadGroups() { 429 List<?> topLevelThreadGroups = fVM.topLevelThreadGroups(); 430 assertEquals("1", 1, topLevelThreadGroups.size()); 431 assertTrue("2", topLevelThreadGroups.get(0) instanceof ThreadGroupReference); 432 } 433 } 434