KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.URL JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
26 import org.netbeans.api.debugger.DebuggerManager;
27 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
28 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
29 import org.netbeans.junit.NbTestCase;
30
31 /**
32  * Tests line breakpoints at various places.
33  *
34  * @author Maros Sandor, Jan Jancura
35  */

36 public class LineBreakpointTest extends NbTestCase {
37
38     private static final String JavaDoc TEST_APP = Utils.getURL(System.getProperty ("test.dir.src") +
39         "org/netbeans/api/debugger/jpda/testapps/LineBreakpointApp.java");
40     
41     
42     private JPDASupport support;
43     
44     
45     public LineBreakpointTest (String JavaDoc s) {
46         super (s);
47     }
48
49     public void testConditionalBreakpoint() throws Exception JavaDoc {
50         doTestBreakpointComplete (
51             39,
52             "x==22",
53             JPDABreakpointEvent.CONDITION_FALSE
54         );
55         doTestBreakpointComplete (
56             40,
57             "x==60",
58             JPDABreakpointEvent.CONDITION_TRUE
59         );
60     }
61
62     public void testMultipleLineBreakpoints () throws Exception JavaDoc {
63         try {
64             LineBreakpoint lb1 = LineBreakpoint.create (TEST_APP, 32);
65             LineBreakpoint lb2 = LineBreakpoint.create (TEST_APP, 37);
66             LineBreakpoint lb3 = LineBreakpoint.create (TEST_APP, 109);
67             lb3.setPreferredClassName("org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp$Inner");
68             LineBreakpoint lb4 = LineBreakpoint.create (TEST_APP, 92);
69             lb4.setPreferredClassName("org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp$InnerStatic");
70             LineBreakpoint lb5 = LineBreakpoint.create (TEST_APP, 41);
71
72             DebuggerManager dm = DebuggerManager.getDebuggerManager ();
73             dm.addBreakpoint (lb1);
74             dm.addBreakpoint (lb2);
75             dm.addBreakpoint (lb3);
76             dm.addBreakpoint (lb4);
77             dm.addBreakpoint (lb5);
78
79             TestBreakpointListener tb1 = new TestBreakpointListener (lb1);
80             lb1.addJPDABreakpointListener (tb1);
81             TestBreakpointListener tb2 = new TestBreakpointListener (lb2);
82             lb2.addJPDABreakpointListener (tb2);
83             TestBreakpointListener tb3 = new TestBreakpointListener (lb3);
84             lb3.addJPDABreakpointListener (tb3);
85             TestBreakpointListener tb4 = new TestBreakpointListener (lb4);
86             lb4.addJPDABreakpointListener (tb4);
87             TestBreakpointListener tb5 = new TestBreakpointListener (lb5);
88             lb5.addJPDABreakpointListener (tb5);
89
90             support = JPDASupport.attach (
91                 "org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp"
92             );
93             JPDADebugger debugger = support.getDebugger();
94
95             support.waitState (JPDADebugger.STATE_STOPPED); // 1st breakpoint hit
96
assertEquals (
97                 "Debugger stopped at wrong line",
98                 lb1.getLineNumber (),
99                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
100             );
101             tb2.checkNotNotified();
102             tb3.checkNotNotified();
103             tb4.checkNotNotified();
104             tb5.checkNotNotified();
105
106             support.doContinue();
107             support.waitState (JPDADebugger.STATE_STOPPED); // 2nd breakpoint hit
108
assertEquals (
109                 "Debugger stopped at wrong line",
110                 lb2.getLineNumber (),
111                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
112             );
113             tb3.checkNotNotified();
114             tb4.checkNotNotified();
115             tb5.checkNotNotified();
116
117             support.doContinue ();
118             support.waitState (JPDADebugger.STATE_STOPPED); // 3rd breakpoint hit
119
assertEquals (
120                 "Debugger stopped at wrong line",
121                 lb3.getLineNumber (),
122                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
123             );
124             tb4.checkNotNotified();
125             tb5.checkNotNotified();
126
127             support.doContinue ();
128             support.waitState (JPDADebugger.STATE_STOPPED); // 4th breakpoint hit
129
assertEquals (
130                 "Debugger stopped at wrong line",
131                 lb4.getLineNumber (),
132                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
133             );
134             tb5.checkNotNotified();
135
136             support.doContinue ();
137             support.waitState (JPDADebugger.STATE_STOPPED); // 5th breakpoint hit
138
assertEquals (
139                 "Debugger stopped at wrong line",
140                 lb5.getLineNumber (),
141                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
142             );
143
144             tb1.checkResult ();
145             tb2.checkResult ();
146             tb3.checkResult ();
147             tb4.checkResult ();
148             tb5.checkResult ();
149
150             dm.removeBreakpoint (lb1);
151             dm.removeBreakpoint (lb2);
152             dm.removeBreakpoint (lb3);
153             dm.removeBreakpoint (lb4);
154             dm.removeBreakpoint (lb5);
155             support.doContinue ();
156             support.waitState (JPDADebugger.STATE_DISCONNECTED);
157         } finally {
158             if (support != null) support.doFinish ();
159         }
160     }
161
162     public void testStaticBlockBreakpoint() throws Exception JavaDoc {
163         doTestBreakpointComplete(29);
164         doTestBreakpointComplete(32);
165     }
166
167     public void testStaticInnerClassBreakpoint() throws Exception JavaDoc {
168         doTestBreakpointComplete(75);
169         doTestBreakpointComplete(78);
170         doTestBreakpointComplete(92);
171     }
172
173     public void testMainLineBreakpoint() throws Exception JavaDoc {
174         doTestBreakpointComplete(36);
175     }
176
177     public void testConstructorLineBreakpoint() throws Exception JavaDoc {
178         doTestBreakpointComplete(51);
179     }
180
181     public void testInnerLineBreakpoint () throws Exception JavaDoc {
182         doTestBreakpointComplete (102);
183         doTestBreakpointComplete (105);
184         doTestBreakpointComplete (113);
185     }
186
187     private void doTestBreakpointComplete (
188         int line,
189         String JavaDoc condition,
190         int conditionResult
191     ) throws IOException JavaDoc, IllegalConnectorArgumentsException,
192     DebuggerStartException {
193         try {
194             LineBreakpoint lb = doTestBreakpoint (
195                 line,
196                 condition,
197                 conditionResult
198             );
199             if ( condition == null ||
200                  conditionResult == JPDABreakpointEvent.CONDITION_TRUE
201             ) {
202                 support.doContinue();
203                 support.waitState (JPDADebugger.STATE_DISCONNECTED);
204             }
205             DebuggerManager.getDebuggerManager ().removeBreakpoint (lb);
206         } finally {
207             if (support != null) support.doFinish();
208         }
209     }
210
211     private void doTestBreakpointComplete (int line) throws IOException JavaDoc,
212     IllegalConnectorArgumentsException, DebuggerStartException {
213         doTestBreakpointComplete (
214             line,
215             null,
216             JPDABreakpointEvent.CONDITION_NONE
217         );
218     }
219
220     private LineBreakpoint doTestBreakpoint (
221         int line,
222         String JavaDoc condition,
223         int conditionResult
224     ) throws IOException JavaDoc, IllegalConnectorArgumentsException,
225     DebuggerStartException {
226         JPDASupport.removeAllBreakpoints ();
227         LineBreakpoint lb = LineBreakpoint.create (TEST_APP, line);
228         if (73 <= line && line <= 98) {
229             lb.setPreferredClassName("org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp$InnerStatic");
230         } else if (100 <= line && line <= 115) {
231             lb.setPreferredClassName("org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp$Inner");
232         }
233         lb.setCondition (condition);
234         TestBreakpointListener tbl = new TestBreakpointListener
235             (lb, conditionResult);
236         lb.addJPDABreakpointListener (tbl);
237         DebuggerManager.getDebuggerManager ().addBreakpoint (lb);
238
239         support = JPDASupport.attach (
240             "org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp"
241         );
242
243         if ( condition == null ||
244              conditionResult == JPDABreakpointEvent.CONDITION_TRUE
245         ) {
246             support.waitState (JPDADebugger.STATE_STOPPED);
247         } else {
248             support.waitState (JPDADebugger.STATE_DISCONNECTED);
249         }
250
251         tbl.checkResult ();
252         return lb;
253     }
254
255     
256     /**
257      * Tests debugger's ability to make difference between different projects
258      * with the same classes while getting the locations during class-loaded event.
259      *
260      * 1. The user creates 2 classes: ${test.dir.src}/.../LineBreakpointApp.java
261      * and ${test.dir.src_2}/.../LineBreakpointApp.java
262      * 2. Then set a breakpoint in ${test.dir.src_2}/.../LineBreakpointApp.java.
263      *
264      * Debugger should stop _only_ in the second project. If debugger stopped in
265      * the first one, then assertion violation would arise because of source path
266      * equality test.
267      */

268     public void testBreakpointUnambiguity1 () throws Exception JavaDoc {
269         try {
270             LineBreakpoint lb1 = LineBreakpoint.create (TEST_APP, 39);
271 // lb1.setSourceRoot(System.getProperty ("test.dir.src"));
272
DebuggerManager dm = DebuggerManager.getDebuggerManager ();
273             dm.addBreakpoint (lb1);
274             
275             TestBreakpointListener tb1 = new TestBreakpointListener (lb1);
276             lb1.addJPDABreakpointListener (tb1);
277             
278             support = JPDASupport.attach (
279                 "org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp"
280             );
281             JPDADebugger debugger = support.getDebugger();
282
283             support.waitState (JPDADebugger.STATE_STOPPED); // breakpoint hit, the source root is correct
284
assertEquals (
285                 "Debugger stopped at wrong line",
286                 lb1.getLineNumber (),
287                 debugger.getCurrentCallStackFrame ().getLineNumber (null)
288             );
289
290             tb1.checkResult ();
291             support.doContinue();
292             support.waitState (JPDADebugger.STATE_DISCONNECTED);
293             dm.removeBreakpoint (lb1);
294             support.doFinish ();
295             /*
296             // Second run - BP should not be hit with a different source root - viz testBreakpointUnambiguity2()
297             support = null;
298             lb1 = LineBreakpoint.create (TEST_APP, 39);
299             lb1.setSourceRoot(System.getProperty ("test.dir.src")+"_2");
300             dm = DebuggerManager.getDebuggerManager ();
301             dm.addBreakpoint (lb1);
302             
303             tb1 = new TestBreakpointListener (lb1);
304             lb1.addJPDABreakpointListener (tb1);
305             
306             support = JPDASupport.attach (
307                 "org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp"
308             );
309             debugger = support.getDebugger();
310             
311             support.waitState (JPDADebugger.STATE_STOPPED); // Stopped or disconnected
312             assertEquals(
313                     "Debugger should not stop on BP with faked source root",
314                     debugger.getState(),
315                     JPDADebugger.STATE_DISCONNECTED
316             );
317             tb1.checkNotNotified();
318             dm.removeBreakpoint (lb1);
319              */

320         } finally {
321             if (support != null) support.doFinish ();
322         }
323     }
324
325     /**
326      * Tests debugger's ability to make difference between different projects
327      * with the same classes while getting the locations during class-loaded event.
328      *
329      * 1. The user creates 2 classes: ${test.dir.src}/.../LineBreakpointApp.java
330      * and ${test.dir.src_2}/.../LineBreakpointApp.java
331      * 2. Then set a breakpoint in ${test.dir.src_2}/.../LineBreakpointApp.java.
332      *
333      * Debugger should stop _only_ in the second project. If debugger stopped in
334      * the first one, then assertion violation would arise because of source path
335      * equality test.
336      */

337     public void testBreakpointUnambiguity2 () throws Exception JavaDoc {
338         try {
339             LineBreakpoint lb1 = LineBreakpoint.create(
340                     Utils.getURL(System.getProperty ("user.home") + // intentionally bad path
341
java.io.File.separator +
342                     "org/netbeans/api/debugger/jpda/testapps/LineBreakpointApp.java"), 39);
343             //lb1.setSourceRoot(System.getProperty ("test.dir.src") + "_2");
344
DebuggerManager dm = DebuggerManager.getDebuggerManager ();
345             dm.addBreakpoint (lb1);
346             
347             TestBreakpointListener tb1 = new TestBreakpointListener (lb1);
348             lb1.addJPDABreakpointListener (tb1);
349             
350             support = JPDASupport.attach (
351                 "org.netbeans.api.debugger.jpda.testapps.LineBreakpointApp"
352             );
353             JPDADebugger debugger = support.getDebugger();
354
355             support.waitState (JPDADebugger.STATE_STOPPED); // Stopped or disconnected
356
assertEquals(
357                     "Debugger should not stop on BP with faked source root",
358                     debugger.getState(),
359                     JPDADebugger.STATE_DISCONNECTED
360             );
361             
362             tb1.checkNotNotified();
363             dm.removeBreakpoint (lb1);
364         } finally {
365             if (support != null) support.doFinish ();
366         }
367     }
368
369     // innerclasses ............................................................
370

371     private class TestBreakpointListener implements JPDABreakpointListener {
372
373         private LineBreakpoint lineBreakpoint;
374         private int conditionResult;
375
376         private JPDABreakpointEvent event;
377         private AssertionError JavaDoc failure;
378
379         public TestBreakpointListener (LineBreakpoint lineBreakpoint) {
380             this (lineBreakpoint, JPDABreakpointEvent.CONDITION_NONE);
381         }
382
383         public TestBreakpointListener (
384             LineBreakpoint lineBreakpoint,
385             int conditionResult
386         ) {
387             this.lineBreakpoint = lineBreakpoint;
388             this.conditionResult = conditionResult;
389         }
390
391         public void breakpointReached (JPDABreakpointEvent event) {
392             try {
393                 checkEvent (event);
394             } catch (AssertionError JavaDoc e) {
395                 failure = e;
396             } catch (Throwable JavaDoc e) {
397                 failure = new AssertionError JavaDoc (e);
398             }
399         }
400
401         private void checkEvent (JPDABreakpointEvent event) {
402             this.event = event;
403             assertEquals (
404                 "Breakpoint event: Wrong source breakpoint",
405                 lineBreakpoint,
406                 event.getSource ()
407             );
408             assertNotNull (
409                 "Breakpoint event: Context thread is null",
410                 event.getThread ()
411             );
412
413             int result = event.getConditionResult ();
414             if ( result == JPDABreakpointEvent.CONDITION_FAILED &&
415                  conditionResult != JPDABreakpointEvent.CONDITION_FAILED
416             )
417                 failure = new AssertionError JavaDoc (event.getConditionException ());
418             else
419             if (result != conditionResult)
420                 failure = new AssertionError JavaDoc (
421                     "Unexpected breakpoint condition result: " + result
422                 );
423         }
424
425         public void checkResult () {
426             if (event == null) {
427                 CallStackFrame f = support.getDebugger ().
428                     getCurrentCallStackFrame ();
429                 int ln = -1;
430                 if (f != null) {
431                     ln = f.getLineNumber (null);
432                 }
433                 throw new AssertionError JavaDoc (
434                     "Breakpoint was not hit (listener was not notified) " + ln
435                 );
436             }
437             if (failure != null) throw failure;
438         }
439         
440         public void checkNotNotified() {
441             if (event != null) {
442                 JPDAThread t = event.getThread();
443                 throw new AssertionError JavaDoc (
444                     "Breakpoint was hit (listener was notified) in thread " + t
445                 );
446             }
447             if (failure != null) throw failure;
448         }
449     }
450 }
451
Popular Tags