1 19 20 package org.netbeans.api.debugger.jpda; 21 22 import java.net.URL ; 23 import java.io.IOException ; 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 36 public class LineBreakpointTest extends NbTestCase { 37 38 private static final String 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 s) { 46 super (s); 47 } 48 49 public void testConditionalBreakpoint() throws Exception { 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 { 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); 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); 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); 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); 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); 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 { 163 doTestBreakpointComplete(29); 164 doTestBreakpointComplete(32); 165 } 166 167 public void testStaticInnerClassBreakpoint() throws Exception { 168 doTestBreakpointComplete(75); 169 doTestBreakpointComplete(78); 170 doTestBreakpointComplete(92); 171 } 172 173 public void testMainLineBreakpoint() throws Exception { 174 doTestBreakpointComplete(36); 175 } 176 177 public void testConstructorLineBreakpoint() throws Exception { 178 doTestBreakpointComplete(51); 179 } 180 181 public void testInnerLineBreakpoint () throws Exception { 182 doTestBreakpointComplete (102); 183 doTestBreakpointComplete (105); 184 doTestBreakpointComplete (113); 185 } 186 187 private void doTestBreakpointComplete ( 188 int line, 189 String condition, 190 int conditionResult 191 ) throws IOException , 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 , 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 condition, 223 int conditionResult 224 ) throws IOException , 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 268 public void testBreakpointUnambiguity1 () throws Exception { 269 try { 270 LineBreakpoint lb1 = LineBreakpoint.create (TEST_APP, 39); 271 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); 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 320 } finally { 321 if (support != null) support.doFinish (); 322 } 323 } 324 325 337 public void testBreakpointUnambiguity2 () throws Exception { 338 try { 339 LineBreakpoint lb1 = LineBreakpoint.create( 340 Utils.getURL(System.getProperty ("user.home") + java.io.File.separator + 342 "org/netbeans/api/debugger/jpda/testapps/LineBreakpointApp.java"), 39); 343 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); 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 371 private class TestBreakpointListener implements JPDABreakpointListener { 372 373 private LineBreakpoint lineBreakpoint; 374 private int conditionResult; 375 376 private JPDABreakpointEvent event; 377 private AssertionError 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 e) { 395 failure = e; 396 } catch (Throwable e) { 397 failure = new AssertionError (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 (event.getConditionException ()); 418 else 419 if (result != conditionResult) 420 failure = new AssertionError ( 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 ( 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 ( 444 "Breakpoint was hit (listener was notified) in thread " + t 445 ); 446 } 447 if (failure != null) throw failure; 448 } 449 } 450 } 451 | Popular Tags |