Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2015 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 2015 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.api.debugger.jpda; |
44 |
|
45 |
import java.util.Arrays; |
46 |
import java.util.EventListener; |
47 |
import java.util.HashSet; |
48 |
import java.util.List; |
49 |
import java.util.Set; |
50 |
import javax.swing.SwingConstants; |
51 |
import junit.framework.Test; |
52 |
import org.netbeans.api.debugger.DebuggerManager; |
53 |
import org.netbeans.junit.NbTestCase; |
54 |
import org.netbeans.modules.debugger.jpda.models.CallStackFrameImpl; |
55 |
|
56 |
/** |
57 |
* A test of JPDAClassType class. |
58 |
* |
59 |
* @author Martin Entlicher |
60 |
*/ |
61 |
public class JPDAClassTypeTest extends NbTestCase { |
62 |
|
63 |
private static final String APP_SRC_NAME = "org/netbeans/api/debugger/jpda/testapps/JPDAClassTypeTestApp.java"; // NOI18N |
64 |
private static final String APP_CLASS_NAME = "org.netbeans.api.debugger.jpda.testapps.JPDAClassTypeTestApp"; // NOI18N |
65 |
|
66 |
private JPDASupport support; |
67 |
private JPDAClassType testAppClass; |
68 |
private JPDAClassType multiImplClass; |
69 |
|
70 |
public JPDAClassTypeTest(String s) { |
71 |
super(s); |
72 |
} |
73 |
|
74 |
public static Test suite() { |
75 |
return JPDASupport.createTestSuite(JPDAClassTypeTest.class); |
76 |
} |
77 |
|
78 |
@Override |
79 |
protected void setUp () throws Exception { |
80 |
super.setUp (); |
81 |
JPDASupport.removeAllBreakpoints (); |
82 |
Utils.BreakPositions bp = Utils.getBreakPositions(System.getProperty("test.dir.src")+ |
83 |
APP_SRC_NAME); |
84 |
LineBreakpoint lb = bp.getLineBreakpoints().get(0); |
85 |
DebuggerManager.getDebuggerManager().addBreakpoint(lb); |
86 |
support = JPDASupport.attach(APP_CLASS_NAME); |
87 |
support.waitState(JPDADebugger.STATE_STOPPED); |
88 |
CallStackFrame csf = support.getDebugger().getCurrentCallStackFrame(); |
89 |
LocalVariable[] methodArguments = ((CallStackFrameImpl) csf).getMethodArguments(); |
90 |
testAppClass = ((ObjectVariable) methodArguments[0]).getClassType(); |
91 |
multiImplClass = ((ObjectVariable) methodArguments[1]).getClassType(); |
92 |
} |
93 |
|
94 |
public void testJPDAClassTypeMethods() throws Exception { |
95 |
checkName(); |
96 |
checkSourceName(); |
97 |
checkClassObject(); |
98 |
checkClassLoader(); |
99 |
checkSuperClass(); |
100 |
checkSubClasses(); |
101 |
checkAllInterfaces(); |
102 |
checkDirectInterfaces(); |
103 |
checkInstanceOf(); |
104 |
checkStaticFields(); |
105 |
checkInvokeMethod(); |
106 |
checkInstanceCount(); |
107 |
checkInstances(); |
108 |
} |
109 |
|
110 |
private void checkName() throws Exception { |
111 |
String tasn = testAppClass.getName(); |
112 |
String misn = multiImplClass.getName(); |
113 |
assertEquals("Wrong Name", APP_CLASS_NAME, tasn); |
114 |
assertEquals("Wrong Name", APP_CLASS_NAME+"$MultiImpl", misn); |
115 |
} |
116 |
|
117 |
private void checkSourceName() throws Exception { |
118 |
String tasn = testAppClass.getSourceName(); |
119 |
String misn = multiImplClass.getSourceName(); |
120 |
String expectedSrcName = APP_SRC_NAME.substring(APP_SRC_NAME.lastIndexOf('/')+1); |
121 |
assertEquals("Wrong Source Name", expectedSrcName, tasn); |
122 |
assertEquals("Wrong Source Name", expectedSrcName, misn); |
123 |
} |
124 |
|
125 |
private void checkClassObject() throws Exception { |
126 |
ClassVariable taClassObject = testAppClass.classObject(); |
127 |
ClassVariable miClassObject = multiImplClass.classObject(); |
128 |
assertEquals("Wrong Class Object Type", "java.lang.Class", taClassObject.getType()); |
129 |
assertEquals("Wrong Class Object Type", "java.lang.Class", miClassObject.getType()); |
130 |
assertEquals("Wrong Class Object", "class "+APP_CLASS_NAME, taClassObject.getToStringValue()); |
131 |
assertEquals("Wrong Class Object", "class "+APP_CLASS_NAME+"$MultiImpl", miClassObject.getToStringValue()); |
132 |
} |
133 |
|
134 |
private void checkClassLoader() throws Exception { |
135 |
ObjectVariable taClassLoader = testAppClass.getClassLoader(); |
136 |
String taClassLoaderStr = taClassLoader.getToStringValue(); |
137 |
assertTrue("Wrong Class Loader: "+taClassLoaderStr, taClassLoaderStr.startsWith("sun.misc.Launcher$AppClassLoader@")); |
138 |
} |
139 |
|
140 |
private void checkSuperClass() throws Exception { |
141 |
Super taSuperClass = testAppClass.getSuperClass(); |
142 |
Super miSuperClass = multiImplClass.getSuperClass(); |
143 |
assertEquals("Wrong Super Class", "java.lang.Object", taSuperClass.getType()); |
144 |
Super ss = taSuperClass.getSuper(); |
145 |
assertNull("Non null Object super", ss); |
146 |
assertEquals("Wrong Super Class", APP_CLASS_NAME+"$SuperImpl", miSuperClass.getType()); |
147 |
} |
148 |
|
149 |
private void checkSubClasses() throws Exception { |
150 |
List<JPDAClassType> subClasses = testAppClass.getSubClasses(); |
151 |
assertTrue("Subclasses should be empty", subClasses.isEmpty()); |
152 |
subClasses = multiImplClass.getSubClasses(); |
153 |
assertEquals("Wrong number of subclasses", 2, subClasses.size()); |
154 |
Set<String> subClassesNames = new HashSet<>(Arrays.asList(new String[] { |
155 |
APP_CLASS_NAME+"$MultiImplSubClass1", |
156 |
APP_CLASS_NAME+"$MultiImplSubClass2", |
157 |
})); |
158 |
for (JPDAClassType sc : subClasses) { |
159 |
boolean removed = subClassesNames.remove(sc.getName()); |
160 |
assertTrue("Wrong subclass '"+sc.getName()+"'", removed); |
161 |
} |
162 |
assertTrue("Not found subclasses: "+subClassesNames.toString(), subClassesNames.isEmpty()); |
163 |
} |
164 |
|
165 |
private void checkAllInterfaces() throws Exception { |
166 |
Set<String> taAllInterfaces = new HashSet<>(Arrays.asList(new String[] { |
167 |
EventListener.class.getName(), |
168 |
SwingConstants.class.getName(), |
169 |
})); |
170 |
Set<String> miAllInterfaces = new HashSet<>(Arrays.asList(new String[] { |
171 |
Runnable.class.getName(), |
172 |
APP_CLASS_NAME+"$Intrfc1", |
173 |
APP_CLASS_NAME+"$Intrfc2", |
174 |
APP_CLASS_NAME+"$Intrfc3", |
175 |
APP_CLASS_NAME+"$Intrfc4", |
176 |
})); |
177 |
Set<String> mifAllInterfaces = new HashSet<>(Arrays.asList(new String[] { |
178 |
Runnable.class.getName(), |
179 |
APP_CLASS_NAME+"$Intrfc1", |
180 |
APP_CLASS_NAME+"$Intrfc2", |
181 |
APP_CLASS_NAME+"$Intrfc3", |
182 |
APP_CLASS_NAME+"$Intrfc4", |
183 |
APP_CLASS_NAME+"$Intrfc5", |
184 |
})); |
185 |
|
186 |
List<JPDAClassType> allInterfaces = testAppClass.getAllInterfaces(); |
187 |
assertEquals("Wrong number of all interfaces", taAllInterfaces.size(), allInterfaces.size()); |
188 |
for (JPDAClassType sc : allInterfaces) { |
189 |
boolean removed = taAllInterfaces.remove(sc.getName()); |
190 |
assertTrue("Wrong all interface '"+sc.getName()+"'", removed); |
191 |
} |
192 |
assertTrue("Not found all interfaces: "+taAllInterfaces.toString(), taAllInterfaces.isEmpty()); |
193 |
|
194 |
allInterfaces = multiImplClass.getAllInterfaces(); |
195 |
assertEquals("Wrong number of all interfaces", miAllInterfaces.size(), allInterfaces.size()); |
196 |
for (JPDAClassType sc : allInterfaces) { |
197 |
boolean removed = miAllInterfaces.remove(sc.getName()); |
198 |
assertTrue("Wrong all interface '"+sc.getName()+"'", removed); |
199 |
} |
200 |
assertTrue("Not found all interfaces: "+miAllInterfaces.toString(), miAllInterfaces.isEmpty()); |
201 |
|
202 |
JPDAClassType multiIntrfcClass = null; |
203 |
for (Field sf : multiImplClass.staticFields()) { |
204 |
if (sf.getName().equals("multiIntrfc")) { |
205 |
multiIntrfcClass = ((ClassVariable) sf).getReflectedType(); |
206 |
break; |
207 |
} |
208 |
} |
209 |
assertNotNull("Did not find the multiIntrfc field", multiIntrfcClass); |
210 |
|
211 |
allInterfaces = multiIntrfcClass.getAllInterfaces(); |
212 |
assertEquals("Wrong number of all interfaces", mifAllInterfaces.size(), allInterfaces.size()); |
213 |
for (JPDAClassType sc : allInterfaces) { |
214 |
boolean removed = mifAllInterfaces.remove(sc.getName()); |
215 |
assertTrue("Wrong all interface '"+sc.getName()+"'", removed); |
216 |
} |
217 |
assertTrue("Not found all interfaces: "+mifAllInterfaces.toString(), mifAllInterfaces.isEmpty()); |
218 |
} |
219 |
|
220 |
private void checkDirectInterfaces() throws Exception { |
221 |
Set<String> taDirectInterfaces = new HashSet<>(Arrays.asList(new String[] { |
222 |
EventListener.class.getName(), |
223 |
SwingConstants.class.getName(), |
224 |
})); |
225 |
Set<String> miDirectInterfaces = new HashSet<>(Arrays.asList(new String[] { |
226 |
Runnable.class.getName(), |
227 |
APP_CLASS_NAME+"$Intrfc1", |
228 |
APP_CLASS_NAME+"$Intrfc2", |
229 |
})); |
230 |
Set<String> mifDirectInterfaces = new HashSet<>(Arrays.asList(new String[] { |
231 |
Runnable.class.getName(), |
232 |
APP_CLASS_NAME+"$Intrfc1", |
233 |
APP_CLASS_NAME+"$Intrfc4", |
234 |
APP_CLASS_NAME+"$Intrfc5", |
235 |
})); |
236 |
|
237 |
List<JPDAClassType> directInterfaces = testAppClass.getDirectInterfaces(); |
238 |
assertEquals("Wrong number of direct interfaces", taDirectInterfaces.size(), directInterfaces.size()); |
239 |
for (JPDAClassType sc : directInterfaces) { |
240 |
boolean removed = taDirectInterfaces.remove(sc.getName()); |
241 |
assertTrue("Wrong direct interface '"+sc.getName()+"'", removed); |
242 |
} |
243 |
assertTrue("Not found direct interfaces: "+taDirectInterfaces.toString(), taDirectInterfaces.isEmpty()); |
244 |
|
245 |
directInterfaces = multiImplClass.getDirectInterfaces(); |
246 |
assertEquals("Wrong number of direct interfaces", miDirectInterfaces.size(), directInterfaces.size()); |
247 |
for (JPDAClassType sc : directInterfaces) { |
248 |
boolean removed = miDirectInterfaces.remove(sc.getName()); |
249 |
assertTrue("Wrong direct interface '"+sc.getName()+"'", removed); |
250 |
} |
251 |
assertTrue("Not found direct interfaces: "+miDirectInterfaces.toString(), miDirectInterfaces.isEmpty()); |
252 |
|
253 |
JPDAClassType multiIntrfcClass = null; |
254 |
for (Field sf : multiImplClass.staticFields()) { |
255 |
if (sf.getName().equals("multiIntrfc")) { |
256 |
multiIntrfcClass = ((ClassVariable) sf).getReflectedType(); |
257 |
} |
258 |
} |
259 |
assertNotNull("Did not find the multiIntrfc field", multiIntrfcClass); |
260 |
|
261 |
directInterfaces = multiIntrfcClass.getDirectInterfaces(); |
262 |
assertEquals("Wrong number of direct interfaces", mifDirectInterfaces.size(), directInterfaces.size()); |
263 |
for (JPDAClassType sc : directInterfaces) { |
264 |
boolean removed = mifDirectInterfaces.remove(sc.getName()); |
265 |
assertTrue("Wrong direct interface '"+sc.getName()+"'", removed); |
266 |
} |
267 |
assertTrue("Not found direct interfaces: "+mifDirectInterfaces.toString(), mifDirectInterfaces.isEmpty()); |
268 |
} |
269 |
|
270 |
private void checkInstanceOf() throws Exception { |
271 |
boolean is = testAppClass.isInstanceOf(APP_SRC_NAME); |
272 |
assertFalse(is); |
273 |
is = testAppClass.isInstanceOf(APP_CLASS_NAME); |
274 |
assertTrue("Instance of "+APP_CLASS_NAME, is); |
275 |
is = testAppClass.isInstanceOf(EventListener.class.getName()); |
276 |
assertTrue("Instance of "+EventListener.class.getName(), is); |
277 |
|
278 |
is = multiImplClass.isInstanceOf("huuuhuuu"); |
279 |
assertFalse(is); |
280 |
is = multiImplClass.isInstanceOf(APP_CLASS_NAME+"$MultiImpl"); |
281 |
assertTrue("Instance of "+APP_CLASS_NAME+"$MultiImpl", is); |
282 |
is = multiImplClass.isInstanceOf(Runnable.class.getName()); |
283 |
assertTrue("Instance of "+Runnable.class.getName(), is); |
284 |
is = multiImplClass.isInstanceOf(APP_CLASS_NAME+"$SuperImpl"); |
285 |
assertTrue("Instance of "+APP_CLASS_NAME+"$SuperImpl", is); |
286 |
is = multiImplClass.isInstanceOf(APP_CLASS_NAME+"$Intrfc4"); |
287 |
assertTrue("Instance of "+APP_CLASS_NAME+"$Intrfc4", is); |
288 |
} |
289 |
|
290 |
private void checkStaticFields() throws Exception { |
291 |
List<Field> staticFields = testAppClass.staticFields(); |
292 |
// Contains all SwingConstants |
293 |
assertFalse(staticFields.isEmpty()); |
294 |
|
295 |
staticFields = multiImplClass.staticFields(); |
296 |
assertEquals("Static fields", 1, staticFields.size()); |
297 |
} |
298 |
|
299 |
private void checkInvokeMethod() throws Exception { |
300 |
Variable pi = multiImplClass.invokeMethod("getPreparedInterface", "()Ljava/lang/Class;", new Variable[]{}); |
301 |
assertNotNull(pi); |
302 |
assertEquals("getPreparedInterface() method invoked", "java.lang.Class", pi.getType()); |
303 |
} |
304 |
|
305 |
private void checkInstanceCount() throws Exception { |
306 |
long ic = testAppClass.getInstanceCount(); |
307 |
assertEquals("Number of instances", 1, ic); |
308 |
ic = multiImplClass.getInstanceCount(); |
309 |
assertEquals("Number of instances", 4, ic); |
310 |
} |
311 |
|
312 |
private void checkInstances() throws Exception { |
313 |
List<ObjectVariable> instances = testAppClass.getInstances(100); |
314 |
assertEquals(1, instances.size()); |
315 |
instances = multiImplClass.getInstances(100); |
316 |
assertEquals(4, instances.size()); |
317 |
instances = multiImplClass.getInstances(2); |
318 |
assertEquals(2, instances.size()); |
319 |
} |
320 |
|
321 |
} |