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 28 33 public class MethodBreakpointTest extends NbTestCase { 34 35 private JPDASupport support; 36 private DebuggerManager dm = DebuggerManager.getDebuggerManager (); 37 38 private static final String CLASS_NAME = 39 "org.netbeans.api.debugger.jpda.testapps.MethodBreakpointApp"; 40 41 public MethodBreakpointTest (String s) { 42 super (s); 43 } 44 45 public void testMethodEntryBreakpoints() throws Exception { 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 { 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 failure; 203 private String methodName; 204 private int hitLine; 205 private int expectedHitCount; 206 private String returnValue; 207 208 public TestBreakpointListener ( 209 String 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 methodName, 220 int hitLine, 221 int expectedHitCount, 222 String 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 e) { 232 failure = e; 233 } catch (Throwable e) { 234 failure = new AssertionError (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 |