KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > JPDAStepTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.debugger.jpda;
21
22 import com.sun.jdi.StackFrame;
23 import org.netbeans.api.debugger.ActionsManager;
24 import org.netbeans.api.debugger.DebuggerManager;
25 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
26 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
27 import org.netbeans.junit.NbTestCase;
28
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
32 import org.netbeans.modules.debugger.jpda.models.JPDAThreadImpl;
33
34
35 /**
36  * Tests JPDAstep (step in, step out and step over).
37  *
38  * @author Roman Ondruska
39  */

40 public class JPDAStepTest extends NbTestCase {
41
42     private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
43     private String JavaDoc sourceRoot = System.getProperty ("test.dir.src");
44     private JPDASupport support;
45     
46     private Object JavaDoc STEP_LOCK = new Object JavaDoc();
47     
48     private boolean stepExecFired = false;
49
50     public JPDAStepTest (String JavaDoc s) {
51         super (s);
52     }
53
54      public void testStepInto () throws Exception JavaDoc {
55         try {
56             JPDASupport.removeAllBreakpoints ();
57             LineBreakpoint lb = LineBreakpoint.create (
58                 Utils.getURL(sourceRoot +
59                     "org/netbeans/api/debugger/jpda/testapps/StepApp.java"),
60                 30
61             );
62             dm.addBreakpoint (lb);
63             lb.addJPDABreakpointListener(new JPDABreakpointListener() {
64                 public void breakpointReached(JPDABreakpointEvent event) {
65                     System.err.println("Breakpoint Reached: "+event.getSource());
66                 }
67             });
68             support = JPDASupport.attach
69                 ("org.netbeans.api.debugger.jpda.testapps.StepApp");
70             support.waitState (JPDADebugger.STATE_STOPPED);
71             dm.removeBreakpoint (lb);
72             assertEquals (
73                 "Execution stopped in wrong class on breakpoint",
74                 getCurrentClassName(),
75                 "org.netbeans.api.debugger.jpda.testapps.StepApp"
76             );
77             assertEquals (
78                 "Execution stopped at wrong line",
79                 30,
80                 getCurrentLineNumber()
81             );
82             stepCheck (
83                 JPDAStep.STEP_OVER,
84                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
85                 31
86             );
87             stepCheck (
88                 JPDAStep.STEP_INTO,
89                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
90                 42
91             );
92             stepCheck (
93                 JPDAStep.STEP_INTO,
94                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
95                 43
96             );
97             stepCheck (
98                 JPDAStep.STEP_INTO,
99                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
100                 48
101             );
102             stepCheck (
103                 JPDAStep.STEP_OVER,
104                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
105                 49
106             );
107             stepCheck (
108                 JPDAStep.STEP_INTO,
109                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
110                 54
111             );
112            
113             // resume VM
114
((JPDADebuggerImpl)support.getDebugger()).getVirtualMachine().resume();
115             support.waitState (JPDADebugger.STATE_DISCONNECTED);
116          
117         } finally {
118             support.doFinish ();
119         }
120     }
121      
122      public void testStepOver () throws Exception JavaDoc {
123         try {
124             JPDASupport.removeAllBreakpoints ();
125             LineBreakpoint lb = LineBreakpoint.create (
126                 Utils.getURL(sourceRoot +
127                     "org/netbeans/api/debugger/jpda/testapps/StepApp.java"),
128                 30
129             );
130             dm.addBreakpoint (lb);
131             support = JPDASupport.attach
132                 ("org.netbeans.api.debugger.jpda.testapps.StepApp");
133             support.waitState (JPDADebugger.STATE_STOPPED);
134             dm.removeBreakpoint (lb);
135             assertEquals (
136                 "Execution stopped in wrong class",
137                 getCurrentClassName(),
138                 "org.netbeans.api.debugger.jpda.testapps.StepApp"
139             );
140             assertEquals (
141                 "Execution stopped at wrong line",
142                 30,
143                 getCurrentLineNumber()
144             );
145             stepCheck (
146                 JPDAStep.STEP_OVER,
147                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
148                 31
149             );
150             stepCheck (
151                 JPDAStep.STEP_OVER,
152                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
153                 32
154             );
155             stepCheck (
156                 JPDAStep.STEP_OVER,
157                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
158                 33
159             );
160             stepCheck (
161                 JPDAStep.STEP_OVER,
162                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
163                 34
164             );
165             stepCheck (
166                 JPDAStep.STEP_OVER,
167                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
168                 35
169             );
170             
171             // resume VM
172
((JPDADebuggerImpl)support.getDebugger()).getVirtualMachine().resume();
173             support.waitState (JPDADebugger.STATE_DISCONNECTED);
174         } finally {
175             support.doFinish ();
176         }
177             
178     }
179
180     public void testStepOut () throws Exception JavaDoc {
181         try {
182             JPDASupport.removeAllBreakpoints ();
183             LineBreakpoint lb = LineBreakpoint.create (
184                 Utils.getURL(sourceRoot +
185                     "org/netbeans/api/debugger/jpda/testapps/StepApp.java"),
186                 30
187             );
188             dm.addBreakpoint (lb);
189             support = JPDASupport.attach
190                 ("org.netbeans.api.debugger.jpda.testapps.StepApp");
191             support.waitState (JPDADebugger.STATE_STOPPED);
192             dm.removeBreakpoint (lb);
193             assertEquals (
194                 "Execution stopped in wrong class",
195                 getCurrentClassName(),
196                 "org.netbeans.api.debugger.jpda.testapps.StepApp"
197             );
198             assertEquals (
199                 "Execution stopped at wrong line",
200                 30,
201                 getCurrentLineNumber()
202             );
203             stepCheck (
204                 JPDAStep.STEP_OVER,
205                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
206                 31
207             );
208             stepCheck (
209                 JPDAStep.STEP_INTO,
210                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
211                 42
212             );
213             stepCheck (
214                 JPDAStep.STEP_OVER,
215                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
216                 43
217             );
218             stepCheck (
219                 JPDAStep.STEP_INTO,
220                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
221                 48
222             );
223             stepCheck (
224                 JPDAStep.STEP_OUT,
225                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
226                 43
227             );
228             stepCheck (
229                 JPDAStep.STEP_OUT,
230                 "org.netbeans.api.debugger.jpda.testapps.StepApp",
231                 31
232             );
233             
234             // resume VM
235
((JPDADebuggerImpl)support.getDebugger()).getVirtualMachine().resume();
236             support.waitState (JPDADebugger.STATE_DISCONNECTED);
237         } finally {
238             support.doFinish ();
239         }
240     }
241     
242     private void stepCheck (
243         int stepType,
244         String JavaDoc clsExpected,
245         int lineExpected
246     ) {
247         stepExecFired = false;
248         JPDAStep step = support.getDebugger().createJPDAStep(JPDAStep.STEP_LINE, stepType);
249  
250         step.addPropertyChangeListener(JPDAStep.PROP_STATE_EXEC, new PropertyChangeListener JavaDoc() {
251                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
252                         synchronized (STEP_LOCK) {
253                             stepExecFired = true;
254                             STEP_LOCK.notify();
255                         }
256                         
257                         
258                     }
259         });
260         step.addStep(support.getDebugger().getCurrentThread());
261         ((JPDADebuggerImpl)support.getDebugger()).getVirtualMachine().resume();
262      
263         synchronized (STEP_LOCK) {
264             while (! stepExecFired) {
265                 try {
266                     STEP_LOCK.wait ();
267                 } catch (InterruptedException JavaDoc ex) {
268                     ex.printStackTrace ();
269                 }
270             }
271         }
272
273         assertEquals(
274             "Execution stopped in wrong class",
275             clsExpected,
276             getCurrentClassName()
277         );
278         assertEquals(
279             "Execution stopped at wrong line",
280             lineExpected,
281             getCurrentLineNumber()
282         );
283         
284     }
285     
286     private String JavaDoc getCurrentClassName() {
287         JPDADebuggerImpl debugger = (JPDADebuggerImpl) support.getDebugger();
288         
289         String JavaDoc className = null;
290         
291         try {
292             JPDAThreadImpl jpdaThread = (JPDAThreadImpl) debugger.getCurrentThread();
293             if (jpdaThread == null) {
294                 System.err.println("NULL Current Thread!");
295                 Thread.dumpStack();
296             } else {
297                 StackFrame sf = jpdaThread.getThreadReference().frame(0);
298                 if (sf == null) {
299                     System.err.println("No stack frame!");
300                     Thread.dumpStack();
301                 } else {
302                     className = sf.location().declaringType().name();
303                 }
304             }
305         } catch (Exception JavaDoc e) {
306             e.printStackTrace();
307         }
308         return className;
309     }
310     
311     private int getCurrentLineNumber() {
312         JPDADebuggerImpl debugger = (JPDADebuggerImpl) support.getDebugger();
313         
314         int lineNumber = -1;
315         
316         try {
317             lineNumber = ((JPDAThreadImpl)debugger.getCurrentThread()).
318                 getThreadReference().frame(0).location().lineNumber();
319         } catch (Exception JavaDoc e) {
320             e.printStackTrace();
321         }
322         return lineNumber;
323     }
324         
325 }
326
327
Popular Tags