KickJava   Java API By Example, From Geeks To Geeks.

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


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 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  * Tests exception breakpoints.
29  *
30  * @author Maros Sandor, Jan Jancura
31  */

32 public class ExceptionBreakpointTest extends NbTestCase {
33
34     private JPDASupport support;
35     private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
36
37     private static final String JavaDoc CLASS_NAME =
38         "org.netbeans.api.debugger.jpda.testapps.ExceptionBreakpointApp";
39
40     
41     public ExceptionBreakpointTest (String JavaDoc s) {
42         super (s);
43     }
44
45     public void testMethodBreakpoints () throws Exception JavaDoc {
46         try {
47             ExceptionBreakpoint eb1 = ExceptionBreakpoint.create (
48                 "org.netbeans.api.debugger.jpda.testapps.ExceptionTestException",
49                 ExceptionBreakpoint.TYPE_EXCEPTION_CATCHED
50             );
51             TestBreakpointListener tbl = new TestBreakpointListener (
52                 "org.netbeans.api.debugger.jpda.testapps.ExceptionTestException",
53                 eb1,
54                 1
55             );
56             eb1.addJPDABreakpointListener (tbl);
57             dm.addBreakpoint (eb1);
58
59             support = JPDASupport.attach (CLASS_NAME);
60
61             for (;;) {
62                 support.waitState (JPDADebugger.STATE_STOPPED);
63                 if (support.getDebugger ().getState () ==
64                       JPDADebugger.STATE_DISCONNECTED
65                 ) break;
66                 support.doContinue ();
67             }
68             tbl.assertFailure ();
69
70             dm.removeBreakpoint (eb1);
71         } finally {
72             support.doFinish ();
73         }
74     }
75
76     private class TestBreakpointListener implements JPDABreakpointListener {
77
78         private int hitCount;
79         private AssertionError JavaDoc failure;
80         private String JavaDoc exceptionClass;
81         private ExceptionBreakpoint bpt;
82         private int expectedHitCount;
83
84         public TestBreakpointListener (
85             String JavaDoc exceptionClass,
86             ExceptionBreakpoint bpt,
87             int expectedHitCount
88         ) {
89             this.exceptionClass = exceptionClass;
90             this.bpt = bpt;
91             this.expectedHitCount = expectedHitCount;
92         }
93
94         public void breakpointReached (JPDABreakpointEvent event) {
95             try {
96                 checkEvent (event);
97             } catch (AssertionError JavaDoc e) {
98                 failure = e;
99             } catch (Throwable JavaDoc e) {
100                 failure = new AssertionError JavaDoc (e);
101             }
102         }
103
104         private void checkEvent (JPDABreakpointEvent event) {
105             assertEquals (
106                 "Breakpoint event: Bad exception location",
107                 CLASS_NAME,
108                 event.getReferenceType ().name ()
109             );
110             assertEquals (
111                 "Breakpoint event: Bad exception thrown",
112                 exceptionClass,
113                 event.getVariable ().getType ()
114             );
115             assertSame (
116                 "Breakpoint event: Bad event source",
117                 bpt,
118                 event.getSource ()
119             );
120             assertEquals (
121                 "Breakpoint event: Condition evaluation failed",
122                 JPDABreakpointEvent.CONDITION_NONE,
123                 event.getConditionResult ()
124             );
125             assertNotNull (
126                 "Breakpoint event: Context thread is null",
127                 event.getThread ()
128             );
129             hitCount++;
130         }
131
132         public void assertFailure () {
133             if (failure != null) throw failure;
134             assertEquals (
135                 "Breakpoint hit count mismatch",
136                 expectedHitCount,
137                 hitCount
138             );
139         }
140     }
141 }
142
Popular Tags