KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > TestClosureOutboundSelector


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.io.*;
36 import java.util.*;
37
38 import junit.framework.*;
39
40 import org.apache.oro.text.perl.*;
41
42 public class TestClosureOutboundSelector extends TestCase {
43     private NodeFactory factory;
44     private NodeFactory localFactory;
45
46     private PackageNode a;
47     private ClassNode a_A;
48     private FeatureNode a_A_a;
49     
50     private PackageNode b;
51     private ClassNode b_B;
52     private FeatureNode b_B_b;
53     
54     private PackageNode c;
55     private ClassNode c_C;
56     private FeatureNode c_C_c;
57
58     protected void setUp() throws Exception JavaDoc {
59         factory = new NodeFactory();
60         localFactory = new NodeFactory();
61
62         a = factory.createPackage("a");
63         a_A = factory.createClass("a.A");
64         a_A_a = factory.createFeature("a.A.a");
65         
66         b = factory.createPackage("b");
67         b_B = factory.createClass("b.B");
68         b_B_b = factory.createFeature("b.B.b");
69         
70         c = factory.createPackage("c");
71         c_C = factory.createClass("c.C");
72         c_C_c = factory.createFeature("c.C.c");
73
74         a_A_a.addDependency(b_B_b);
75         b_B_b.addDependency(c_C_c);
76     }
77
78     public void testFactory() {
79         ClosureOutboundSelector selector = new ClosureOutboundSelector();
80
81         selector.setFactory(localFactory);
82
83         assertEquals("factory", localFactory, selector.getFactory());
84     }
85
86     public void testCoverage() {
87         Collection coverage = new ArrayList();
88
89         ClosureOutboundSelector selector = new ClosureOutboundSelector();
90
91         selector.setCoverage(coverage);
92
93         assertEquals("coverage", coverage, selector.getCoverage());
94     }
95
96     public void testOneSelectedNode() {
97         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
98         selector.traverseNodes(Collections.singleton(b_B_b));
99
100         assertEquals("nodes in selection", 1, selector.getSelectedNodes().size());
101         assertEquals("c.C.c in selection", c_C_c, selector.getSelectedNodes().iterator().next());
102         assertSame("c.C.c in selection", c_C_c, selector.getSelectedNodes().iterator().next());
103     }
104
105     public void testOneCopiedNode() {
106         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
107         selector.traverseNodes(Collections.singleton(b_B_b));
108
109         assertEquals("packages in scope", 2, localFactory.getPackages().size());
110         assertEquals("classes in scope" , 2, localFactory.getClasses().size());
111         assertEquals("features in scope", 2, localFactory.getFeatures().size());
112
113         assertEquals("package b in scope" , b, localFactory.getPackages().get("b"));
114         assertEquals("class b.B in scope" , b_B, localFactory.getClasses().get("b.B"));
115         assertEquals("feature b.B.b in scope", b_B_b, localFactory.getFeatures().get("b.B.b"));
116         assertEquals("package c in scope" , c, localFactory.getPackages().get("c"));
117         assertEquals("class c.C in scope" , c_C, localFactory.getClasses().get("c.C"));
118         assertEquals("feature c.C.c in scope", c_C_c, localFactory.getFeatures().get("c.C.c"));
119
120         assertNotSame("package b in scope" , b, localFactory.getPackages().get("b"));
121         assertNotSame("class b.B in scope" , b_B, localFactory.getClasses().get("b.B"));
122         assertNotSame("feature b.B.b in scope", b_B_b, localFactory.getFeatures().get("b.B.b"));
123         assertNotSame("package c in scope" , c, localFactory.getPackages().get("c"));
124         assertNotSame("class c.C in scope" , c_C, localFactory.getClasses().get("c.C"));
125         assertNotSame("feature c.C.c in scope", c_C_c, localFactory.getFeatures().get("c.C.c"));
126
127         assertEquals("nodes in selection", 1, selector.getCopiedNodes().size());
128         assertEquals("c.C.c in selection", c_C_c, selector.getCopiedNodes().iterator().next());
129         assertNotSame("c.C.c in selection", c_C_c, selector.getCopiedNodes().iterator().next());
130         assertSame("c.C.c in selection", localFactory.getFeatures().get("c.C.c"), selector.getCopiedNodes().iterator().next());
131         assertEquals("c.C.c's inbounds", 1, ((Node) selector.getCopiedNodes().iterator().next()).getInboundDependencies().size());
132         assertEquals("c.C.c's outbounds", 0, ((Node) selector.getCopiedNodes().iterator().next()).getOutboundDependencies().size());
133     }
134
135     public void testThreeSelectedNodesFromPackage() {
136         b.addDependency(c);
137         b.addDependency(c_C);
138         b.addDependency(c_C_c);
139         
140         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
141         selector.traverseNodes(Collections.singleton(b));
142
143         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
144         assertTrue("c in selection", selector.getSelectedNodes().contains(c));
145         assertTrue("c.C in selection", selector.getSelectedNodes().contains(c_C));
146         assertTrue("c.C.c in selection", selector.getSelectedNodes().contains(c_C_c));
147     }
148
149     public void testThreeSelectedNodesFromClass() {
150         b_B.addDependency(c);
151         b_B.addDependency(c_C);
152         b_B.addDependency(c_C_c);
153         
154         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
155         selector.traverseNodes(Collections.singleton(b_B));
156
157         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
158         assertTrue("c in selection", selector.getSelectedNodes().contains(c));
159         assertTrue("c.C in selection", selector.getSelectedNodes().contains(c_C));
160         assertTrue("c.C.c in selection", selector.getSelectedNodes().contains(c_C_c));
161     }
162
163     public void testThreeSelectedNodesFromFeature() {
164         b_B_b.addDependency(c);
165         b_B_b.addDependency(c_C);
166         b_B_b.addDependency(c_C_c);
167         
168         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
169         selector.traverseNodes(Collections.singleton(b_B_b));
170
171         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
172         assertTrue("c in selection", selector.getSelectedNodes().contains(c));
173         assertTrue("c.C in selection", selector.getSelectedNodes().contains(c_C));
174         assertTrue("c.C.c in selection", selector.getSelectedNodes().contains(c_C_c));
175     }
176
177     public void testThreeCopiedNodesFromPackage() {
178         b.addDependency(c);
179         b.addDependency(c_C);
180         b.addDependency(c_C_c);
181         
182         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
183         selector.traverseNodes(Collections.singleton(b));
184
185         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
186         assertTrue("c in selection", selector.getCopiedNodes().contains(c));
187         assertTrue("c.C in selection", selector.getCopiedNodes().contains(c_C));
188         assertTrue("c.C.c in selection", selector.getCopiedNodes().contains(c_C_c));
189
190         assertEquals("b's outbounds", 3, localFactory.createPackage("b").getOutboundDependencies().size());
191     }
192
193     public void testThreeCopiedNodesFromClass() {
194         b_B.addDependency(c);
195         b_B.addDependency(c_C);
196         b_B.addDependency(c_C_c);
197         
198         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
199         selector.traverseNodes(Collections.singleton(b_B));
200
201         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
202         assertTrue("c in selection", selector.getCopiedNodes().contains(c));
203         assertTrue("c.C in selection", selector.getCopiedNodes().contains(c_C));
204         assertTrue("c.C.c in selection", selector.getCopiedNodes().contains(c_C_c));
205
206         assertEquals("b.B's outbounds", 3, localFactory.createClass("b.B").getOutboundDependencies().size());
207     }
208
209     public void testThreeCopiedNodesFromFeature() {
210         b_B_b.addDependency(c);
211         b_B_b.addDependency(c_C);
212         b_B_b.addDependency(c_C_c);
213         
214         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
215         selector.traverseNodes(Collections.singleton(b_B_b));
216
217         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
218         assertTrue("c in selection", selector.getCopiedNodes().contains(c));
219         assertTrue("c.C in selection", selector.getCopiedNodes().contains(c_C));
220         assertTrue("c.C.c in selection", selector.getCopiedNodes().contains(c_C_c));
221
222         assertEquals("b.B.b's outbounds", 3, localFactory.createFeature("b.B.b").getOutboundDependencies().size());
223     }
224
225     public void testTwoSelectedNodeWithPackageInCoverage() {
226         b_B_b.addDependency(c);
227         b_B_b.addDependency(c_C);
228         b_B_b.addDependency(c_C_c);
229         
230         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c));
231         selector.traverseNodes(Collections.singleton(b_B_b));
232
233         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
234         assertTrue("c.C in selection", selector.getSelectedNodes().contains(c_C));
235         assertTrue("c.C.c in selection", selector.getSelectedNodes().contains(c_C_c));
236     }
237
238     public void testTwoSelectedNodeWithClassInCoverage() {
239         b_B_b.addDependency(c);
240         b_B_b.addDependency(c_C);
241         b_B_b.addDependency(c_C_c);
242         
243         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c_C));
244         selector.traverseNodes(Collections.singleton(b_B_b));
245
246         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
247         assertTrue("c in selection", selector.getSelectedNodes().contains(c));
248         assertTrue("c.C.c in selection", selector.getSelectedNodes().contains(c_C_c));
249     }
250
251     public void testTwoSelectedNodeWithFeatureInCoverage() {
252         b_B_b.addDependency(c);
253         b_B_b.addDependency(c_C);
254         b_B_b.addDependency(c_C_c);
255         
256         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c_C_c));
257         selector.traverseNodes(Collections.singleton(b_B_b));
258
259         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
260         assertTrue("c in selection", selector.getSelectedNodes().contains(c));
261         assertTrue("c.C in selection", selector.getSelectedNodes().contains(c_C));
262     }
263
264     public void testTwoCopiedNodeWithPackageInCoverage() {
265         b_B_b.addDependency(c);
266         b_B_b.addDependency(c_C);
267         b_B_b.addDependency(c_C_c);
268         
269         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c));
270         selector.traverseNodes(Collections.singleton(b_B_b));
271
272         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
273         assertTrue("c.C in selection", selector.getCopiedNodes().contains(c_C));
274         assertTrue("c.C.c in selection", selector.getCopiedNodes().contains(c_C_c));
275
276         assertEquals("b.B.b's outbounds", 2, localFactory.createFeature("b.B.b").getOutboundDependencies().size());
277     }
278
279     public void testTwoCopiedNodeWithClassInCoverage() {
280         b_B_b.addDependency(c);
281         b_B_b.addDependency(c_C);
282         b_B_b.addDependency(c_C_c);
283         
284         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c_C));
285         selector.traverseNodes(Collections.singleton(b_B_b));
286
287         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
288         assertTrue("c in selection", selector.getCopiedNodes().contains(c));
289         assertTrue("c.C.c in selection", selector.getCopiedNodes().contains(c_C_c));
290
291         assertEquals("b.B.b's outbounds", 2, localFactory.createFeature("b.B.b").getOutboundDependencies().size());
292     }
293
294     public void testTwoCopiedNodeWithFeatureInCoverage() {
295         b_B_b.addDependency(c);
296         b_B_b.addDependency(c_C);
297         b_B_b.addDependency(c_C_c);
298         
299         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.singleton(c_C_c));
300         selector.traverseNodes(Collections.singleton(b_B_b));
301
302         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
303         assertTrue("c in selection", selector.getCopiedNodes().contains(c));
304         assertTrue("c.C in selection", selector.getCopiedNodes().contains(c_C));
305
306         assertEquals("b.B.b's outbounds", 2, localFactory.createFeature("b.B.b").getOutboundDependencies().size());
307     }
308
309     public void testReset() {
310         NodeFactory localFactory = new NodeFactory();
311
312         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
313         selector.traverseNodes(Collections.singleton(b_B_b));
314
315         assertEquals("nodes in selection", 1, selector.getSelectedNodes().size());
316         assertEquals("copied nodes", 1, selector.getCopiedNodes().size());
317
318         selector.reset();
319         
320         assertEquals("nodes in selection", 0, selector.getSelectedNodes().size());
321         assertEquals("copied nodes", 0, selector.getCopiedNodes().size());
322     }
323
324     public void testVisitInferredPackage() {
325         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
326         selector.traverseNodes(Collections.singleton(b));
327
328         assertEquals("package.isConfirmed()", b.isConfirmed(), ((Node) localFactory.getPackages().get(b.getName())).isConfirmed());
329     }
330
331     public void testVisitConfirmedPackage() {
332         b.setConfirmed(true);
333         
334         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
335         selector.traverseNodes(Collections.singleton(b));
336
337         assertEquals("package.isConfirmed()", b.isConfirmed(), ((Node) localFactory.getPackages().get(b.getName())).isConfirmed());
338     }
339
340     public void testFollowToInferredPackage() {
341         b.addDependency(c);
342         
343         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
344         selector.traverseNodes(Collections.singleton(b));
345
346         assertEquals("package.isConfirmed()", c.isConfirmed(), ((Node) localFactory.getPackages().get(c.getName())).isConfirmed());
347     }
348
349     public void testFollowToConfirmedPackage() {
350         b.addDependency(c);
351         c.setConfirmed(true);
352         
353         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
354         selector.traverseNodes(Collections.singleton(b));
355
356         assertEquals("package.isConfirmed()", c.isConfirmed(), ((Node) localFactory.getPackages().get(c.getName())).isConfirmed());
357     }
358
359     public void testVisitInferredClass() {
360         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
361         selector.traverseNodes(Collections.singleton(b_B));
362
363         assertEquals("class.isConfirmed()", b_B.isConfirmed(), ((Node) localFactory.getClasses().get(b_B.getName())).isConfirmed());
364     }
365
366     public void testVisitConfirmedClass() {
367         b_B.setConfirmed(true);
368         
369         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
370         selector.traverseNodes(Collections.singleton(b_B));
371
372         assertEquals("class.isConfirmed()", b_B.isConfirmed(), ((Node) localFactory.getClasses().get(b_B.getName())).isConfirmed());
373     }
374
375     public void testFollowToInferredClass() {
376         b_B.addDependency(c_C);
377         
378         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
379         selector.traverseNodes(Collections.singleton(b_B));
380
381         assertEquals("class.isConfirmed()", c_C.isConfirmed(), ((Node) localFactory.getClasses().get(c_C.getName())).isConfirmed());
382     }
383
384     public void testFollowToConfirmedClass() {
385         b_B.addDependency(c_C);
386         c_C.setConfirmed(true);
387         
388         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
389         selector.traverseNodes(Collections.singleton(b_B));
390
391         assertEquals("class.isConfirmed()", c_C.isConfirmed(), ((Node) localFactory.getClasses().get(c_C.getName())).isConfirmed());
392     }
393
394     public void testVisitInferredFeature() {
395         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
396         selector.traverseNodes(Collections.singleton(b_B_b));
397
398         assertEquals("feature.isConfirmed()", b_B_b.isConfirmed(), ((Node) localFactory.getFeatures().get(b_B_b.getName())).isConfirmed());
399     }
400
401     public void testVisitConfirmedFeature() {
402         b_B_b.setConfirmed(true);
403         
404         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
405         selector.traverseNodes(Collections.singleton(b_B_b));
406
407         assertEquals("feature.isConfirmed()", b_B_b.isConfirmed(), ((Node) localFactory.getFeatures().get(b_B_b.getName())).isConfirmed());
408     }
409
410     public void testFollowToInferredFeature() {
411         b_B_b.addDependency(c_C_c);
412         
413         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
414         selector.traverseNodes(Collections.singleton(b_B_b));
415
416         assertEquals("feature.isConfirmed()", c_C_c.isConfirmed(), ((Node) localFactory.getFeatures().get(c_C_c.getName())).isConfirmed());
417     }
418
419     public void testFollowToConfirmedFeature() {
420         b_B_b.addDependency(c_C_c);
421         c_C_c.setConfirmed(true);
422         
423         ClosureOutboundSelector selector = new ClosureOutboundSelector(localFactory, Collections.EMPTY_SET);
424         selector.traverseNodes(Collections.singleton(b_B_b));
425
426         assertEquals("feature.isConfirmed()", c_C_c.isConfirmed(), ((Node) localFactory.getFeatures().get(c_C_c.getName())).isConfirmed());
427     }
428 }
429
Popular Tags