KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public class ClassBreakpointTest extends NbTestCase {
34
35     private JPDASupport support;
36     private JPDADebugger debugger;
37     private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
38
39     private static final String JavaDoc CLASS_NAME = "org.netbeans.api.debugger.jpda.testapps.ClassBreakpointApp";
40
41     
42     public ClassBreakpointTest (String JavaDoc s) {
43         super (s);
44     }
45
46     public void testMethodBreakpoints() throws Exception JavaDoc {
47         try {
48             ClassLoadUnloadBreakpoint cb1 = ClassLoadUnloadBreakpoint.create("org.netbeans.api.debugger.jpda.testapps.ClassBreakpointTest1", false, ClassLoadUnloadBreakpoint.TYPE_CLASS_LOADED);
49             TestBreakpointListener tbl = new TestBreakpointListener("org.netbeans.api.debugger.jpda.testapps.ClassBreakpointTest1", 1);
50             cb1.addJPDABreakpointListener(tbl);
51             dm.addBreakpoint(cb1);
52
53             ClassLoadUnloadBreakpoint cb2 = ClassLoadUnloadBreakpoint.create("*", false, ClassLoadUnloadBreakpoint.TYPE_CLASS_LOADED);
54             TestBreakpointListener tb2 = new TestBreakpointListener(null, -1);
55             cb2.addJPDABreakpointListener(tb2);
56             dm.addBreakpoint(cb2);
57
58             support = JPDASupport.attach (CLASS_NAME);
59             debugger = support.getDebugger();
60
61             for (;;) {
62                 support.waitState (JPDADebugger.STATE_STOPPED);
63                 if (debugger.getState() == JPDADebugger.STATE_DISCONNECTED) break;
64                 support.doContinue();
65             }
66             tbl.assertFailure();
67             tb2.assertFailure();
68
69             dm.removeBreakpoint(cb1);
70             dm.removeBreakpoint(cb2);
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 className;
81         private int expectedHitCount;
82
83         public TestBreakpointListener(String JavaDoc className, int expectedHitCount) {
84             this.className = className;
85             this.expectedHitCount = expectedHitCount;
86         }
87
88         public void breakpointReached(JPDABreakpointEvent event) {
89             try {
90                 checkEvent(event);
91             } catch (AssertionError JavaDoc e) {
92                 failure = e;
93             } catch (Throwable JavaDoc e) {
94                 failure = new AssertionError JavaDoc(e);
95             }
96         }
97
98         private void checkEvent(JPDABreakpointEvent event) {
99             ClassLoadUnloadBreakpoint cb = (ClassLoadUnloadBreakpoint) event.getSource();
100
101             assertEquals("Breakpoint event: Condition evaluation failed", JPDABreakpointEvent.CONDITION_NONE, event.getConditionResult());
102             assertNotNull("Breakpoint event: Context thread is null", event.getThread());
103             assertNotNull("Breakpoint event: Reference type is null", event.getReferenceType());
104             if (className != null)
105             {
106                 assertEquals("Breakpoint event: Hit at wrong class", className, event.getReferenceType().name());
107             }
108
109             hitCount++;
110         }
111
112         public void assertFailure() {
113             if (failure != null) throw failure;
114             if (expectedHitCount != -1)
115             {
116                 assertEquals("Breakpoint hit count mismatch for: " + className, expectedHitCount, hitCount);
117             }
118         }
119     }
120 }
121
Popular Tags