Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.debugger.jpda; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.Arrays; |
46 |
import java.util.HashSet; |
47 |
import java.util.List; |
48 |
import java.util.Set; |
49 |
import junit.framework.Test; |
50 |
import org.netbeans.api.debugger.DebuggerManager; |
51 |
import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent; |
52 |
import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener; |
53 |
import org.netbeans.junit.NbTestCase; |
54 |
import org.netbeans.spi.debugger.jpda.BreakpointsClassFilter; |
55 |
|
56 |
/** |
57 |
* Test of {@link BreakpointsClassFilter}. |
58 |
* |
59 |
* @author Martin Entlicher |
60 |
*/ |
61 |
public class BreakpointsClassFilterTest extends NbTestCase { |
62 |
|
63 |
private static final String TEST_APP_PATH = System.getProperty ("test.dir.src") + |
64 |
"org/netbeans/api/debugger/jpda/testapps/BreakpointsClassFilterApp.java"; |
65 |
private static final String FILTER_GROUP_NAME = "filtered"; |
66 |
private static final String APP_CLASS_NAME = "org.netbeans.api.debugger.jpda.testapps.BreakpointsClassFilterApp"; |
67 |
private static final String APP2_CLASS_NAME = APP_CLASS_NAME + "2"; |
68 |
|
69 |
private JPDASupport support; |
70 |
|
71 |
/* |
72 |
public static Test suite() { |
73 |
return JPDASupport.createTestSuite(BreakpointsClassFilterTest.class); |
74 |
} |
75 |
*/ |
76 |
|
77 |
public BreakpointsClassFilterTest(String s) { |
78 |
super(s); |
79 |
} |
80 |
|
81 |
/** |
82 |
* Two breakpoints of each kind are submitted, |
83 |
* the first is hit in BreakpointsClassFilterApp class only, |
84 |
* but the second is hit also in BreakpointsClassFilterApp2 class. |
85 |
* The latter has {@link #FILTER_GROUP_NAME} set as a group name to |
86 |
* distinguish the filtered breakpoints. |
87 |
* Hits in the two application classes are checked by TestBreakpointListener. |
88 |
* |
89 |
* @throws Exception |
90 |
*/ |
91 |
public void testFilteredBreakpoints() throws Exception { |
92 |
JPDASupport.removeAllBreakpoints (); |
93 |
List<JPDABreakpoint> breakpoints = new ArrayList<JPDABreakpoint>(); |
94 |
Utils.BreakPositions bp = Utils.getBreakPositions(TEST_APP_PATH); |
95 |
List<LineBreakpoint> lineBreakpoints = bp.getLineBreakpoints(); |
96 |
lineBreakpoints.get(1).setGroupName(FILTER_GROUP_NAME); |
97 |
|
98 |
MethodBreakpoint mb1 = MethodBreakpoint.create(APP_CLASS_NAME, "test"); |
99 |
mb1.setBreakpointType(MethodBreakpoint.TYPE_METHOD_ENTRY); |
100 |
|
101 |
FieldBreakpoint fb1 = FieldBreakpoint.create(APP_CLASS_NAME, "field", FieldBreakpoint.TYPE_ACCESS); |
102 |
FieldBreakpoint fb2 = FieldBreakpoint.create(APP_CLASS_NAME, "field2", FieldBreakpoint.TYPE_ACCESS); |
103 |
fb2.setGroupName(FILTER_GROUP_NAME); |
104 |
|
105 |
ExceptionBreakpoint eb1 = ExceptionBreakpoint.create(NegativeArraySizeException.class.getName(), ExceptionBreakpoint.TYPE_EXCEPTION_CATCHED_UNCATCHED); |
106 |
ExceptionBreakpoint eb2 = ExceptionBreakpoint.create(ArithmeticException.class.getName(), ExceptionBreakpoint.TYPE_EXCEPTION_CATCHED_UNCATCHED); |
107 |
eb2.setGroupName(FILTER_GROUP_NAME); |
108 |
|
109 |
MethodBreakpoint mb2 = MethodBreakpoint.create(APP_CLASS_NAME, "test"); |
110 |
mb2.setBreakpointType(MethodBreakpoint.TYPE_METHOD_EXIT); |
111 |
mb2.setGroupName(FILTER_GROUP_NAME); |
112 |
|
113 |
breakpoints.add(lineBreakpoints.get(0)); |
114 |
breakpoints.add(mb1); |
115 |
breakpoints.add(fb1); |
116 |
breakpoints.add(eb1); |
117 |
breakpoints.add(lineBreakpoints.get(1)); |
118 |
breakpoints.add(mb2); |
119 |
breakpoints.add(fb2); |
120 |
breakpoints.add(eb2); |
121 |
|
122 |
List<TestBreakpointListener> listeners = new ArrayList<TestBreakpointListener>(); |
123 |
for (JPDABreakpoint b : breakpoints) { |
124 |
TestBreakpointListener tbl = new TestBreakpointListener |
125 |
(b, FILTER_GROUP_NAME.equals(b.getGroupName()), breakpoints); |
126 |
b.addJPDABreakpointListener(tbl); |
127 |
DebuggerManager.getDebuggerManager ().addBreakpoint (b); |
128 |
listeners.add(tbl); |
129 |
} |
130 |
|
131 |
support = JPDASupport.attach ( |
132 |
APP_CLASS_NAME |
133 |
); |
134 |
support.waitState(JPDADebugger.STATE_DISCONNECTED); |
135 |
assertTrue(Arrays.toString(breakpoints.toArray()), breakpoints.isEmpty()); |
136 |
for (TestBreakpointListener l : listeners) { |
137 |
assertTrue(l.getMessage(), l.isOK()); |
138 |
} |
139 |
} |
140 |
|
141 |
private class TestBreakpointListener implements JPDABreakpointListener { |
142 |
|
143 |
private final JPDABreakpoint breakpoint; |
144 |
private final boolean isFiltered; |
145 |
private final List<JPDABreakpoint> allBreakpoints; |
146 |
private Boolean ok = null; |
147 |
private String message = "Not hit yet."; |
148 |
private final Set<String> toBeHitIn; |
149 |
|
150 |
public TestBreakpointListener(JPDABreakpoint breakpoint, boolean isFiltered, |
151 |
List<JPDABreakpoint> allBreakpoints) { |
152 |
this.breakpoint = breakpoint; |
153 |
this.isFiltered = isFiltered; |
154 |
this.allBreakpoints = allBreakpoints; |
155 |
if (isFiltered) { |
156 |
toBeHitIn = new HashSet<String>(); |
157 |
toBeHitIn.add(APP_CLASS_NAME); |
158 |
toBeHitIn.add(APP2_CLASS_NAME); |
159 |
} else { |
160 |
toBeHitIn = null; |
161 |
} |
162 |
} |
163 |
|
164 |
@Override |
165 |
public void breakpointReached(JPDABreakpointEvent event) { |
166 |
//System.err.println("breakpointReached("+event+", isFiltered = "+isFiltered+"), referenceType = "+event.getReferenceType()); |
167 |
if (!isFiltered) { |
168 |
boolean removed = allBreakpoints.remove(breakpoint); |
169 |
if (ok == null && removed) { |
170 |
ok = Boolean.TRUE; |
171 |
message = "O.K."; |
172 |
} else { |
173 |
if (ok != null) { |
174 |
message += "Hit again at "+event.getReferenceType()+" "; |
175 |
ok = Boolean.FALSE; |
176 |
} else if (!removed) { |
177 |
message += "BP not in list. "; |
178 |
ok = Boolean.FALSE; |
179 |
} |
180 |
} |
181 |
} else { // Filtered, expected to hit twice (in APP_CLASS_NAME and APP2_CLASS_NAME) |
182 |
//System.err.println("breakpointReached("+event+", isFiltered = "+isFiltered+"), referenceType = "+event.getReferenceType()); |
183 |
String className = event.getReferenceType().name();//event.getThread().getClassName(); |
184 |
boolean removed = toBeHitIn.remove(className); |
185 |
if (ok == null && removed && toBeHitIn.isEmpty()) { |
186 |
allBreakpoints.remove(breakpoint); |
187 |
ok = Boolean.TRUE; |
188 |
message = "O.K."; |
189 |
} else { |
190 |
if (!removed) { |
191 |
message += "Hit again at "+className+" "; |
192 |
ok = Boolean.FALSE; |
193 |
} |
194 |
} |
195 |
} |
196 |
event.resume(); |
197 |
} |
198 |
|
199 |
public boolean isOK() { |
200 |
return Boolean.TRUE.equals(ok); |
201 |
} |
202 |
|
203 |
public String getMessage() { |
204 |
return message + " " + breakpoint+" with group "+breakpoint.getGroupName(); |
205 |
} |
206 |
|
207 |
} |
208 |
|
209 |
@BreakpointsClassFilter.Registration(path="netbeans-JPDASession") |
210 |
public static class TestBreakpointsClassFilter extends BreakpointsClassFilter { |
211 |
|
212 |
public TestBreakpointsClassFilter() { |
213 |
//System.err.println("NEW TestBreakpointsClassFilter()"); |
214 |
} |
215 |
|
216 |
@Override |
217 |
public ClassNames filterClassNames(ClassNames classNames, JPDABreakpoint breakpoint) { |
218 |
//System.err.println("filterClassNames("+Arrays.toString(classNames.getClassNames())); |
219 |
String groupName = breakpoint.getGroupName(); |
220 |
if (FILTER_GROUP_NAME.equals(groupName)) { |
221 |
String[] classes = classNames.getClassNames(); |
222 |
String[] newClasses = new String[classes.length + 1]; |
223 |
System.arraycopy(classes, 0, newClasses, 0, classes.length); |
224 |
if (breakpoint instanceof ExceptionBreakpoint) { |
225 |
newClasses[classes.length] = NullPointerException.class.getName(); |
226 |
} else { |
227 |
newClasses[classes.length] = APP2_CLASS_NAME; |
228 |
} |
229 |
return new ClassNames(newClasses, classNames.getExcludedClassNames()); |
230 |
} else { |
231 |
return classNames; |
232 |
} |
233 |
} |
234 |
|
235 |
} |
236 |
|
237 |
} |