KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class ThreadBreakpointTest 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.ThreadBreakpointApp";
39
40     
41     public ThreadBreakpointTest (String JavaDoc s) {
42         super (s);
43     }
44
45     public void testMethodBreakpoints () throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc e) {
84                 failure = e;
85             } catch (Throwable JavaDoc e) {
86                 failure = new AssertionError JavaDoc (e);
87             }
88         }
89
90         private void checkEvent (JPDABreakpointEvent event) {
91 // ThreadBreakpoint tb = (ThreadBreakpoint) event.getSource();
92
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 JavaDoc
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