1 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 34 public class FieldBreakpointTest extends NbTestCase { 35 36 private JPDASupport support; 37 private DebuggerManager dm = DebuggerManager.getDebuggerManager (); 38 39 private static final String CLASS_NAME = 40 "org.netbeans.api.debugger.jpda.testapps.FieldBreakpointApp"; 41 42 public FieldBreakpointTest (String s) { 43 super (s); 44 } 45 46 public void testFieldBreakpoints() throws Exception { 47 try { 48 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 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 tb2.assertFailure (); 127 tb4.assertFailure (); 129 tb5.assertFailure (); 130 131 dm.removeBreakpoint (fb2); 133 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 failure; 146 private String variableName; 147 private int[] hitLines; 148 149 public TestBreakpointListener ( 150 String 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 e) { 163 failure = e; 164 } catch (Throwable e) { 165 failure = new AssertionError (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 ( 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 |