1 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 32 public class ThreadBreakpointTest extends NbTestCase { 33 34 private JPDASupport support; 35 private DebuggerManager dm = DebuggerManager.getDebuggerManager (); 36 37 private static final String CLASS_NAME = 38 "org.netbeans.api.debugger.jpda.testapps.ThreadBreakpointApp"; 39 40 41 public ThreadBreakpointTest (String s) { 42 super (s); 43 } 44 45 public void testMethodBreakpoints () throws Exception { 46 try { 47 ThreadBreakpoint tb1 = ThreadBreakpoint.create (); 48 tb1.setBreakpointType (ThreadBreakpoint.TYPE_THREAD_STARTED_OR_DEATH); 49 TestBreakpointListener tbl = new TestBreakpointListener (10); 50 tb1.addJPDABreakpointListener (tbl); 51 dm.addBreakpoint (tb1); 52 53 support = JPDASupport.attach (CLASS_NAME); 54 55 for (;;) { 56 support.waitState (JPDADebugger.STATE_STOPPED); 57 if (support.getDebugger ().getState () == 58 JPDADebugger.STATE_DISCONNECTED 59 ) break; 60 support.doContinue (); 61 } 62 tbl.assertFailure (); 63 64 dm.removeBreakpoint (tb1); 65 } finally { 66 support.doFinish(); 67 } 68 } 69 70 private class TestBreakpointListener implements JPDABreakpointListener { 71 72 private int hitCount; 73 private AssertionError failure; 74 private int expectedHitCount; 75 76 public TestBreakpointListener (int expectedHitCount) { 77 this.expectedHitCount = expectedHitCount; 78 } 79 80 public void breakpointReached (JPDABreakpointEvent event) { 81 try { 82 checkEvent (event); 83 } catch (AssertionError e) { 84 failure = e; 85 } catch (Throwable e) { 86 failure = new AssertionError (e); 87 } 88 } 89 90 private void checkEvent (JPDABreakpointEvent event) { 91 assertEquals ( 93 "Breakpoint event: Condition evaluation failed", 94 JPDABreakpointEvent.CONDITION_NONE, 95 event.getConditionResult () 96 ); 97 assertNotNull ( 98 "Breakpoint event: Context thread is null", 99 event.getThread () 100 ); 101 JPDAThread thread = event.getThread (); 102 if (thread.getName ().startsWith ("test-")) { 103 JPDAThreadGroup group = thread.getParentThreadGroup (); 104 assertEquals ( 105 "Wrong thread group", 106 "testgroup", 107 group.getName () 108 ); 109 assertEquals ( 110 "Wrong parent thread group", 111 "main", 112 group.getParentThreadGroup ().getName () 113 ); 114 assertEquals ( 115 "Wrong number of child thread groups", 116 0, 117 group.getThreadGroups ().length 118 ); 119 JPDAThread [] threads = group.getThreads (); 120 for (int i = 0; i < threads.length; i++) { 121 JPDAThread jpdaThread = threads [i]; 122 if ( !jpdaThread.getName ().startsWith ("test-")) 123 throw new AssertionError 124 ("Thread group contains an alien thread"); 125 assertSame ( 126 "Child/parent mismatch", 127 jpdaThread.getParentThreadGroup (), 128 group 129 ); 130 } 131 hitCount++; 132 } 133 } 134 135 public void assertFailure () { 136 if (failure != null) throw failure; 137 assertEquals ( 138 "Breakpoint hit count mismatch", 139 expectedHitCount, 140 hitCount 141 ); 142 } 143 } 144 } 145 | Popular Tags |