KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestClosureInboundSelector 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         ClosureInboundSelector selector = new ClosureInboundSelector();
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         ClosureInboundSelector selector = new ClosureInboundSelector();
90
91         selector.setCoverage(coverage);
92
93         assertEquals("coverage", coverage, selector.getCoverage());
94     }
95     
96     public void testOneSelectedNode() {
97         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
98         selector.traverseNodes(Collections.singleton(b_B_b));
99
100         assertEquals("nodes in selection", 1, selector.getSelectedNodes().size());
101         assertEquals("a.A.a in selection", a_A_a, selector.getSelectedNodes().iterator().next());
102         assertSame("a.A.a in selection", a_A_a, selector.getSelectedNodes().iterator().next());
103     }
104
105     public void testOneCopiedNode() {
106         ClosureInboundSelector selector = new ClosureInboundSelector(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 a in scope" , a, localFactory.getPackages().get("a"));
114         assertEquals("class a.A in scope" , a_A, localFactory.getClasses().get("a.A"));
115         assertEquals("feature a.A.a in scope", a_A_a, localFactory.getFeatures().get("a.A.a"));
116         assertEquals("package b in scope" , b, localFactory.getPackages().get("b"));
117         assertEquals("class b.B in scope" , b_B, localFactory.getClasses().get("b.B"));
118         assertEquals("feature b.B.b in scope", b_B_b, localFactory.getFeatures().get("b.B.b"));
119
120         assertNotSame("package a in scope" , a, localFactory.getPackages().get("a"));
121         assertNotSame("class a.A in scope" , a_A, localFactory.getClasses().get("a.A"));
122         assertNotSame("feature a.A.a in scope", a_A_a, localFactory.getFeatures().get("a.A.a"));
123         assertNotSame("package b in scope" , b, localFactory.getPackages().get("b"));
124         assertNotSame("class b.B in scope" , b_B, localFactory.getClasses().get("b.B"));
125         assertNotSame("feature b.B.b in scope", b_B_b, localFactory.getFeatures().get("b.B.b"));
126
127         assertEquals("nodes in selection", 1, selector.getCopiedNodes().size());
128         assertEquals("a.A.a in selection", a_A_a, selector.getCopiedNodes().iterator().next());
129         assertNotSame("a.A.a in selection", a_A_a, selector.getCopiedNodes().iterator().next());
130         assertSame("a.A.a in selection", localFactory.getFeatures().get("a.A.a"), selector.getCopiedNodes().iterator().next());
131         assertEquals("a.A.a's inbounds", 0, ((Node) selector.getCopiedNodes().iterator().next()).getInboundDependencies().size());
132         assertEquals("a.A.a's outbounds", 1, ((Node) selector.getCopiedNodes().iterator().next()).getOutboundDependencies().size());
133     }
134
135     public void testThreeSelectedNodesFromPackage() {
136         a.addDependency(b);
137         a_A.addDependency(b);
138         a_A_a.addDependency(b);
139         
140         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
141         selector.traverseNodes(Collections.singleton(b));
142
143         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
144         assertTrue("a in selection", selector.getSelectedNodes().contains(a));
145         assertTrue("a.A in selection", selector.getSelectedNodes().contains(a_A));
146         assertTrue("a.A.a in selection", selector.getSelectedNodes().contains(a_A_a));
147     }
148
149     public void testThreeSelectedNodesFromClass() {
150         a.addDependency(b_B);
151         a_A.addDependency(b_B);
152         a_A_a.addDependency(b_B);
153         
154         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
155         selector.traverseNodes(Collections.singleton(b_B));
156
157         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
158         assertTrue("a in selection", selector.getSelectedNodes().contains(a));
159         assertTrue("a.A in selection", selector.getSelectedNodes().contains(a_A));
160         assertTrue("a.A.a in selection", selector.getSelectedNodes().contains(a_A_a));
161     }
162
163     public void testThreeSelectedNodesFromFeature() {
164         a.addDependency(b_B_b);
165         a_A.addDependency(b_B_b);
166         a_A_a.addDependency(b_B_b);
167         
168         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
169         selector.traverseNodes(Collections.singleton(b_B_b));
170
171         assertEquals("nodes in selection", 3, selector.getSelectedNodes().size());
172         assertTrue("a in selection", selector.getSelectedNodes().contains(a));
173         assertTrue("a.A in selection", selector.getSelectedNodes().contains(a_A));
174         assertTrue("a.A.a in selection", selector.getSelectedNodes().contains(a_A_a));
175     }
176
177     public void testThreeCopiedNodesFromPackage() {
178         a.addDependency(b);
179         a_A.addDependency(b);
180         a_A_a.addDependency(b);
181         
182         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
183         selector.traverseNodes(Collections.singleton(b));
184
185         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
186         assertTrue("a in selection", selector.getCopiedNodes().contains(a));
187         assertTrue("a.A in selection", selector.getCopiedNodes().contains(a_A));
188         assertTrue("a.A.a in selection", selector.getCopiedNodes().contains(a_A_a));
189
190         assertEquals("b's inbounds", 3, localFactory.createPackage("b").getInboundDependencies().size());
191     }
192
193     public void testThreeCopiedNodesFromClass() {
194         a.addDependency(b_B);
195         a_A.addDependency(b_B);
196         a_A_a.addDependency(b_B);
197         
198         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
199         selector.traverseNodes(Collections.singleton(b_B));
200
201         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
202         assertTrue("a in selection", selector.getCopiedNodes().contains(a));
203         assertTrue("a.A in selection", selector.getCopiedNodes().contains(a_A));
204         assertTrue("a.A.a in selection", selector.getCopiedNodes().contains(a_A_a));
205
206         assertEquals("b.B's inbounds", 3, localFactory.createClass("b.B").getInboundDependencies().size());
207     }
208
209     public void testThreeCopiedNodesFromFeature() {
210         a.addDependency(b_B_b);
211         a_A.addDependency(b_B_b);
212         a_A_a.addDependency(b_B_b);
213         
214         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
215         selector.traverseNodes(Collections.singleton(b_B_b));
216
217         assertEquals("nodes in selection", 3, selector.getCopiedNodes().size());
218         assertTrue("a in selection", selector.getCopiedNodes().contains(a));
219         assertTrue("a.A in selection", selector.getCopiedNodes().contains(a_A));
220         assertTrue("a.A.a in selection", selector.getCopiedNodes().contains(a_A_a));
221
222         assertEquals("b.B.b's inbounds", 3, localFactory.createFeature("b.B.b").getInboundDependencies().size());
223     }
224
225     public void testTwoSelectedNodeWithPackageInCoverage() {
226         a.addDependency(b_B_b);
227         a_A.addDependency(b_B_b);
228         a_A_a.addDependency(b_B_b);
229         
230         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a));
231         selector.traverseNodes(Collections.singleton(b_B_b));
232
233         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
234         assertTrue("a.A in selection", selector.getSelectedNodes().contains(a_A));
235         assertTrue("a.A.a in selection", selector.getSelectedNodes().contains(a_A_a));
236     }
237
238     public void testTwoSelectedNodeWithClassInCoverage() {
239         a.addDependency(b_B_b);
240         a_A.addDependency(b_B_b);
241         a_A_a.addDependency(b_B_b);
242         
243         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a_A));
244         selector.traverseNodes(Collections.singleton(b_B_b));
245
246         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
247         assertTrue("a in selection", selector.getSelectedNodes().contains(a));
248         assertTrue("a.A.a in selection", selector.getSelectedNodes().contains(a_A_a));
249     }
250
251     public void testTwoSelectedNodeWithFeatureInCoverage() {
252         a.addDependency(b_B_b);
253         a_A.addDependency(b_B_b);
254         a_A_a.addDependency(b_B_b);
255         
256         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a_A_a));
257         selector.traverseNodes(Collections.singleton(b_B_b));
258
259         assertEquals("nodes in selection", 2, selector.getSelectedNodes().size());
260         assertTrue("a in selection", selector.getSelectedNodes().contains(a));
261         assertTrue("a.A in selection", selector.getSelectedNodes().contains(a_A));
262     }
263
264     public void testTwoCopiedNodeWithPackageInCoverage() {
265         a.addDependency(b_B_b);
266         a_A.addDependency(b_B_b);
267         a_A_a.addDependency(b_B_b);
268         
269         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a));
270         selector.traverseNodes(Collections.singleton(b_B_b));
271
272         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
273         assertTrue("a.A in selection", selector.getCopiedNodes().contains(a_A));
274         assertTrue("a.A.a in selection", selector.getCopiedNodes().contains(a_A_a));
275
276         assertEquals("b.B.b's inbounds", 2, localFactory.createFeature("b.B.b").getInboundDependencies().size());
277     }
278
279     public void testTwoCopiedNodeWithClassInCoverage() {
280         a.addDependency(b_B_b);
281         a_A.addDependency(b_B_b);
282         a_A_a.addDependency(b_B_b);
283         
284         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a_A));
285         selector.traverseNodes(Collections.singleton(b_B_b));
286
287         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
288         assertTrue("a in selection", selector.getCopiedNodes().contains(a));
289         assertTrue("a.A.a in selection", selector.getCopiedNodes().contains(a_A_a));
290
291         assertEquals("b.B.b's inbounds", 2, localFactory.createFeature("b.B.b").getInboundDependencies().size());
292     }
293
294     public void testTwoCopiedNodeWithFeatureInCoverage() {
295         a.addDependency(b_B_b);
296         a_A.addDependency(b_B_b);
297         a_A_a.addDependency(b_B_b);
298         
299         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.singleton(a_A_a));
300         selector.traverseNodes(Collections.singleton(b_B_b));
301
302         assertEquals("nodes in selection", 2, selector.getCopiedNodes().size());
303         assertTrue("a in selection", selector.getCopiedNodes().contains(a));
304         assertTrue("a.A in selection", selector.getCopiedNodes().contains(a_A));
305
306         assertEquals("b.B.b's inbounds", 2, localFactory.createFeature("b.B.b").getInboundDependencies().size());
307     }
308
309     public void testReset() {
310         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
311         selector.traverseNodes(Collections.singleton(b_B_b));
312
313         assertEquals("nodes in selection", 1, selector.getSelectedNodes().size());
314         assertEquals("copied nodes", 1, selector.getCopiedNodes().size());
315
316         selector.reset();
317         
318         assertEquals("nodes in selection", 0, selector.getSelectedNodes().size());
319         assertEquals("copied nodes", 0, selector.getCopiedNodes().size());
320     }
321
322     public void testVisitInferredPackage() {
323         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
324         selector.traverseNodes(Collections.singleton(b));
325
326         assertEquals("package.isConfirmed()", b.isConfirmed(), ((Node) localFactory.getPackages().get(b.getName())).isConfirmed());
327     }
328
329     public void testVisitConfirmedPackage() {
330         b.setConfirmed(true);
331         
332         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
333         selector.traverseNodes(Collections.singleton(b));
334
335         assertEquals("package.isConfirmed()", b.isConfirmed(), ((Node) localFactory.getPackages().get(b.getName())).isConfirmed());
336     }
337
338     public void testFollowToInferredPackage() {
339         a.addDependency(b);
340         
341         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
342         selector.traverseNodes(Collections.singleton(b));
343
344         assertEquals("package.isConfirmed()", a.isConfirmed(), ((Node) localFactory.getPackages().get(a.getName())).isConfirmed());
345     }
346
347     public void testFollowToConfirmedPackage() {
348         a.addDependency(b);
349         a.setConfirmed(true);
350         
351         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
352         selector.traverseNodes(Collections.singleton(b));
353
354         assertEquals("package.isConfirmed()", a.isConfirmed(), ((Node) localFactory.getPackages().get(a.getName())).isConfirmed());
355     }
356
357     public void testVisitInferredClass() {
358         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
359         selector.traverseNodes(Collections.singleton(b_B));
360
361         assertEquals("class.isConfirmed()", b_B.isConfirmed(), ((Node) localFactory.getClasses().get(b_B.getName())).isConfirmed());
362     }
363
364     public void testVisitConfirmedClass() {
365         b_B.setConfirmed(true);
366         
367         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
368         selector.traverseNodes(Collections.singleton(b_B));
369
370         assertEquals("class.isConfirmed()", b_B.isConfirmed(), ((Node) localFactory.getClasses().get(b_B.getName())).isConfirmed());
371     }
372
373     public void testFollowToInferredClass() {
374         a_A.addDependency(b_B);
375         
376         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
377         selector.traverseNodes(Collections.singleton(b_B));
378
379         assertEquals("class.isConfirmed()", a_A.isConfirmed(), ((Node) localFactory.getClasses().get(a_A.getName())).isConfirmed());
380     }
381
382     public void testFollowToConfirmedClass() {
383         a_A.addDependency(b_B);
384         a_A.setConfirmed(true);
385         
386         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
387         selector.traverseNodes(Collections.singleton(b_B));
388
389         assertEquals("class.isConfirmed()", a_A.isConfirmed(), ((Node) localFactory.getClasses().get(a_A.getName())).isConfirmed());
390     }
391
392     public void testVisitInferredFeature() {
393         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
394         selector.traverseNodes(Collections.singleton(b_B_b));
395
396         assertEquals("feature.isConfirmed()", b_B_b.isConfirmed(), ((Node) localFactory.getFeatures().get(b_B_b.getName())).isConfirmed());
397     }
398
399     public void testVisitConfirmedFeature() {
400         b_B_b.setConfirmed(true);
401         
402         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
403         selector.traverseNodes(Collections.singleton(b_B_b));
404
405         assertEquals("feature.isConfirmed()", b_B_b.isConfirmed(), ((Node) localFactory.getFeatures().get(b_B_b.getName())).isConfirmed());
406     }
407
408     public void testFollowToInferredFeature() {
409         a_A_a.addDependency(b_B_b);
410         
411         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
412         selector.traverseNodes(Collections.singleton(b_B_b));
413
414         assertEquals("feature.isConfirmed()", a_A_a.isConfirmed(), ((Node) localFactory.getFeatures().get(a_A_a.getName())).isConfirmed());
415     }
416
417     public void testFollowToConfirmedFeature() {
418         a_A_a.addDependency(b_B_b);
419         a_A_a.setConfirmed(true);
420         
421         ClosureInboundSelector selector = new ClosureInboundSelector(localFactory, Collections.EMPTY_SET);
422         selector.traverseNodes(Collections.singleton(b_B_b));
423
424         assertEquals("feature.isConfirmed()", a_A_a.isConfirmed(), ((Node) localFactory.getFeatures().get(a_A_a.getName())).isConfirmed());
425     }
426 }
427
Popular Tags