1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.dependency; |
34 | |
|
35 | |
import java.util.*; |
36 | |
|
37 | |
import org.jmock.*; |
38 | |
import org.jmock.api.*; |
39 | |
import org.jmock.integration.junit3.*; |
40 | |
import org.jmock.lib.legacy.*; |
41 | |
import static org.jmock.lib.script.ScriptedAction.*; |
42 | |
|
43 | |
import com.jeantessier.classreader.*; |
44 | |
|
45 | 77 | public class TestCodeDependencyCollectorUsingMocks extends MockObjectTestCase { |
46 | |
private static final String TEST_CLASS_NAME = "a.A"; |
47 | |
private static final String TEST_SUPERCLASS_NAME = "a.Parent"; |
48 | |
private static final String TEST_INTERFACE_NAME = "a.I"; |
49 | |
private static final String TEST_EXCEPTION_NAME = "java.lang.Exception"; |
50 | |
|
51 | |
private NodeFactory mockFactory; |
52 | |
private Classfile mockClassfile; |
53 | |
private ClassNode mockClassNode; |
54 | |
|
55 | |
private CodeDependencyCollector sut; |
56 | |
|
57 | |
protected void setUp() throws Exception { |
58 | 11 | super.setUp(); |
59 | |
|
60 | 11 | setImposteriser(ClassImposteriser.INSTANCE); |
61 | |
|
62 | 11 | mockFactory = mock(NodeFactory.class); |
63 | 11 | mockClassfile = mock(Classfile.class); |
64 | 11 | mockClassNode = mock(ClassNode.class, "testclass"); |
65 | |
|
66 | 11 | sut = new CodeDependencyCollector(mockFactory); |
67 | 11 | } |
68 | |
|
69 | |
public void testVisitClass_info_exceptions() { |
70 | 1 | final Class_info mockException = mock(Class_info.class); |
71 | 1 | final ClassNode mockExceptionNode = mock(ClassNode.class); |
72 | |
|
73 | 1 | checking(new Expectations() {{ |
74 | 1 | one (mockException).getName(); |
75 | 1 | will(returnValue(TEST_EXCEPTION_NAME)); |
76 | 1 | one (mockFactory).createClass(TEST_EXCEPTION_NAME); |
77 | 1 | will(returnValue(mockExceptionNode)); |
78 | 1 | one (mockClassNode).addDependency(mockExceptionNode); |
79 | 1 | }}); |
80 | |
|
81 | 1 | sut.setCurrent(mockClassNode); |
82 | 1 | sut.visitClass_info(mockException); |
83 | 1 | } |
84 | |
|
85 | |
public void testVisitClassfile_withoutsuperclass() { |
86 | 1 | expectClassNodeForClassname(); |
87 | |
|
88 | 1 | checking(new Expectations() {{ |
89 | 1 | atLeast(1).of (mockClassfile).getClassName(); |
90 | 1 | will(returnValue(TEST_CLASS_NAME)); |
91 | 1 | one (mockFactory).createClass(TEST_CLASS_NAME, true); |
92 | 1 | will(returnValue(mockClassNode)); |
93 | 1 | one (mockClassfile).getSuperclassIndex(); |
94 | 1 | will(returnValue(0)); |
95 | 1 | never (mockClassNode).addParent(with(any(ClassNode.class))); |
96 | 1 | ignoring (mockClassfile).getAllInterfaces(); |
97 | 1 | ignoring (mockClassfile).getAllFields(); |
98 | 1 | ignoring (mockClassfile).getAllMethods(); |
99 | 1 | ignoring (mockClassfile).getAttributes(); |
100 | 1 | }}); |
101 | |
|
102 | 1 | sut.visitClassfile(mockClassfile); |
103 | 1 | } |
104 | |
|
105 | |
public void testVisitClassfile_withsuperclass() { |
106 | 1 | final Class_info mockRawSuperclass = mock(Class_info.class); |
107 | 1 | final ClassNode mockSuperclassNode = mock(ClassNode.class, "superclass"); |
108 | |
|
109 | 1 | expectClassNodeForClassname(); |
110 | |
|
111 | 1 | checking(new Expectations() {{ |
112 | 1 | atLeast(1).of (mockClassfile).getClassName(); |
113 | 1 | will(returnValue(TEST_CLASS_NAME)); |
114 | 1 | one (mockFactory).createClass(TEST_CLASS_NAME, true); |
115 | 1 | will(returnValue(mockClassNode)); |
116 | 1 | one (mockClassfile).getSuperclassIndex(); |
117 | 1 | will(returnValue(1)); |
118 | 1 | one (mockClassfile).getRawSuperclass(); |
119 | 1 | will(returnValue(mockRawSuperclass)); |
120 | 1 | one (mockRawSuperclass).accept(sut); |
121 | 1 | atLeast(1).of (mockRawSuperclass).getName(); |
122 | 1 | will(returnValue(TEST_SUPERCLASS_NAME)); |
123 | 1 | atLeast(1).of (mockFactory).createClass(TEST_SUPERCLASS_NAME); |
124 | 1 | will(returnValue(mockSuperclassNode)); |
125 | 1 | one (mockClassNode).addParent(mockSuperclassNode); |
126 | 1 | ignoring (mockClassfile).getAllInterfaces(); |
127 | 1 | ignoring (mockClassfile).getAllFields(); |
128 | 1 | ignoring (mockClassfile).getAllMethods(); |
129 | 1 | ignoring (mockClassfile).getAttributes(); |
130 | 1 | }}); |
131 | |
|
132 | 1 | sut.visitClassfile(mockClassfile); |
133 | 1 | } |
134 | |
|
135 | |
public void testVisitClassfile_withinterface() { |
136 | 1 | final Class_info mockInterface = mock(Class_info.class); |
137 | 1 | final ClassNode mockInterfaceNode = mock(ClassNode.class, "interface"); |
138 | |
|
139 | 1 | final Collection<Class_info> allInterfaces = new ArrayList<Class_info>(); |
140 | 1 | allInterfaces.add(mockInterface); |
141 | |
|
142 | 1 | expectClassNodeForClassname(); |
143 | |
|
144 | 1 | checking(new Expectations() {{ |
145 | 1 | atLeast(1).of (mockClassfile).getClassName(); |
146 | 1 | will(returnValue(TEST_CLASS_NAME)); |
147 | 1 | one (mockFactory).createClass(TEST_CLASS_NAME, true); |
148 | 1 | will(returnValue(mockClassNode)); |
149 | 1 | one (mockClassfile).getSuperclassIndex(); |
150 | 1 | will(returnValue(0)); |
151 | 1 | one (mockClassfile).getAllInterfaces(); |
152 | 1 | will(returnValue(allInterfaces)); |
153 | 1 | one (mockInterface).accept(sut); |
154 | 1 | atLeast(1).of (mockInterface).getName(); |
155 | 1 | will(returnValue(TEST_INTERFACE_NAME)); |
156 | 1 | atLeast(1).of (mockFactory).createClass(TEST_INTERFACE_NAME); |
157 | 1 | will(returnValue(mockInterfaceNode)); |
158 | 1 | one (mockClassNode).addParent(mockInterfaceNode); |
159 | 1 | ignoring (mockClassfile).getAllFields(); |
160 | 1 | ignoring (mockClassfile).getAllMethods(); |
161 | 1 | ignoring (mockClassfile).getAttributes(); |
162 | 1 | }}); |
163 | |
|
164 | 1 | sut.visitClassfile(mockClassfile); |
165 | 1 | } |
166 | |
|
167 | |
public void testVisitClassfile_fireevents() { |
168 | 1 | final DependencyListener mockListener = mock(DependencyListener.class); |
169 | |
|
170 | 1 | expectClassNodeForClassname(); |
171 | |
|
172 | 1 | checking(new Expectations() {{ |
173 | 1 | atLeast(1).of (mockClassfile).getClassName(); |
174 | 1 | will(returnValue(TEST_CLASS_NAME)); |
175 | |
|
176 | 1 | one (mockListener).beginClass(with(any(DependencyEvent.class))); |
177 | 1 | will(createEventCheckingAction(TEST_CLASS_NAME)); |
178 | |
|
179 | 1 | one (mockFactory).createClass(TEST_CLASS_NAME, true); |
180 | |
|
181 | 1 | ignoring (mockClassfile).getSuperclassIndex(); |
182 | 1 | ignoring (mockClassfile).getAllInterfaces(); |
183 | 1 | ignoring (mockClassfile).getAllFields(); |
184 | 1 | ignoring (mockClassfile).getAllMethods(); |
185 | 1 | ignoring (mockClassfile).getAttributes(); |
186 | |
|
187 | 1 | one (mockListener).endClass(with(any(DependencyEvent.class))); |
188 | 1 | will(createEventCheckingAction(TEST_CLASS_NAME)); |
189 | |
|
190 | 1 | }}); |
191 | |
|
192 | 1 | sut.addDependencyListener(mockListener); |
193 | 1 | sut.visitClassfile(mockClassfile); |
194 | 1 | } |
195 | |
|
196 | |
public void testVisitExceptionHandler_finally() { |
197 | 1 | final ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); |
198 | |
|
199 | 1 | checking(new Expectations() {{ |
200 | 1 | one (mockExceptionHandler).getCatchTypeIndex(); |
201 | 1 | will(returnValue(0)); |
202 | 1 | }}); |
203 | |
|
204 | 1 | sut.visitExceptionHandler(mockExceptionHandler); |
205 | 1 | } |
206 | |
|
207 | |
public void testVisitExceptionHandler_catch() { |
208 | 1 | final ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class); |
209 | 1 | final Class_info mockCatchType = mock(Class_info.class); |
210 | |
|
211 | 1 | checking(new Expectations() {{ |
212 | 1 | one (mockExceptionHandler).getCatchTypeIndex(); |
213 | 1 | will(returnValue(1)); |
214 | 1 | one (mockExceptionHandler).getRawCatchType(); |
215 | 1 | will(returnValue(mockCatchType)); |
216 | 1 | one (mockCatchType).accept(sut); |
217 | 1 | }}); |
218 | |
|
219 | 1 | sut.visitExceptionHandler(mockExceptionHandler); |
220 | 1 | } |
221 | |
|
222 | |
public void testVisitAnnotation() { |
223 | 1 | final Annotation mockAnnotation = mock(Annotation.class); |
224 | 1 | final ClassNode mockDependableClassNode = mock(ClassNode.class, "dependable"); |
225 | |
|
226 | 1 | checking(new Expectations() {{ |
227 | 1 | one (mockAnnotation).getType(); |
228 | 1 | will(returnValue("dependable.Dependable")); |
229 | 1 | one (mockFactory).createClass("dependable.Dependable"); |
230 | 1 | will(returnValue(mockDependableClassNode)); |
231 | 1 | one (mockClassNode).addDependency(mockDependableClassNode); |
232 | |
|
233 | 1 | ignoring (mockAnnotation).getElementValuePairs(); |
234 | 1 | }}); |
235 | |
|
236 | 1 | sut.setCurrent(mockClassNode); |
237 | 1 | sut.visitAnnotation(mockAnnotation); |
238 | 1 | } |
239 | |
|
240 | |
public void testVisitEnumElementValue() { |
241 | 1 | final EnumElementValue mockEnumElementValue = mock(EnumElementValue.class); |
242 | 1 | final FeatureNode mockDependableFeatureNode = mock(FeatureNode.class, "dependable.CONSTANT"); |
243 | |
|
244 | 1 | checking(new Expectations() {{ |
245 | 1 | one (mockEnumElementValue).getTypeName(); |
246 | 1 | will(returnValue("dependable.Dependable")); |
247 | 1 | one (mockEnumElementValue).getConstName(); |
248 | 1 | will(returnValue("CONSTANT")); |
249 | 1 | one (mockFactory).createFeature("dependable.Dependable.CONSTANT"); |
250 | 1 | will(returnValue(mockDependableFeatureNode)); |
251 | 1 | one (mockClassNode).addDependency(mockDependableFeatureNode); |
252 | 1 | }}); |
253 | |
|
254 | 1 | sut.setCurrent(mockClassNode); |
255 | 1 | sut.visitEnumElementValue(mockEnumElementValue); |
256 | 1 | } |
257 | |
|
258 | |
public void testVisitClassElementValue() { |
259 | 1 | final ClassElementValue mockClassElementValue = mock(ClassElementValue.class); |
260 | 1 | final ClassNode mockDependableClassNode = mock(ClassNode.class, "dependable"); |
261 | |
|
262 | 1 | checking(new Expectations() {{ |
263 | 1 | one (mockClassElementValue).getClassInfo(); |
264 | 1 | will(returnValue("dependable.Dependable")); |
265 | 1 | one (mockFactory).createClass("dependable.Dependable"); |
266 | 1 | will(returnValue(mockDependableClassNode)); |
267 | 1 | one (mockClassNode).addDependency(mockDependableClassNode); |
268 | 1 | }}); |
269 | |
|
270 | 1 | sut.setCurrent(mockClassNode); |
271 | 1 | sut.visitClassElementValue(mockClassElementValue); |
272 | 1 | } |
273 | |
|
274 | |
public void testVisitClassAnnotations() { |
275 | 1 | expectClassNodeForClassname(); |
276 | |
|
277 | 1 | checking(new Expectations() {{ |
278 | 1 | ignoring (mockClassfile).getAttributes(); |
279 | 1 | }}); |
280 | |
|
281 | 1 | sut.visitClassfileAttributes(mockClassfile); |
282 | 1 | } |
283 | |
|
284 | |
private void expectClassNodeForClassname() { |
285 | 5 | checking(new Expectations() {{ |
286 | 5 | one (mockClassfile).getClassName(); |
287 | 5 | will(returnValue(TEST_CLASS_NAME)); |
288 | 5 | one (mockFactory).createClass(TEST_CLASS_NAME); |
289 | 5 | will(returnValue(mockClassNode)); |
290 | 5 | }}); |
291 | 5 | } |
292 | |
|
293 | |
private Action createEventCheckingAction(final String expectedClassName) { |
294 | 2 | return |
295 | |
perform( |
296 | |
"junit.framework.Assert.assertEquals(\"source\", sut, $0.getSource());" + |
297 | |
"junit.framework.Assert.assertEquals(\"classname\", expectedClassName, $0.getClassName());" + |
298 | |
"junit.framework.Assert.assertNull(\"dependent\", $0.getDependent());" + |
299 | |
"junit.framework.Assert.assertNull(\"dependable\", $0.getDependable())") |
300 | |
.where("sut", sut) |
301 | |
.where("expectedClassName", expectedClassName); |
302 | |
} |
303 | |
} |