KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > debug > DebugTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.debug;
35
36 import java.io.*;
37
38 import edu.rice.cs.drjava.config.*;
39 import edu.rice.cs.drjava.model.*;
40
41 import edu.rice.cs.util.swing.Utilities;
42
43 /** Tests the JPDA-based debugger.
44  * @version $Id: DebugTest.java 4031 2006-11-15 22:09:06Z rcartwright $
45  */

46 public final class DebugTest extends DebugTestCase implements OptionConstants {
47   
48   /** Tests startUp and shutdown, ensuring that all appropriate fields are initialized. Ensures multiple startups
49    * and shutdowns work, even after a reset, which changes the debug port.
50    */

51   public void testStartupAndShutdown() throws DebugException, InterruptedException JavaDoc {
52     _log.log("----testStartupAndShutdown----");
53     DebugTestListener debugListener = new DebugStartAndStopListener();
54     _debugger.addListener(debugListener);
55
56     // Start debugger
57
synchronized(_notifierLock) {
58       _debugger.startUp();
59       _setPendingNotifies(1); // startUp
60
while (_pendingNotifies > 0) _notifierLock.wait();
61     }
62     debugListener.assertDebuggerStartedCount(1); //fires
63
debugListener.assertDebuggerShutdownCount(0);
64
65     // Check fields and status
66
assertTrue("Debug Manager should be ready", _debugger.isReady());
67     assertNotNull("EventRequestManager should not be null after startUp", _debugger.getEventRequestManager());
68     assertNotNull("PendingRequestManager should not be null after startUp", _debugger.getPendingRequestManager());
69
70     // Shutdown the debugger
71
synchronized(_notifierLock) {
72       _debugger.shutdown();
73       _setPendingNotifies(1); // shutdown
74
while (_pendingNotifies > 0) _notifierLock.wait();
75     }
76     debugListener.assertDebuggerStartedCount(1);
77     debugListener.assertDebuggerShutdownCount(1); //fires
78

79     // Start debugger again without resetting
80
synchronized(_notifierLock) {
81       _debugger.startUp();
82       _setPendingNotifies(1); // startUp
83
while (_pendingNotifies > 0) _notifierLock.wait();
84     }
85     debugListener.assertDebuggerStartedCount(2); //fires
86
debugListener.assertDebuggerShutdownCount(1);
87     
88     // Define listener that will count notification events
89
InterpretListener resetListener = new InterpretListener() {
90       public void interactionStarted() {
91         // Don't notify: happens in the same thread
92
_log.log("interactionStarted called in resetListener");
93         interactionStartCount++;
94       }
95       public void interactionEnded() {
96          // Don't notify: happens in the same thread
97
_log.log("interactionEnded called in resetListener");
98         interactionEndCount++;
99       }
100       public void interpreterChanged(boolean inProgress) {
101         // Don't notify: happens in the same thread
102
_log.log("interpreterChanged called in resetListener");
103         interpreterChangedCount++;
104       }
105       public void interpreterResetting() {
106         // Don't notify: happens in the same thread
107
_log.log("interpreterResetting called in resetListener");
108         interpreterResettingCount++;
109       }
110       public void interpreterReady(File wd) {
111         synchronized(_notifierLock) {
112           interpreterReadyCount++;
113           _log.log("interpreterReady " + interpreterReadyCount);
114           _notifyLock();
115         }
116       }
117       public void consoleReset() { consoleResetCount++; }
118     };
119     
120     // Install listener
121
_model.addListener(resetListener); // shutdown, interpreterReady
122
_setPendingNotifies(2);
123     
124     // Use the interpeter so that resetInteractions restarts the slave JVM
125
// interpret("2+2");
126

127     _model.resetInteractions(FileOption.NULL_FILE);
128
129     synchronized(_notifierLock) { while (_pendingNotifies > 0) _notifierLock.wait(); }
130     
131     _model.removeListener(resetListener);
132     
133     resetListener.assertInterpreterResettingCount(1); //fires (no waiting)
134
resetListener.assertInterpreterReadyCount(1); //fires
135
debugListener.assertDebuggerStartedCount(2);
136     debugListener.assertDebuggerShutdownCount(2); //fires
137

138
139     // Start debugger again after reset
140
synchronized(_notifierLock) {
141       _debugger.startUp();
142       _setPendingNotifies(1); // startUp
143
while (_pendingNotifies > 0) _notifierLock.wait();
144     }
145     debugListener.assertDebuggerStartedCount(3); //fires
146
debugListener.assertDebuggerShutdownCount(2);
147
148     // Shutdown the debugger
149
synchronized(_notifierLock) {
150       _debugger.shutdown();
151       _setPendingNotifies(1); // shutdown
152
while (_pendingNotifies > 0) _notifierLock.wait();
153     }
154     debugListener.assertDebuggerStartedCount(3);
155     debugListener.assertDebuggerShutdownCount(3); //fires
156

157     _debugger.removeListener(debugListener);
158   }
159
160
161   /**
162    * Test that when two threads are suspended setCurrentThread can be used
163    * to switch between them in the debugger
164    */

165   public synchronized void testMultiThreadedSetCurrentThread() throws Exception JavaDoc {
166     _log.log("----testMultiThreadedSetCurrentThread----");
167     BreakpointTestListener debugListener = new BreakpointTestListener();
168     _debugger.addListener(debugListener);
169
170     // Start up
171
OpenDefinitionsDocument doc = _startupDebugger("Monkey.java",
172                                                    MONKEY_CLASS);
173
174     // Set two breakpoints
175
int index = MONKEY_CLASS.indexOf("System.out.println(\"I\'m a thread! Yeah!\");");
176     _debugger.toggleBreakpoint(doc,index, 11, true);
177     index = MONKEY_CLASS.indexOf("System.out.println(\"James likes bananas!\");");
178     _debugger.toggleBreakpoint(doc,index, 17, true);
179
180     // Run the main() method, hitting both breakpoints in different threads
181
synchronized(_notifierLock) {
182       interpretIgnoreResult("java Monkey");
183       _setPendingNotifies(6); // (suspended, updated, breakpointReached) * 2
184
while (_pendingNotifies > 0) _notifierLock.wait();
185     }
186     DebugThreadData threadA = new DebugThreadData(_debugger.getCurrentThread());
187     DebugThreadData threadB = new DebugThreadData(_debugger.getThreadAt(1));
188     synchronized(_notifierLock) {
189       _asyncDoSetCurrentThread(threadB);
190       _setPendingNotifies(2); // updated, suspended
191
while (_pendingNotifies > 0) _notifierLock.wait();
192     }
193
194     DebugThreadData thread1 = new DebugThreadData(_debugger.getThreadAt(1));
195     DebugThreadData thread2 = new DebugThreadData(_debugger.getCurrentThread());
196
197     // make sure threads have switched places
198
assertTrue(thread1.getUniqueID() == threadA.getUniqueID());
199     assertTrue(thread2.getUniqueID() == threadB.getUniqueID());
200
201     // Shut down
202
_shutdownAndWaitForInteractionEnded();
203     _debugger.removeListener(debugListener);
204   }
205
206   /**
207    * Tests that setCurrentThread works for multiple threads
208    *
209    * This test has been commented out because we do not support setting the
210    * current thread to be an unsuspended thread right now
211    *
212   public synchronized void testMultiThreadedSetCurrentThread() throws Exception {
213     _log.log("----testMultiThreadedSetCurrentThread----");
214     BreakpointTestListener debugListener = new BreakpointTestListener();
215     _debugger.addListener(debugListener);
216
217     // Start up
218     OpenDefinitionsDocument doc = _startupDebugger("Suspender.java",
219                                                    SUSPEND_CLASS);
220
221     int index = SUSPEND_CLASS.indexOf("int a = 1;");
222     _debugger.toggleBreakpoint(doc,index,5,true);
223
224     // Run the main() method, hitting breakpoints
225     synchronized(_notifierLock) {
226       interpretIgnoreResult("java Suspender");
227       _setPendingNotifies(3); // suspended, updated, breakpointReached
228       while (_pendingNotifies > 0) _notifierLock.wait();
229     }
230     final DebugThreadData thread = new DebugThreadData(_debugger.getCurrentThread());
231     synchronized(_notifierLock) {
232        // _debugger.setCurrentThread(...);
233        // must be executed in another thread because otherwise the notifies
234        // will be received before the _notifierLock is released
235       new Thread() {
236         public void run() {
237           try {
238             _debugger.resume();
239             _doSetCurrentThread(thread);
240           }
241           catch (DebugException excep) {
242             excep.printStackTrace();
243             fail("_doSetCurrentThread failed in testMultiThreadedSetCurrentThread");
244           }
245         }
246       }.start();
247       _setPendingNotifies(2); // suspended, updated
248       while (_pendingNotifies > 0) _notifierLock.wait();
249     }
250     // Ensure thread suspended
251     debugListener.assertCurrThreadSuspendedCount(2); //fires
252
253     // Shut down
254     _shutdownWithoutSuspendedInteraction();
255     _debugger.removeListener(debugListener);
256   }*/

257
258   /** Tests that breakpoints behave correctly for multiple threads. */
259   public synchronized void testMultiThreadedBreakpointsAndStep() throws Exception JavaDoc {
260     _log.log("----testMultiThreadedBreakpointsAndStep----");
261     BreakpointTestListener debugListener = new BreakpointTestListener();
262     _debugger.addListener(debugListener);
263
264     // Start up
265
OpenDefinitionsDocument doc = _startupDebugger("Monkey.java", MONKEY_CLASS);
266
267     // Set breakpoints
268
int index = MONKEY_CLASS.indexOf("System.out.println(\"I\'m a thread! Yeah!\");");
269     _debugger.toggleBreakpoint(doc,index,11,true);
270     index = MONKEY_CLASS.indexOf("System.out.println(\"I just woke up. I\'m a big boy now.\");");
271     _debugger.toggleBreakpoint(doc,index,16,true);
272     
273     Utilities.clearEventQueue();
274     debugListener.assertRegionAddedCount(2);
275
276     // Run the main method, hitting breakpoints
277
synchronized(_notifierLock) {
278       interpretIgnoreResult("java Monkey");
279       _setPendingNotifies(6); // (suspended, updated, breakpointReached) x 2
280
while (_pendingNotifies > 0) _notifierLock.wait();
281     }
282
283     DebugThreadData thread = new DebugThreadData(_debugger.getCurrentThread());
284     // Resumes one thread, finishing it and switching to the next break point
285
synchronized(_notifierLock) {
286       _asyncResume();
287       _setPendingNotifies(2); // suspended, updated
288
// no longer get a currThreadDied since we immediately
289
// switch to the next thread
290
while (_pendingNotifies > 0) _notifierLock.wait();
291     }
292
293     DebugThreadData thread2 = new DebugThreadData(_debugger.getCurrentThread());
294     assertTrue("testMultiThreadedBreakPoint thread references should not be equal",
295                !thread.getName().equals(thread2.getName()));
296
297     // Ensure breakpoint is hit
298
debugListener.assertBreakpointReachedCount(2); //fires
299
debugListener.assertThreadLocationUpdatedCount(3); //fires
300
debugListener.assertCurrThreadSuspendedCount(3); //fires
301
debugListener.assertCurrThreadResumedCount(1);
302     _debugger.removeListener(debugListener);
303
304     _log.log("Testing stepping...");
305
306     // Step
307
StepTestListener stepTestListener = new StepTestListener();
308     _debugger.addListener(stepTestListener);
309     synchronized(_notifierLock) {
310       _asyncStep(Debugger.STEP_INTO);
311       _setPendingNotifies(2); // suspended, updated
312
while (_pendingNotifies > 0) _notifierLock.wait();
313     }
314     stepTestListener.assertStepRequestedCount(1);
315     _debugger.removeListener(stepTestListener);
316
317     DebugThreadData thread3 = new DebugThreadData(_debugger.getCurrentThread());
318     assertEquals("testMultiThreadedBreakPoint thread references should be equal",
319                  thread2.getName(), thread3.getName());
320
321     // Resume until finished, waiting for interpret call to end
322
_debugger.addListener(debugListener);
323     InterpretListener interpretListener = new InterpretListener();
324     _model.addListener(interpretListener);
325     synchronized(_notifierLock) {
326       _asyncResume();
327       _setPendingNotifies(3); // interactionEnded, interpreterChanged, currThreadDied
328
// we get a currThreadDied here since it's the last thread
329
while (_pendingNotifies > 0) _notifierLock.wait();
330     }
331     interpretListener.assertInteractionEndCount(1);
332     _model.removeListener(interpretListener);
333
334     // Shut down
335
_shutdownWithoutSuspendedInteraction();
336     _debugger.removeListener(debugListener);
337   }
338
339
340
341   /**
342    * Tests that breakpoints behave correctly.
343    */

344   public synchronized void testBreakpoints() throws Exception JavaDoc {
345     _log.log("----testBreakpoints----");
346     BreakpointTestListener debugListener = new BreakpointTestListener();
347     _debugger.addListener(debugListener);
348
349     // Start up
350
OpenDefinitionsDocument doc = _startupDebugger("DrJavaDebugClass.java",
351                                                    DEBUG_CLASS);
352
353    // Add breakpoint before class is loaded
354
_debugger.toggleBreakpoint(doc,DEBUG_CLASS.indexOf("bar();"),4,true);
355     
356     Utilities.clearEventQueue();
357     debugListener.assertRegionAddedCount(1);
358
359     // Run the foo() method, hitting breakpoint
360
synchronized(_notifierLock) {
361       interpretIgnoreResult("new DrJavaDebugClass().foo()");
362       _setPendingNotifies(3); // suspended, updated, breakpointReached
363
while (_pendingNotifies > 0) _notifierLock.wait();
364     }
365
366     _log.log("----After breakpoint:\n" + getInteractionsText());
367
368     // Ensure breakpoint is hit
369
debugListener.assertBreakpointReachedCount(1); //fires
370
debugListener.assertThreadLocationUpdatedCount(1); //fires
371
debugListener.assertCurrThreadSuspendedCount(1); //fires
372
debugListener.assertCurrThreadResumedCount(0);
373     debugListener.assertCurrThreadDiedCount(0);
374     assertInteractionsContains("Foo Line 1");
375     assertInteractionsDoesNotContain("Bar Line 1");
376
377     _log.log("adding another breakpoint");
378
379     // Set another breakpoint (after is class loaded)
380
_debugger.toggleBreakpoint(doc, DEBUG_CLASS.indexOf("System.out.println(\"Bar Line 2\")"), 9, true);
381     
382     Utilities.clearEventQueue();
383     debugListener.assertRegionAddedCount(2);
384
385     // Resume until next breakpoint
386
synchronized(_notifierLock) {
387       _log.log("resuming");
388       _asyncResume();
389       _setPendingNotifies(3); // suspended, updated, breakpointReached
390
while (_pendingNotifies > 0) _notifierLock.wait();
391     }
392     _log.log("----After one resume:\n" + getInteractionsText());
393     debugListener.assertCurrThreadResumedCount(1); //fires (no waiting)
394
debugListener.assertBreakpointReachedCount(2); //fires
395
debugListener.assertThreadLocationUpdatedCount(2); //fires
396
debugListener.assertCurrThreadSuspendedCount(2); //fires
397
debugListener.assertCurrThreadDiedCount(0);
398     assertInteractionsContains("Bar Line 1");
399     assertInteractionsDoesNotContain("Bar Line 2");
400
401     // Resume until finished, waiting for interpret call to end
402
InterpretListener interpretListener = new InterpretListener();
403     _model.addListener(interpretListener);
404     synchronized(_notifierLock) {
405       _log.log("-------- Resuming --------");
406       _asyncResume();
407       _setPendingNotifies(3); // interactionEnded, interpreterChanged, currThreadDied
408
// here, we get a currThreadDied since it's the last thread
409
while (_pendingNotifies > 0) _notifierLock.wait();
410     }
411     interpretListener.assertInteractionEndCount(1);
412     _model.removeListener(interpretListener);
413
414     _log.log("----After second resume:\n" + getInteractionsText());
415     debugListener.assertCurrThreadResumedCount(2); //fires (no waiting)
416
debugListener.assertBreakpointReachedCount(2);
417     debugListener.assertThreadLocationUpdatedCount(2);
418     debugListener.assertCurrThreadSuspendedCount(2);
419     assertInteractionsContains("Foo Line 3");
420
421     // Shut down
422
_shutdownWithoutSuspendedInteraction();
423     _debugger.removeListener(debugListener);
424   }
425
426   /**
427    * Tests that the debugger will stop at a breakpoint in one class
428    * when the invoking method resides in a class with the same
429    * prefix in its name. (bug #769764)
430    * (ie. Class DrJavaDebugTest2 has a method which calls something
431    * in class DrJavaDebugTest, which has a breakpoint.)
432    */

433   public synchronized void testBreakpointsWithSameNamePrefix() throws Exception JavaDoc {
434     _log.log("----testBreakpointsWithSameNamePrefix----");
435     BreakpointTestListener debugListener = new BreakpointTestListener();
436     _debugger.addListener(debugListener);
437
438     // Start up
439
OpenDefinitionsDocument doc = _startupDebugger("DrJavaDebugClass.java",
440                                                    DEBUG_CLASS);
441
442    // Add breakpoint in DrJavaDebugClass before class is loaded
443
_debugger.toggleBreakpoint(doc,DEBUG_CLASS.indexOf("Bar Line 1"),8,true);
444     
445     Utilities.clearEventQueue();
446     debugListener.assertRegionAddedCount(1);
447
448     // Run the baz() method, hitting breakpoint
449
synchronized(_notifierLock) {
450       interpretIgnoreResult("new DrJavaDebugClass2().baz()");
451       _setPendingNotifies(3); // suspended, updated, breakpointReached
452
while (_pendingNotifies > 0) _notifierLock.wait();
453     }
454
455     _log.log("----After breakpoint:\n" + getInteractionsText());
456
457     // Ensure breakpoint is hit
458
debugListener.assertBreakpointReachedCount(1); //fires
459
debugListener.assertThreadLocationUpdatedCount(1); //fires
460
debugListener.assertCurrThreadSuspendedCount(1); //fires
461
debugListener.assertCurrThreadResumedCount(0);
462     debugListener.assertCurrThreadDiedCount(0);
463     assertInteractionsContains("Baz Line 1");
464     assertInteractionsDoesNotContain("Bar Line 1");
465
466     // Resume until finished, waiting for interpret call to end
467
InterpretListener interpretListener = new InterpretListener();
468     _model.addListener(interpretListener);
469     synchronized(_notifierLock) {
470       _log.log("-------- Resuming --------");
471       _asyncResume();
472       _setPendingNotifies(3); // interactionEnded, interpreterChanged, currThreadDied
473
// here, we get a currThreadDied since it's the last thread
474
while (_pendingNotifies > 0) _notifierLock.wait();
475     }
476     interpretListener.assertInteractionEndCount(1);
477     _model.removeListener(interpretListener);
478
479     _log.log("----After second resume:\n" + getInteractionsText());
480     debugListener.assertCurrThreadResumedCount(1); //fires (no waiting)
481
debugListener.assertBreakpointReachedCount(1);
482     debugListener.assertThreadLocationUpdatedCount(1);
483     debugListener.assertCurrThreadSuspendedCount(1);
484     assertInteractionsContains("Bar Line 2");
485
486     // Shut down
487
_shutdownWithoutSuspendedInteraction();
488     _debugger.removeListener(debugListener);
489   }
490
491   /**
492    * Tests that breakpoints and steps behave correctly.
493    */

494   public void testStepInto() throws Exception JavaDoc {
495     _log.log("----testStepInto----");
496     StepTestListener debugListener = new StepTestListener();
497     _debugger.addListener(debugListener);
498
499     // Start up
500
OpenDefinitionsDocument doc = _startupDebugger("DrJavaDebugClass.java",
501                                                    DEBUG_CLASS);
502
503     // Add a breakpoint
504
_debugger.toggleBreakpoint(doc,DEBUG_CLASS.indexOf("bar();"),4,true);
505     
506     Utilities.clearEventQueue();
507     debugListener.assertRegionAddedCount(1);
508
509     // Run the foo() method, hitting breakpoint
510
synchronized(_notifierLock) {
511       interpretIgnoreResult("new DrJavaDebugClass().foo()");
512       _setPendingNotifies(3); // suspended, updated, breakpointReached
513
while (_pendingNotifies > 0) _notifierLock.wait();
514     }
515
516     _log.log("----After breakpoint:\n" + getInteractionsText());
517
518     // Ensure breakpoint is hit
519
debugListener.assertBreakpointReachedCount(1); //fires
520
debugListener.assertThreadLocationUpdatedCount(1); //fires
521
debugListener.assertCurrThreadSuspendedCount(1); //fires
522
debugListener.assertCurrThreadResumedCount(0);
523     debugListener.assertCurrThreadDiedCount(0);
524     assertInteractionsContains("Foo Line 1");
525     assertInteractionsDoesNotContain("Bar Line 1");
526
527     // Step into bar() method
528
synchronized(_notifierLock) {
529       _asyncStep(Debugger.STEP_INTO);
530       _setPendingNotifies(2); // suspended, updated
531
while (_pendingNotifies > 0) _notifierLock.wait();
532     }
533     debugListener.assertStepRequestedCount(1); // fires (don't wait)
534
debugListener.assertCurrThreadResumedCount(1); // fires (don't wait)
535
debugListener.assertThreadLocationUpdatedCount(2); // fires
536
debugListener.assertCurrThreadSuspendedCount(2); // fires
537
debugListener.assertBreakpointReachedCount(1);
538     debugListener.assertCurrThreadDiedCount(0);
539     assertInteractionsDoesNotContain("Bar Line 1");
540
541     // Step to next line
542
synchronized(_notifierLock) {
543       _asyncStep(Debugger.STEP_OVER);
544       _setPendingNotifies(2); // suspended, updated
545
while (_pendingNotifies > 0) _notifierLock.wait();
546     }
547
548     _log.log("****"+getInteractionsText());
549     debugListener.assertStepRequestedCount(2); // fires (don't wait)
550
debugListener.assertCurrThreadResumedCount(2); // fires (don't wait)
551
debugListener.assertThreadLocationUpdatedCount(3); // fires
552
debugListener.assertCurrThreadDiedCount(0);
553     debugListener.assertCurrThreadSuspendedCount(3); // fires
554
debugListener.assertBreakpointReachedCount(1);
555     assertInteractionsContains("Bar Line 1");
556     assertInteractionsDoesNotContain("Bar Line 2");
557
558     // Step to next line
559
synchronized(_notifierLock) {
560       _asyncStep(Debugger.STEP_OVER);
561       _setPendingNotifies(2); // suspended, updated
562
while (_pendingNotifies > 0) _notifierLock.wait();
563     }
564     debugListener.assertStepRequestedCount(3); // fires (don't wait)
565
debugListener.assertCurrThreadResumedCount(3); // fires (don't wait)
566
debugListener.assertThreadLocationUpdatedCount(4); // fires
567
debugListener.assertCurrThreadDiedCount(0);
568     debugListener.assertCurrThreadSuspendedCount(4); // fires
569
debugListener.assertBreakpointReachedCount(1);
570     assertInteractionsContains("Bar Line 2");
571     assertInteractionsDoesNotContain("Foo Line 3");
572
573     // Step twice to print last line in Foo
574
synchronized(_notifierLock) {
575       _asyncStep(Debugger.STEP_OVER);
576       _setPendingNotifies(2); // suspended, updated
577
while (_pendingNotifies > 0) _notifierLock.wait();
578     }
579     synchronized(_notifierLock) {
580       _asyncStep(Debugger.STEP_OVER);
581       _setPendingNotifies(2); // suspended, updated
582
while (_pendingNotifies > 0) _notifierLock.wait();
583     }
584     debugListener.assertStepRequestedCount(5); // fires (don't wait)
585
debugListener.assertCurrThreadResumedCount(5); // fires (don't wait)
586
debugListener.assertThreadLocationUpdatedCount(6); // fires
587
debugListener.assertCurrThreadDiedCount(0);
588     debugListener.assertCurrThreadSuspendedCount(6); //fires
589
debugListener.assertBreakpointReachedCount(1);
590     assertInteractionsContains("Foo Line 3");
591
592
593     // Step again to finish, waiting for interpret call to end
594
InterpretListener interpretListener = new InterpretListener();
595     _model.addListener(interpretListener);
596     synchronized(_notifierLock) {
597       _asyncStep(Debugger.STEP_OVER);
598       _setPendingNotifies(3); // interactionEnded, interpreterChanged, currThreadDied
599
// here, we get a currThreadDied since it's the last thread
600
while (_pendingNotifies > 0) _notifierLock.wait();
601     }
602     interpretListener.assertInteractionEndCount(1);
603     _model.removeListener(interpretListener);
604
605     debugListener.assertStepRequestedCount(6); // fires (don't wait)
606
debugListener.assertCurrThreadDiedCount(1);
607
608     // Shut down
609
_shutdownWithoutSuspendedInteraction();
610     _debugger.removeListener(debugListener);
611   }
612
613   /** Tests that stepping out of a method behaves correctly. */
614   public synchronized void testStepOut() throws Exception JavaDoc {
615      _log.log("----testStepOut----");
616     StepTestListener debugListener = new StepTestListener();
617     _debugger.addListener(debugListener);
618
619     // Start up
620
OpenDefinitionsDocument doc = _startupDebugger("DrJavaDebugClass.java",
621                                                    DEBUG_CLASS);
622
623     // Set breakpoint
624
_debugger.toggleBreakpoint(doc,DEBUG_CLASS.indexOf("bar();"),4,true);
625     
626     Utilities.clearEventQueue();
627     debugListener.assertRegionAddedCount(1);
628
629     // Run the foo() method, hitting breakpoint
630
synchronized(_notifierLock) {
631       interpretIgnoreResult("new DrJavaDebugClass().foo()");
632       _setPendingNotifies(3); // suspended, updated, breakpointReached
633
while (_pendingNotifies > 0) _notifierLock.wait();
634     }
635
636     _log.log("----After breakpoint:\n" + getInteractionsText());
637
638     // Ensure breakpoint is hit
639
debugListener.assertBreakpointReachedCount(1); // fires
640
debugListener.assertThreadLocationUpdatedCount(1); // fires
641
debugListener.assertCurrThreadSuspendedCount(1); // fires
642
debugListener.assertCurrThreadResumedCount(0);
643     debugListener.assertCurrThreadDiedCount(0);
644     assertInteractionsContains("Foo Line 1");
645     assertInteractionsDoesNotContain("Bar Line 1");
646
647     // Step into bar() method
648
synchronized(_notifierLock) {
649       _asyncStep(Debugger.STEP_INTO);
650       _setPendingNotifies(2); // suspended, updated
651
while (_pendingNotifies > 0) _notifierLock.wait();
652     }
653     debugListener.assertStepRequestedCount(1); // fires (don't wait)
654
debugListener.assertCurrThreadResumedCount(1); // fires (don't wait)
655
debugListener.assertThreadLocationUpdatedCount(2); //fires
656
debugListener.assertCurrThreadSuspendedCount(2); //fires
657
debugListener.assertBreakpointReachedCount(1);
658     debugListener.assertCurrThreadDiedCount(0);
659     assertInteractionsDoesNotContain("Bar Line 1");
660
661     // Step out of method
662
synchronized(_notifierLock) {
663       _asyncStep(Debugger.STEP_OUT);
664       _setPendingNotifies(2); // suspended, updated
665
while (_pendingNotifies > 0) _notifierLock.wait();
666     }
667
668     _log.log("****"+getInteractionsText());
669     debugListener.assertStepRequestedCount(2); // fires (don't wait)
670
debugListener.assertCurrThreadResumedCount(2); // fires (don't wait)
671
debugListener.assertThreadLocationUpdatedCount(3); // fires
672
debugListener.assertCurrThreadDiedCount(0);
673     debugListener.assertCurrThreadSuspendedCount(3); //fires
674
debugListener.assertBreakpointReachedCount(1);
675     assertInteractionsContains("Bar Line 2");
676     assertInteractionsDoesNotContain("Foo Line 3");
677
678     // Shut down
679
_shutdownAndWaitForInteractionEnded();
680     _debugger.removeListener(debugListener);
681   }
682
683   /**
684    * Tests that stepping works in a public class with a package
685    */

686   public synchronized void testStepOverWithPackage() throws Exception JavaDoc {
687     _log.log("----testStepOverWithPackage----");
688     StepTestListener debugListener = new StepTestListener();
689     _debugger.addListener(debugListener);
690
691     // Create the file in an "a" sub-directory
692
File aDir = new File(_tempDir, "a");
693     aDir.mkdir();
694     File file = new File(aDir, "DrJavaDebugClassWithPackage.java");
695
696     // Start up
697
OpenDefinitionsDocument doc = _startupDebugger(file, DEBUG_CLASS_WITH_PACKAGE);
698
699     // Add a breakpoint
700
_debugger.toggleBreakpoint(doc,DEBUG_CLASS_WITH_PACKAGE.indexOf("foo line 1"), 4,true);
701     
702     Utilities.clearEventQueue();
703     debugListener.assertRegionAddedCount(1);
704
705     // Run the foo() method, hitting breakpoint
706
synchronized(_notifierLock) {
707       interpretIgnoreResult("new a.DrJavaDebugClassWithPackage().foo()");
708       _setPendingNotifies(3); // suspended, updated, breakpointReached
709
while (_pendingNotifies > 0) _notifierLock.wait();
710     }
711
712     _log.log("----After breakpoint:\n" + getInteractionsText());
713
714     // Ensure breakpoint is hit
715
debugListener.assertBreakpointReachedCount(1); //fires
716
debugListener.assertThreadLocationUpdatedCount(1); //fires
717
debugListener.assertCurrThreadSuspendedCount(1); //fires
718
debugListener.assertCurrThreadResumedCount(0);
719     debugListener.assertCurrThreadDiedCount(0);
720     assertInteractionsDoesNotContain("foo line 1");
721
722     // Step over once
723
synchronized(_notifierLock) {
724       _asyncStep(Debugger.STEP_OVER);
725       _setPendingNotifies(2); // suspended, updated
726
while (_pendingNotifies > 0) _notifierLock.wait();
727     }
728     debugListener.assertStepRequestedCount(1); // fires (don't wait)
729
debugListener.assertCurrThreadResumedCount(1); // fires (don't wait)
730
debugListener.assertThreadLocationUpdatedCount(2); // fires
731
debugListener.assertCurrThreadSuspendedCount(2); // fires
732
debugListener.assertBreakpointReachedCount(1);
733     debugListener.assertCurrThreadDiedCount(0);
734     assertInteractionsContains("foo line 1");
735     assertInteractionsDoesNotContain("foo line 2");
736
737     // Step over again
738
synchronized(_notifierLock) {
739       _asyncStep(Debugger.STEP_OVER);
740       _setPendingNotifies(2); // suspended, updated
741
while (_pendingNotifies > 0) _notifierLock.wait();
742     }
743
744     _log.log("****"+getInteractionsText());
745     debugListener.assertStepRequestedCount(2); // fires (don't wait)
746
debugListener.assertCurrThreadResumedCount(2); // fires (don't wait)
747
debugListener.assertThreadLocationUpdatedCount(3); // fires
748
debugListener.assertCurrThreadDiedCount(0);
749     debugListener.assertCurrThreadSuspendedCount(3); // fires
750
debugListener.assertBreakpointReachedCount(1);
751     assertInteractionsContains("foo line 2");
752
753     // Resume until finished, waiting for interpret call to finish
754
InterpretListener interpretListener = new InterpretListener();
755     _model.addListener(interpretListener);
756     synchronized(_notifierLock) {
757       _asyncResume();
758       _setPendingNotifies(3); // interactionEnded, interpreterChanged, currThreadDied
759
// here, we get a currThreadDied since it's the last thread
760
while (_pendingNotifies > 0) _notifierLock.wait();
761     }
762     interpretListener.assertInteractionEndCount(1);
763     _model.removeListener(interpretListener);
764
765     _log.log("----After resume:\n" + getInteractionsText());
766     debugListener.assertCurrThreadResumedCount(3); //fires (no waiting)
767
debugListener.assertBreakpointReachedCount(1);
768     debugListener.assertThreadLocationUpdatedCount(3);
769     debugListener.assertCurrThreadSuspendedCount(3);
770
771
772     // Shut down
773
_shutdownWithoutSuspendedInteraction();
774     _debugger.removeListener(debugListener);
775   }
776
777   /**
778    * Tests the utility function to get a relative directory for a package.
779    */

780   public void testGetPackageDir() {
781     String JavaDoc class1 = "edu.rice.cs.drjava.model.MyTest";
782     String JavaDoc class2 = "MyTest";
783     String JavaDoc sep = System.getProperty("file.separator");
784
785     assertEquals("package dir with package",
786                  "edu" + sep + "rice" + sep + "cs" + sep +
787                  "drjava" + sep + "model" + sep,
788                  _debugger.getPackageDir(class1));
789     assertEquals("package dir without package",
790                  "",
791                  _debugger.getPackageDir(class2));
792   }
793 }
794   
795
Popular Tags