KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.api.debugger.DebuggerManager;
23 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
24 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
25 import org.netbeans.junit.NbTestCase;
26
27
28 /**
29  * Tests method breakpoints.
30  *
31  * @author Maros Sandor, Jan Jancura
32  */

33 public class MethodBreakpointTest extends NbTestCase {
34
35     private JPDASupport support;
36     private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
37
38     private static final String JavaDoc CLASS_NAME =
39         "org.netbeans.api.debugger.jpda.testapps.MethodBreakpointApp";
40
41     public MethodBreakpointTest (String JavaDoc s) {
42         super (s);
43     }
44
45     public void testMethodEntryBreakpoints() throws Exception JavaDoc {
46         try {
47             MethodBreakpoint mb1 = MethodBreakpoint.create (CLASS_NAME, "a");
48             TestBreakpointListener tbl = new TestBreakpointListener
49                 ("a", 46, 1);
50             mb1.addJPDABreakpointListener (tbl);
51             dm.addBreakpoint(mb1);
52
53             MethodBreakpoint mb2 = MethodBreakpoint.create (CLASS_NAME, "b");
54             TestBreakpointListener tb2 = new TestBreakpointListener
55                 ("b", 51, 2);
56             mb2.addJPDABreakpointListener (tb2);
57             dm.addBreakpoint (mb2);
58
59             MethodBreakpoint mb3 = MethodBreakpoint.create (CLASS_NAME, "c");
60             TestBreakpointListener tb3 = new TestBreakpointListener
61                 ("c", 55, 4);
62             mb3.addJPDABreakpointListener (tb3);
63             dm.addBreakpoint (mb3);
64
65             MethodBreakpoint mb4 = MethodBreakpoint.create
66                 (CLASS_NAME, "<init>");
67             TestBreakpointListener tb4 = new TestBreakpointListener
68                 ("<init>", 41, 1);
69             mb4.addJPDABreakpointListener (tb4);
70             dm.addBreakpoint (mb4);
71
72             MethodBreakpoint mb5 = MethodBreakpoint.create
73                 (CLASS_NAME, "<clinit>");
74             TestBreakpointListener tb5 = new TestBreakpointListener
75                 ("<clinit>", 38, 1);
76             mb5.addJPDABreakpointListener (tb5);
77             dm.addBreakpoint (mb5);
78
79             MethodBreakpoint mb6 = MethodBreakpoint.create (
80                 CLASS_NAME + "$InnerStatic",
81                 "<clinit>"
82             );
83             TestBreakpointListener tb6 = new TestBreakpointListener (
84                 "InnerStatic.<clinit>",
85                 60,
86                 1
87             );
88             mb6.addJPDABreakpointListener (tb6);
89             dm.addBreakpoint (mb6);
90
91             MethodBreakpoint mb7 = MethodBreakpoint.create (
92                 CLASS_NAME + "$InnerStatic",
93                 "getW"
94             );
95             TestBreakpointListener tb7 = new TestBreakpointListener (
96                 "InnerStatic.getW",
97                 81,
98                 1
99             );
100             mb7.addJPDABreakpointListener (tb7);
101             dm.addBreakpoint (mb7);
102
103             MethodBreakpoint mb8 = MethodBreakpoint.create (
104                 CLASS_NAME + "$Inner",
105                 "<init>"
106             );
107             TestBreakpointListener tb8 = new TestBreakpointListener (
108                 "Inner.<init>",
109                 93,
110                 4
111             );
112             mb8.addJPDABreakpointListener (tb8);
113             dm.addBreakpoint (mb8);
114
115             MethodBreakpoint mb9 = MethodBreakpoint.create (
116                 CLASS_NAME + "$Inner",
117                 "getW"
118             );
119             TestBreakpointListener tb9 = new TestBreakpointListener (
120                 "Inner.getW",
121                 98,
122                 8
123             );
124             mb9.addJPDABreakpointListener (tb9);
125             dm.addBreakpoint (mb9);
126
127             support = JPDASupport.attach (CLASS_NAME);
128
129             for (;;) {
130                 support.waitState (JPDADebugger.STATE_STOPPED);
131                 if (support.getDebugger ().getState () ==
132                     JPDADebugger.STATE_DISCONNECTED
133                 ) break;
134                 support.doContinue ();
135             }
136             tbl.assertFailure ();
137             tb2.assertFailure ();
138             tb3.assertFailure ();
139             tb4.assertFailure ();
140             tb5.assertFailure ();
141             tb6.assertFailure ();
142             tb7.assertFailure ();
143             tb8.assertFailure ();
144             tb9.assertFailure ();
145
146             dm.removeBreakpoint (mb1);
147             dm.removeBreakpoint (mb2);
148             dm.removeBreakpoint (mb3);
149             dm.removeBreakpoint (mb4);
150             dm.removeBreakpoint (mb5);
151             dm.removeBreakpoint (mb6);
152             dm.removeBreakpoint (mb7);
153             dm.removeBreakpoint (mb8);
154             dm.removeBreakpoint (mb9);
155         } finally {
156             support.doFinish();
157         }
158     }
159
160     public void testMethodExitBreakpoints() throws Exception JavaDoc {
161         try {
162             MethodBreakpoint mb1 = MethodBreakpoint.create (
163                 CLASS_NAME + "$AbstractInner", "compute"
164             );
165             mb1.setBreakpointType(MethodBreakpoint.TYPE_METHOD_EXIT);
166             TestBreakpointListener tbl = new TestBreakpointListener
167                 ("compute", 118, 1, "1.0");
168             mb1.addJPDABreakpointListener (tbl);
169             dm.addBreakpoint(mb1);
170             
171             MethodBreakpoint mb2 = MethodBreakpoint.create (
172                 CLASS_NAME + "$InterfaceInner", "getString"
173             );
174             mb2.setBreakpointType(MethodBreakpoint.TYPE_METHOD_EXIT);
175             TestBreakpointListener tb2 = new TestBreakpointListener
176                 ("getString", 123, 1, "\"Hello\"");
177             mb2.addJPDABreakpointListener (tb2);
178             dm.addBreakpoint(mb2);
179             
180             support = JPDASupport.attach (CLASS_NAME);
181
182             for (;;) {
183                 support.waitState (JPDADebugger.STATE_STOPPED);
184                 if (support.getDebugger ().getState () ==
185                     JPDADebugger.STATE_DISCONNECTED
186                 ) break;
187                 support.doContinue ();
188             }
189             tbl.assertFailure ();
190             tb2.assertFailure ();
191
192             dm.removeBreakpoint (mb1);
193             dm.removeBreakpoint (mb2);
194         } finally {
195             support.doFinish();
196         }
197     }
198
199     private class TestBreakpointListener implements JPDABreakpointListener {
200
201         private int hitCount;
202         private AssertionError JavaDoc failure;
203         private String JavaDoc methodName;
204         private int hitLine;
205         private int expectedHitCount;
206         private String JavaDoc returnValue;
207
208         public TestBreakpointListener (
209             String JavaDoc methodName,
210             int hitLine,
211             int expectedHitCount
212         ) {
213             this.methodName = methodName;
214             this.hitLine = hitLine;
215             this.expectedHitCount = expectedHitCount;
216         }
217
218         public TestBreakpointListener (
219             String JavaDoc methodName,
220             int hitLine,
221             int expectedHitCount,
222             String JavaDoc returnValue
223         ) {
224             this(methodName, hitLine, expectedHitCount);
225             this.returnValue = returnValue;
226         }
227         
228         public void breakpointReached(JPDABreakpointEvent event) {
229             try {
230                 checkEvent(event);
231             } catch (AssertionError JavaDoc e) {
232                 failure = e;
233             } catch (Throwable JavaDoc e) {
234                 failure = new AssertionError JavaDoc(e);
235             }
236         }
237
238         private void checkEvent (JPDABreakpointEvent event) {
239             MethodBreakpoint mb = (MethodBreakpoint) event.getSource ();
240
241             assertEquals (
242                 "Breakpoint event: Condition evaluation failed",
243                 JPDABreakpointEvent.CONDITION_NONE,
244                 event.getConditionResult ()
245             );
246             assertNotNull (
247                 "Breakpoint event: Context thread is null",
248                 event.getThread ()
249             );
250             assertEquals (
251                 "Breakpoint event: Hit at wrong place",
252                 hitLine,
253                 event.getThread ().getLineNumber (null)
254             );
255             assertEquals (
256                 "Breakpoint event: Hit at wrong method",
257                 mb.getMethodName (),
258                 event.getThread ().getMethodName ()
259             );
260             
261             if (returnValue != null && !System.getProperty("java.version").startsWith("1.5")) {
262                 Variable retVar = event.getVariable();
263                 assertNotNull(
264                         "Breakpoint event: The return value must not be null!",
265                         retVar);
266                 ReturnVariable returnVariable = (ReturnVariable) retVar;
267                 assertEquals(
268                         "Breakpoint event: Wrong method name hit",
269                         methodName, returnVariable.methodName());
270                 assertEquals(
271                         "Breakpoint event: Wrong return value",
272                         returnValue, returnVariable.getValue());
273             }
274
275             hitCount++;
276         }
277
278         public void assertFailure () {
279             if (failure != null)
280                 throw failure;
281             assertEquals (
282                 "Breakpoint hit count mismatch for: " + methodName,
283                 expectedHitCount,
284                 hitCount
285             );
286         }
287     }
288 }
289
Popular Tags