KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class FieldBreakpointTest extends NbTestCase {
35
36     private JPDASupport support;
37     private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
38
39     private static final String JavaDoc CLASS_NAME =
40             "org.netbeans.api.debugger.jpda.testapps.FieldBreakpointApp";
41
42     public FieldBreakpointTest (String JavaDoc s) {
43         super (s);
44     }
45
46     public void testFieldBreakpoints() throws Exception JavaDoc {
47         try {
48 // Does not work on JDK1.4
49
// FieldBreakpoint fb1 = FieldBreakpoint.create (
50
// CLASS_NAME,
51
// "x",
52
// FieldBreakpoint.TYPE_MODIFICATION
53
// );
54
// TestBreakpointListener tbl = new TestBreakpointListener (
55
// "x",
56
// 0,
57
// new int [] {29, 32, 37 }
58
// );
59
// fb1.addJPDABreakpointListener (tbl);
60
// dm.addBreakpoint (fb1);
61

62             FieldBreakpoint fb2 = FieldBreakpoint.create (
63                 CLASS_NAME,
64                 "y",
65                 FieldBreakpoint.TYPE_MODIFICATION
66             );
67             TestBreakpointListener tb2 = new TestBreakpointListener (
68                 "y",
69                 0,
70                 new int [] { 44, 47, 51 }
71             );
72             fb2.addJPDABreakpointListener (tb2);
73             dm.addBreakpoint (fb2);
74
75 // Does not work on JDK1.4
76
// FieldBreakpoint fb3 = FieldBreakpoint.create (
77
// CLASS_NAME + "$InnerStatic",
78
// "q",
79
// FieldBreakpoint.TYPE_MODIFICATION
80
// );
81
// TestBreakpointListener tb3 = new TestBreakpointListener (
82
// "InnerStatic.q",
83
// 0,
84
// new int [] {75, 78 }
85
// );
86
// fb3.addJPDABreakpointListener (tb3);
87
// dm.addBreakpoint (fb3);
88

89             FieldBreakpoint fb4 = FieldBreakpoint.create (
90                 CLASS_NAME + "$InnerStatic",
91                 "w",
92                 FieldBreakpoint.TYPE_MODIFICATION
93             );
94             TestBreakpointListener tb4 = new TestBreakpointListener (
95                 "InnerStatic.w",
96                 0,
97                 new int [] { 81, 84, 88 }
98             );
99             fb4.addJPDABreakpointListener (tb4);
100             dm.addBreakpoint (fb4);
101
102             FieldBreakpoint fb5 = FieldBreakpoint.create (
103                 CLASS_NAME + "$Inner",
104                 "w",
105                 FieldBreakpoint.TYPE_MODIFICATION
106             );
107             TestBreakpointListener tb5 = new TestBreakpointListener (
108                 "Inner.w",
109                 0,
110                 new int [] { 102, 105, 109 }
111             );
112             fb5.addJPDABreakpointListener (tb5);
113             dm.addBreakpoint (fb5);
114
115             support = JPDASupport.attach (CLASS_NAME);
116             for (;;) {
117                 support.waitState (JPDADebugger.STATE_STOPPED);
118                 if ( support.getDebugger ().getState () ==
119                      JPDADebugger.STATE_DISCONNECTED
120                 )
121                     break;
122                 support.doContinue ();
123             }
124             
125 // tbl.assertFailure ();
126
tb2.assertFailure ();
127 // tb3.assertFailure ();
128
tb4.assertFailure ();
129             tb5.assertFailure ();
130
131 // dm.removeBreakpoint (fb1);
132
dm.removeBreakpoint (fb2);
133 // dm.removeBreakpoint (fb3);
134
dm.removeBreakpoint (fb4);
135             dm.removeBreakpoint (fb5);
136         } finally {
137             support.doFinish ();
138         }
139     }
140
141     private class TestBreakpointListener implements JPDABreakpointListener {
142
143         private int hitCount;
144         private int currentFieldValue;
145         private AssertionError JavaDoc failure;
146         private String JavaDoc variableName;
147         private int[] hitLines;
148
149         public TestBreakpointListener (
150                 String JavaDoc variableName,
151                 int initialValue,
152                 int [] hitLines
153                 ) {
154             this.variableName = variableName;
155             this.hitLines = hitLines;
156             currentFieldValue = initialValue;
157         }
158
159         public void breakpointReached(JPDABreakpointEvent event) {
160             try {
161                 checkEvent(event);
162             } catch (AssertionError JavaDoc e) {
163                 failure = e;
164             } catch (Throwable JavaDoc e) {
165                 failure = new AssertionError JavaDoc(e);
166             }
167         }
168
169         private void checkEvent (JPDABreakpointEvent event)
170         throws AbsentInformationException {
171             
172             FieldBreakpoint fb = (FieldBreakpoint) event.getSource ();
173
174             System.out.println (
175                 variableName + " : " +
176                 event.getThread ().getCallStack () [0].getLineNumber (null) +
177                 " : " + event.getVariable ().getValue ());
178             
179             if (hitCount >= hitLines.length)
180                 throw new AssertionError JavaDoc (
181                     "Breakpoint hit too many times for " + variableName +
182                     ": " + hitCount + " at " +
183                     event.getThread ().getCallStack () [0].getLineNumber (null)
184                 );
185             int hitLine = hitLines [hitCount++];
186             assertEquals (
187                 "Breakpoint event: Condition evaluation failed",
188                 JPDABreakpointEvent.CONDITION_NONE,
189                 event.getConditionResult ()
190             );
191             assertNotNull (
192                 "Breakpoint event: Context thread is null",
193                 event.getThread ()
194             );
195             assertEquals (
196                 "Breakpoint event: Hit at wrong place",
197                 hitLine,
198                 event.getThread ().getCallStack () [0].getLineNumber (null)
199             );
200             Variable var = event.getVariable ();
201             assertNotNull (
202                 "Breakpoint event: No variable information",
203                 var
204             );
205
206             if (fb.getBreakpointType () == FieldBreakpoint.TYPE_ACCESS) {
207                 assertEquals (
208                     "Breakpoint event: Wrong field value",
209                     Integer.toString (currentFieldValue),
210                     var.getValue ()
211                 );
212             } else {
213                 currentFieldValue ++;
214                 assertEquals (
215                     "Breakpoint event: Wrong field value of " +
216                     fb.getFieldName () + " at " +
217                     event.getThread ().getCallStack () [0].getLineNumber (null),
218                     Integer.toString (currentFieldValue),
219                     var.getValue ()
220                 );
221             }
222         }
223
224         public void assertFailure () {
225             if (failure != null) throw failure;
226             assertEquals (
227                 "Breakpoint hit count mismatch for: " + variableName,
228                 hitLines.length,
229                 hitCount
230             );
231         }
232     }
233 }
234
Popular Tags