001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.util.*;
036:
037: import junit.framework.*;
038:
039: public class TestClosureInboundSelector extends TestCase {
040: private NodeFactory factory;
041: private NodeFactory localFactory;
042:
043: private PackageNode a;
044: private ClassNode a_A;
045: private FeatureNode a_A_a;
046:
047: private PackageNode b;
048: private ClassNode b_B;
049: private FeatureNode b_B_b;
050:
051: private PackageNode c;
052: private ClassNode c_C;
053: private FeatureNode c_C_c;
054:
055: protected void setUp() throws Exception {
056: factory = new NodeFactory();
057: localFactory = new NodeFactory();
058:
059: a = factory.createPackage("a");
060: a_A = factory.createClass("a.A");
061: a_A_a = factory.createFeature("a.A.a");
062:
063: b = factory.createPackage("b");
064: b_B = factory.createClass("b.B");
065: b_B_b = factory.createFeature("b.B.b");
066:
067: c = factory.createPackage("c");
068: c_C = factory.createClass("c.C");
069: c_C_c = factory.createFeature("c.C.c");
070:
071: a_A_a.addDependency(b_B_b);
072: b_B_b.addDependency(c_C_c);
073: }
074:
075: public void testFactory() {
076: ClosureInboundSelector selector = new ClosureInboundSelector();
077:
078: selector.setFactory(localFactory);
079:
080: assertEquals("factory", localFactory, selector.getFactory());
081: }
082:
083: public void testCoverage() {
084: Collection coverage = new ArrayList();
085:
086: ClosureInboundSelector selector = new ClosureInboundSelector();
087:
088: selector.setCoverage(coverage);
089:
090: assertEquals("coverage", coverage, selector.getCoverage());
091: }
092:
093: public void testOneSelectedNode() {
094: ClosureInboundSelector selector = new ClosureInboundSelector(
095: localFactory, Collections.EMPTY_SET);
096: selector.traverseNodes(Collections.singleton(b_B_b));
097:
098: assertEquals("nodes in selection", 1, selector
099: .getSelectedNodes().size());
100: assertEquals("a.A.a in selection", a_A_a, selector
101: .getSelectedNodes().iterator().next());
102: assertSame("a.A.a in selection", a_A_a, selector
103: .getSelectedNodes().iterator().next());
104: }
105:
106: public void testOneCopiedNode() {
107: ClosureInboundSelector selector = new ClosureInboundSelector(
108: localFactory, Collections.EMPTY_SET);
109: selector.traverseNodes(Collections.singleton(b_B_b));
110:
111: assertEquals("packages in scope", 2, localFactory.getPackages()
112: .size());
113: assertEquals("classes in scope", 2, localFactory.getClasses()
114: .size());
115: assertEquals("features in scope", 2, localFactory.getFeatures()
116: .size());
117:
118: assertEquals("package a in scope", a, localFactory
119: .getPackages().get("a"));
120: assertEquals("class a.A in scope", a_A, localFactory
121: .getClasses().get("a.A"));
122: assertEquals("feature a.A.a in scope", a_A_a, localFactory
123: .getFeatures().get("a.A.a"));
124: assertEquals("package b in scope", b, localFactory
125: .getPackages().get("b"));
126: assertEquals("class b.B in scope", b_B, localFactory
127: .getClasses().get("b.B"));
128: assertEquals("feature b.B.b in scope", b_B_b, localFactory
129: .getFeatures().get("b.B.b"));
130:
131: assertNotSame("package a in scope", a, localFactory
132: .getPackages().get("a"));
133: assertNotSame("class a.A in scope", a_A, localFactory
134: .getClasses().get("a.A"));
135: assertNotSame("feature a.A.a in scope", a_A_a, localFactory
136: .getFeatures().get("a.A.a"));
137: assertNotSame("package b in scope", b, localFactory
138: .getPackages().get("b"));
139: assertNotSame("class b.B in scope", b_B, localFactory
140: .getClasses().get("b.B"));
141: assertNotSame("feature b.B.b in scope", b_B_b, localFactory
142: .getFeatures().get("b.B.b"));
143:
144: assertEquals("nodes in selection", 1, selector.getCopiedNodes()
145: .size());
146: assertEquals("a.A.a in selection", a_A_a, selector
147: .getCopiedNodes().iterator().next());
148: assertNotSame("a.A.a in selection", a_A_a, selector
149: .getCopiedNodes().iterator().next());
150: assertSame("a.A.a in selection", localFactory.getFeatures()
151: .get("a.A.a"), selector.getCopiedNodes().iterator()
152: .next());
153: assertEquals("a.A.a's inbounds", 0, ((Node) selector
154: .getCopiedNodes().iterator().next())
155: .getInboundDependencies().size());
156: assertEquals("a.A.a's outbounds", 1, ((Node) selector
157: .getCopiedNodes().iterator().next())
158: .getOutboundDependencies().size());
159: }
160:
161: public void testThreeSelectedNodesFromPackage() {
162: a.addDependency(b);
163: a_A.addDependency(b);
164: a_A_a.addDependency(b);
165:
166: ClosureInboundSelector selector = new ClosureInboundSelector(
167: localFactory, Collections.EMPTY_SET);
168: selector.traverseNodes(Collections.singleton(b));
169:
170: assertEquals("nodes in selection", 3, selector
171: .getSelectedNodes().size());
172: assertTrue("a in selection", selector.getSelectedNodes()
173: .contains(a));
174: assertTrue("a.A in selection", selector.getSelectedNodes()
175: .contains(a_A));
176: assertTrue("a.A.a in selection", selector.getSelectedNodes()
177: .contains(a_A_a));
178: }
179:
180: public void testThreeSelectedNodesFromClass() {
181: a.addDependency(b_B);
182: a_A.addDependency(b_B);
183: a_A_a.addDependency(b_B);
184:
185: ClosureInboundSelector selector = new ClosureInboundSelector(
186: localFactory, Collections.EMPTY_SET);
187: selector.traverseNodes(Collections.singleton(b_B));
188:
189: assertEquals("nodes in selection", 3, selector
190: .getSelectedNodes().size());
191: assertTrue("a in selection", selector.getSelectedNodes()
192: .contains(a));
193: assertTrue("a.A in selection", selector.getSelectedNodes()
194: .contains(a_A));
195: assertTrue("a.A.a in selection", selector.getSelectedNodes()
196: .contains(a_A_a));
197: }
198:
199: public void testThreeSelectedNodesFromFeature() {
200: a.addDependency(b_B_b);
201: a_A.addDependency(b_B_b);
202: a_A_a.addDependency(b_B_b);
203:
204: ClosureInboundSelector selector = new ClosureInboundSelector(
205: localFactory, Collections.EMPTY_SET);
206: selector.traverseNodes(Collections.singleton(b_B_b));
207:
208: assertEquals("nodes in selection", 3, selector
209: .getSelectedNodes().size());
210: assertTrue("a in selection", selector.getSelectedNodes()
211: .contains(a));
212: assertTrue("a.A in selection", selector.getSelectedNodes()
213: .contains(a_A));
214: assertTrue("a.A.a in selection", selector.getSelectedNodes()
215: .contains(a_A_a));
216: }
217:
218: public void testThreeCopiedNodesFromPackage() {
219: a.addDependency(b);
220: a_A.addDependency(b);
221: a_A_a.addDependency(b);
222:
223: ClosureInboundSelector selector = new ClosureInboundSelector(
224: localFactory, Collections.EMPTY_SET);
225: selector.traverseNodes(Collections.singleton(b));
226:
227: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
228: .size());
229: assertTrue("a in selection", selector.getCopiedNodes()
230: .contains(a));
231: assertTrue("a.A in selection", selector.getCopiedNodes()
232: .contains(a_A));
233: assertTrue("a.A.a in selection", selector.getCopiedNodes()
234: .contains(a_A_a));
235:
236: assertEquals("b's inbounds", 3, localFactory.createPackage("b")
237: .getInboundDependencies().size());
238: }
239:
240: public void testThreeCopiedNodesFromClass() {
241: a.addDependency(b_B);
242: a_A.addDependency(b_B);
243: a_A_a.addDependency(b_B);
244:
245: ClosureInboundSelector selector = new ClosureInboundSelector(
246: localFactory, Collections.EMPTY_SET);
247: selector.traverseNodes(Collections.singleton(b_B));
248:
249: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
250: .size());
251: assertTrue("a in selection", selector.getCopiedNodes()
252: .contains(a));
253: assertTrue("a.A in selection", selector.getCopiedNodes()
254: .contains(a_A));
255: assertTrue("a.A.a in selection", selector.getCopiedNodes()
256: .contains(a_A_a));
257:
258: assertEquals("b.B's inbounds", 3, localFactory.createClass(
259: "b.B").getInboundDependencies().size());
260: }
261:
262: public void testThreeCopiedNodesFromFeature() {
263: a.addDependency(b_B_b);
264: a_A.addDependency(b_B_b);
265: a_A_a.addDependency(b_B_b);
266:
267: ClosureInboundSelector selector = new ClosureInboundSelector(
268: localFactory, Collections.EMPTY_SET);
269: selector.traverseNodes(Collections.singleton(b_B_b));
270:
271: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
272: .size());
273: assertTrue("a in selection", selector.getCopiedNodes()
274: .contains(a));
275: assertTrue("a.A in selection", selector.getCopiedNodes()
276: .contains(a_A));
277: assertTrue("a.A.a in selection", selector.getCopiedNodes()
278: .contains(a_A_a));
279:
280: assertEquals("b.B.b's inbounds", 3, localFactory.createFeature(
281: "b.B.b").getInboundDependencies().size());
282: }
283:
284: public void testTwoSelectedNodeWithPackageInCoverage() {
285: a.addDependency(b_B_b);
286: a_A.addDependency(b_B_b);
287: a_A_a.addDependency(b_B_b);
288:
289: ClosureInboundSelector selector = new ClosureInboundSelector(
290: localFactory, Collections.singleton(a));
291: selector.traverseNodes(Collections.singleton(b_B_b));
292:
293: assertEquals("nodes in selection", 2, selector
294: .getSelectedNodes().size());
295: assertTrue("a.A in selection", selector.getSelectedNodes()
296: .contains(a_A));
297: assertTrue("a.A.a in selection", selector.getSelectedNodes()
298: .contains(a_A_a));
299: }
300:
301: public void testTwoSelectedNodeWithClassInCoverage() {
302: a.addDependency(b_B_b);
303: a_A.addDependency(b_B_b);
304: a_A_a.addDependency(b_B_b);
305:
306: ClosureInboundSelector selector = new ClosureInboundSelector(
307: localFactory, Collections.singleton(a_A));
308: selector.traverseNodes(Collections.singleton(b_B_b));
309:
310: assertEquals("nodes in selection", 2, selector
311: .getSelectedNodes().size());
312: assertTrue("a in selection", selector.getSelectedNodes()
313: .contains(a));
314: assertTrue("a.A.a in selection", selector.getSelectedNodes()
315: .contains(a_A_a));
316: }
317:
318: public void testTwoSelectedNodeWithFeatureInCoverage() {
319: a.addDependency(b_B_b);
320: a_A.addDependency(b_B_b);
321: a_A_a.addDependency(b_B_b);
322:
323: ClosureInboundSelector selector = new ClosureInboundSelector(
324: localFactory, Collections.singleton(a_A_a));
325: selector.traverseNodes(Collections.singleton(b_B_b));
326:
327: assertEquals("nodes in selection", 2, selector
328: .getSelectedNodes().size());
329: assertTrue("a in selection", selector.getSelectedNodes()
330: .contains(a));
331: assertTrue("a.A in selection", selector.getSelectedNodes()
332: .contains(a_A));
333: }
334:
335: public void testTwoCopiedNodeWithPackageInCoverage() {
336: a.addDependency(b_B_b);
337: a_A.addDependency(b_B_b);
338: a_A_a.addDependency(b_B_b);
339:
340: ClosureInboundSelector selector = new ClosureInboundSelector(
341: localFactory, Collections.singleton(a));
342: selector.traverseNodes(Collections.singleton(b_B_b));
343:
344: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
345: .size());
346: assertTrue("a.A in selection", selector.getCopiedNodes()
347: .contains(a_A));
348: assertTrue("a.A.a in selection", selector.getCopiedNodes()
349: .contains(a_A_a));
350:
351: assertEquals("b.B.b's inbounds", 2, localFactory.createFeature(
352: "b.B.b").getInboundDependencies().size());
353: }
354:
355: public void testTwoCopiedNodeWithClassInCoverage() {
356: a.addDependency(b_B_b);
357: a_A.addDependency(b_B_b);
358: a_A_a.addDependency(b_B_b);
359:
360: ClosureInboundSelector selector = new ClosureInboundSelector(
361: localFactory, Collections.singleton(a_A));
362: selector.traverseNodes(Collections.singleton(b_B_b));
363:
364: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
365: .size());
366: assertTrue("a in selection", selector.getCopiedNodes()
367: .contains(a));
368: assertTrue("a.A.a in selection", selector.getCopiedNodes()
369: .contains(a_A_a));
370:
371: assertEquals("b.B.b's inbounds", 2, localFactory.createFeature(
372: "b.B.b").getInboundDependencies().size());
373: }
374:
375: public void testTwoCopiedNodeWithFeatureInCoverage() {
376: a.addDependency(b_B_b);
377: a_A.addDependency(b_B_b);
378: a_A_a.addDependency(b_B_b);
379:
380: ClosureInboundSelector selector = new ClosureInboundSelector(
381: localFactory, Collections.singleton(a_A_a));
382: selector.traverseNodes(Collections.singleton(b_B_b));
383:
384: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
385: .size());
386: assertTrue("a in selection", selector.getCopiedNodes()
387: .contains(a));
388: assertTrue("a.A in selection", selector.getCopiedNodes()
389: .contains(a_A));
390:
391: assertEquals("b.B.b's inbounds", 2, localFactory.createFeature(
392: "b.B.b").getInboundDependencies().size());
393: }
394:
395: public void testReset() {
396: ClosureInboundSelector selector = new ClosureInboundSelector(
397: localFactory, Collections.EMPTY_SET);
398: selector.traverseNodes(Collections.singleton(b_B_b));
399:
400: assertEquals("nodes in selection", 1, selector
401: .getSelectedNodes().size());
402: assertEquals("copied nodes", 1, selector.getCopiedNodes()
403: .size());
404:
405: selector.reset();
406:
407: assertEquals("nodes in selection", 0, selector
408: .getSelectedNodes().size());
409: assertEquals("copied nodes", 0, selector.getCopiedNodes()
410: .size());
411: }
412:
413: public void testVisitInferredPackage() {
414: ClosureInboundSelector selector = new ClosureInboundSelector(
415: localFactory, Collections.EMPTY_SET);
416: selector.traverseNodes(Collections.singleton(b));
417:
418: assertEquals("package.isConfirmed()", b.isConfirmed(),
419: ((Node) localFactory.getPackages().get(b.getName()))
420: .isConfirmed());
421: }
422:
423: public void testVisitConfirmedPackage() {
424: b.setConfirmed(true);
425:
426: ClosureInboundSelector selector = new ClosureInboundSelector(
427: localFactory, Collections.EMPTY_SET);
428: selector.traverseNodes(Collections.singleton(b));
429:
430: assertEquals("package.isConfirmed()", b.isConfirmed(),
431: ((Node) localFactory.getPackages().get(b.getName()))
432: .isConfirmed());
433: }
434:
435: public void testFollowToInferredPackage() {
436: a.addDependency(b);
437:
438: ClosureInboundSelector selector = new ClosureInboundSelector(
439: localFactory, Collections.EMPTY_SET);
440: selector.traverseNodes(Collections.singleton(b));
441:
442: assertEquals("package.isConfirmed()", a.isConfirmed(),
443: ((Node) localFactory.getPackages().get(a.getName()))
444: .isConfirmed());
445: }
446:
447: public void testFollowToConfirmedPackage() {
448: a.addDependency(b);
449: a.setConfirmed(true);
450:
451: ClosureInboundSelector selector = new ClosureInboundSelector(
452: localFactory, Collections.EMPTY_SET);
453: selector.traverseNodes(Collections.singleton(b));
454:
455: assertEquals("package.isConfirmed()", a.isConfirmed(),
456: ((Node) localFactory.getPackages().get(a.getName()))
457: .isConfirmed());
458: }
459:
460: public void testVisitInferredClass() {
461: ClosureInboundSelector selector = new ClosureInboundSelector(
462: localFactory, Collections.EMPTY_SET);
463: selector.traverseNodes(Collections.singleton(b_B));
464:
465: assertEquals("class.isConfirmed()", b_B.isConfirmed(),
466: ((Node) localFactory.getClasses().get(b_B.getName()))
467: .isConfirmed());
468: }
469:
470: public void testVisitConfirmedClass() {
471: b_B.setConfirmed(true);
472:
473: ClosureInboundSelector selector = new ClosureInboundSelector(
474: localFactory, Collections.EMPTY_SET);
475: selector.traverseNodes(Collections.singleton(b_B));
476:
477: assertEquals("class.isConfirmed()", b_B.isConfirmed(),
478: ((Node) localFactory.getClasses().get(b_B.getName()))
479: .isConfirmed());
480: }
481:
482: public void testFollowToInferredClass() {
483: a_A.addDependency(b_B);
484:
485: ClosureInboundSelector selector = new ClosureInboundSelector(
486: localFactory, Collections.EMPTY_SET);
487: selector.traverseNodes(Collections.singleton(b_B));
488:
489: assertEquals("class.isConfirmed()", a_A.isConfirmed(),
490: ((Node) localFactory.getClasses().get(a_A.getName()))
491: .isConfirmed());
492: }
493:
494: public void testFollowToConfirmedClass() {
495: a_A.addDependency(b_B);
496: a_A.setConfirmed(true);
497:
498: ClosureInboundSelector selector = new ClosureInboundSelector(
499: localFactory, Collections.EMPTY_SET);
500: selector.traverseNodes(Collections.singleton(b_B));
501:
502: assertEquals("class.isConfirmed()", a_A.isConfirmed(),
503: ((Node) localFactory.getClasses().get(a_A.getName()))
504: .isConfirmed());
505: }
506:
507: public void testVisitInferredFeature() {
508: ClosureInboundSelector selector = new ClosureInboundSelector(
509: localFactory, Collections.EMPTY_SET);
510: selector.traverseNodes(Collections.singleton(b_B_b));
511:
512: assertEquals(
513: "feature.isConfirmed()",
514: b_B_b.isConfirmed(),
515: ((Node) localFactory.getFeatures().get(b_B_b.getName()))
516: .isConfirmed());
517: }
518:
519: public void testVisitConfirmedFeature() {
520: b_B_b.setConfirmed(true);
521:
522: ClosureInboundSelector selector = new ClosureInboundSelector(
523: localFactory, Collections.EMPTY_SET);
524: selector.traverseNodes(Collections.singleton(b_B_b));
525:
526: assertEquals(
527: "feature.isConfirmed()",
528: b_B_b.isConfirmed(),
529: ((Node) localFactory.getFeatures().get(b_B_b.getName()))
530: .isConfirmed());
531: }
532:
533: public void testFollowToInferredFeature() {
534: a_A_a.addDependency(b_B_b);
535:
536: ClosureInboundSelector selector = new ClosureInboundSelector(
537: localFactory, Collections.EMPTY_SET);
538: selector.traverseNodes(Collections.singleton(b_B_b));
539:
540: assertEquals(
541: "feature.isConfirmed()",
542: a_A_a.isConfirmed(),
543: ((Node) localFactory.getFeatures().get(a_A_a.getName()))
544: .isConfirmed());
545: }
546:
547: public void testFollowToConfirmedFeature() {
548: a_A_a.addDependency(b_B_b);
549: a_A_a.setConfirmed(true);
550:
551: ClosureInboundSelector selector = new ClosureInboundSelector(
552: localFactory, Collections.EMPTY_SET);
553: selector.traverseNodes(Collections.singleton(b_B_b));
554:
555: assertEquals(
556: "feature.isConfirmed()",
557: a_A_a.isConfirmed(),
558: ((Node) localFactory.getFeatures().get(a_A_a.getName()))
559: .isConfirmed());
560: }
561: }
|