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.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: import org.apache.oro.text.perl.*;
041:
042: public class TestClosureOutboundSelector extends TestCase {
043: private NodeFactory factory;
044: private NodeFactory localFactory;
045:
046: private PackageNode a;
047: private ClassNode a_A;
048: private FeatureNode a_A_a;
049:
050: private PackageNode b;
051: private ClassNode b_B;
052: private FeatureNode b_B_b;
053:
054: private PackageNode c;
055: private ClassNode c_C;
056: private FeatureNode c_C_c;
057:
058: protected void setUp() throws Exception {
059: factory = new NodeFactory();
060: localFactory = new NodeFactory();
061:
062: a = factory.createPackage("a");
063: a_A = factory.createClass("a.A");
064: a_A_a = factory.createFeature("a.A.a");
065:
066: b = factory.createPackage("b");
067: b_B = factory.createClass("b.B");
068: b_B_b = factory.createFeature("b.B.b");
069:
070: c = factory.createPackage("c");
071: c_C = factory.createClass("c.C");
072: c_C_c = factory.createFeature("c.C.c");
073:
074: a_A_a.addDependency(b_B_b);
075: b_B_b.addDependency(c_C_c);
076: }
077:
078: public void testFactory() {
079: ClosureOutboundSelector selector = new ClosureOutboundSelector();
080:
081: selector.setFactory(localFactory);
082:
083: assertEquals("factory", localFactory, selector.getFactory());
084: }
085:
086: public void testCoverage() {
087: Collection coverage = new ArrayList();
088:
089: ClosureOutboundSelector selector = new ClosureOutboundSelector();
090:
091: selector.setCoverage(coverage);
092:
093: assertEquals("coverage", coverage, selector.getCoverage());
094: }
095:
096: public void testOneSelectedNode() {
097: ClosureOutboundSelector selector = new ClosureOutboundSelector(
098: localFactory, Collections.EMPTY_SET);
099: selector.traverseNodes(Collections.singleton(b_B_b));
100:
101: assertEquals("nodes in selection", 1, selector
102: .getSelectedNodes().size());
103: assertEquals("c.C.c in selection", c_C_c, selector
104: .getSelectedNodes().iterator().next());
105: assertSame("c.C.c in selection", c_C_c, selector
106: .getSelectedNodes().iterator().next());
107: }
108:
109: public void testOneCopiedNode() {
110: ClosureOutboundSelector selector = new ClosureOutboundSelector(
111: localFactory, Collections.EMPTY_SET);
112: selector.traverseNodes(Collections.singleton(b_B_b));
113:
114: assertEquals("packages in scope", 2, localFactory.getPackages()
115: .size());
116: assertEquals("classes in scope", 2, localFactory.getClasses()
117: .size());
118: assertEquals("features in scope", 2, localFactory.getFeatures()
119: .size());
120:
121: assertEquals("package b in scope", b, localFactory
122: .getPackages().get("b"));
123: assertEquals("class b.B in scope", b_B, localFactory
124: .getClasses().get("b.B"));
125: assertEquals("feature b.B.b in scope", b_B_b, localFactory
126: .getFeatures().get("b.B.b"));
127: assertEquals("package c in scope", c, localFactory
128: .getPackages().get("c"));
129: assertEquals("class c.C in scope", c_C, localFactory
130: .getClasses().get("c.C"));
131: assertEquals("feature c.C.c in scope", c_C_c, localFactory
132: .getFeatures().get("c.C.c"));
133:
134: assertNotSame("package b in scope", b, localFactory
135: .getPackages().get("b"));
136: assertNotSame("class b.B in scope", b_B, localFactory
137: .getClasses().get("b.B"));
138: assertNotSame("feature b.B.b in scope", b_B_b, localFactory
139: .getFeatures().get("b.B.b"));
140: assertNotSame("package c in scope", c, localFactory
141: .getPackages().get("c"));
142: assertNotSame("class c.C in scope", c_C, localFactory
143: .getClasses().get("c.C"));
144: assertNotSame("feature c.C.c in scope", c_C_c, localFactory
145: .getFeatures().get("c.C.c"));
146:
147: assertEquals("nodes in selection", 1, selector.getCopiedNodes()
148: .size());
149: assertEquals("c.C.c in selection", c_C_c, selector
150: .getCopiedNodes().iterator().next());
151: assertNotSame("c.C.c in selection", c_C_c, selector
152: .getCopiedNodes().iterator().next());
153: assertSame("c.C.c in selection", localFactory.getFeatures()
154: .get("c.C.c"), selector.getCopiedNodes().iterator()
155: .next());
156: assertEquals("c.C.c's inbounds", 1, ((Node) selector
157: .getCopiedNodes().iterator().next())
158: .getInboundDependencies().size());
159: assertEquals("c.C.c's outbounds", 0, ((Node) selector
160: .getCopiedNodes().iterator().next())
161: .getOutboundDependencies().size());
162: }
163:
164: public void testThreeSelectedNodesFromPackage() {
165: b.addDependency(c);
166: b.addDependency(c_C);
167: b.addDependency(c_C_c);
168:
169: ClosureOutboundSelector selector = new ClosureOutboundSelector(
170: localFactory, Collections.EMPTY_SET);
171: selector.traverseNodes(Collections.singleton(b));
172:
173: assertEquals("nodes in selection", 3, selector
174: .getSelectedNodes().size());
175: assertTrue("c in selection", selector.getSelectedNodes()
176: .contains(c));
177: assertTrue("c.C in selection", selector.getSelectedNodes()
178: .contains(c_C));
179: assertTrue("c.C.c in selection", selector.getSelectedNodes()
180: .contains(c_C_c));
181: }
182:
183: public void testThreeSelectedNodesFromClass() {
184: b_B.addDependency(c);
185: b_B.addDependency(c_C);
186: b_B.addDependency(c_C_c);
187:
188: ClosureOutboundSelector selector = new ClosureOutboundSelector(
189: localFactory, Collections.EMPTY_SET);
190: selector.traverseNodes(Collections.singleton(b_B));
191:
192: assertEquals("nodes in selection", 3, selector
193: .getSelectedNodes().size());
194: assertTrue("c in selection", selector.getSelectedNodes()
195: .contains(c));
196: assertTrue("c.C in selection", selector.getSelectedNodes()
197: .contains(c_C));
198: assertTrue("c.C.c in selection", selector.getSelectedNodes()
199: .contains(c_C_c));
200: }
201:
202: public void testThreeSelectedNodesFromFeature() {
203: b_B_b.addDependency(c);
204: b_B_b.addDependency(c_C);
205: b_B_b.addDependency(c_C_c);
206:
207: ClosureOutboundSelector selector = new ClosureOutboundSelector(
208: localFactory, Collections.EMPTY_SET);
209: selector.traverseNodes(Collections.singleton(b_B_b));
210:
211: assertEquals("nodes in selection", 3, selector
212: .getSelectedNodes().size());
213: assertTrue("c in selection", selector.getSelectedNodes()
214: .contains(c));
215: assertTrue("c.C in selection", selector.getSelectedNodes()
216: .contains(c_C));
217: assertTrue("c.C.c in selection", selector.getSelectedNodes()
218: .contains(c_C_c));
219: }
220:
221: public void testThreeCopiedNodesFromPackage() {
222: b.addDependency(c);
223: b.addDependency(c_C);
224: b.addDependency(c_C_c);
225:
226: ClosureOutboundSelector selector = new ClosureOutboundSelector(
227: localFactory, Collections.EMPTY_SET);
228: selector.traverseNodes(Collections.singleton(b));
229:
230: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
231: .size());
232: assertTrue("c in selection", selector.getCopiedNodes()
233: .contains(c));
234: assertTrue("c.C in selection", selector.getCopiedNodes()
235: .contains(c_C));
236: assertTrue("c.C.c in selection", selector.getCopiedNodes()
237: .contains(c_C_c));
238:
239: assertEquals("b's outbounds", 3, localFactory
240: .createPackage("b").getOutboundDependencies().size());
241: }
242:
243: public void testThreeCopiedNodesFromClass() {
244: b_B.addDependency(c);
245: b_B.addDependency(c_C);
246: b_B.addDependency(c_C_c);
247:
248: ClosureOutboundSelector selector = new ClosureOutboundSelector(
249: localFactory, Collections.EMPTY_SET);
250: selector.traverseNodes(Collections.singleton(b_B));
251:
252: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
253: .size());
254: assertTrue("c in selection", selector.getCopiedNodes()
255: .contains(c));
256: assertTrue("c.C in selection", selector.getCopiedNodes()
257: .contains(c_C));
258: assertTrue("c.C.c in selection", selector.getCopiedNodes()
259: .contains(c_C_c));
260:
261: assertEquals("b.B's outbounds", 3, localFactory.createClass(
262: "b.B").getOutboundDependencies().size());
263: }
264:
265: public void testThreeCopiedNodesFromFeature() {
266: b_B_b.addDependency(c);
267: b_B_b.addDependency(c_C);
268: b_B_b.addDependency(c_C_c);
269:
270: ClosureOutboundSelector selector = new ClosureOutboundSelector(
271: localFactory, Collections.EMPTY_SET);
272: selector.traverseNodes(Collections.singleton(b_B_b));
273:
274: assertEquals("nodes in selection", 3, selector.getCopiedNodes()
275: .size());
276: assertTrue("c in selection", selector.getCopiedNodes()
277: .contains(c));
278: assertTrue("c.C in selection", selector.getCopiedNodes()
279: .contains(c_C));
280: assertTrue("c.C.c in selection", selector.getCopiedNodes()
281: .contains(c_C_c));
282:
283: assertEquals("b.B.b's outbounds", 3, localFactory
284: .createFeature("b.B.b").getOutboundDependencies()
285: .size());
286: }
287:
288: public void testTwoSelectedNodeWithPackageInCoverage() {
289: b_B_b.addDependency(c);
290: b_B_b.addDependency(c_C);
291: b_B_b.addDependency(c_C_c);
292:
293: ClosureOutboundSelector selector = new ClosureOutboundSelector(
294: localFactory, Collections.singleton(c));
295: selector.traverseNodes(Collections.singleton(b_B_b));
296:
297: assertEquals("nodes in selection", 2, selector
298: .getSelectedNodes().size());
299: assertTrue("c.C in selection", selector.getSelectedNodes()
300: .contains(c_C));
301: assertTrue("c.C.c in selection", selector.getSelectedNodes()
302: .contains(c_C_c));
303: }
304:
305: public void testTwoSelectedNodeWithClassInCoverage() {
306: b_B_b.addDependency(c);
307: b_B_b.addDependency(c_C);
308: b_B_b.addDependency(c_C_c);
309:
310: ClosureOutboundSelector selector = new ClosureOutboundSelector(
311: localFactory, Collections.singleton(c_C));
312: selector.traverseNodes(Collections.singleton(b_B_b));
313:
314: assertEquals("nodes in selection", 2, selector
315: .getSelectedNodes().size());
316: assertTrue("c in selection", selector.getSelectedNodes()
317: .contains(c));
318: assertTrue("c.C.c in selection", selector.getSelectedNodes()
319: .contains(c_C_c));
320: }
321:
322: public void testTwoSelectedNodeWithFeatureInCoverage() {
323: b_B_b.addDependency(c);
324: b_B_b.addDependency(c_C);
325: b_B_b.addDependency(c_C_c);
326:
327: ClosureOutboundSelector selector = new ClosureOutboundSelector(
328: localFactory, Collections.singleton(c_C_c));
329: selector.traverseNodes(Collections.singleton(b_B_b));
330:
331: assertEquals("nodes in selection", 2, selector
332: .getSelectedNodes().size());
333: assertTrue("c in selection", selector.getSelectedNodes()
334: .contains(c));
335: assertTrue("c.C in selection", selector.getSelectedNodes()
336: .contains(c_C));
337: }
338:
339: public void testTwoCopiedNodeWithPackageInCoverage() {
340: b_B_b.addDependency(c);
341: b_B_b.addDependency(c_C);
342: b_B_b.addDependency(c_C_c);
343:
344: ClosureOutboundSelector selector = new ClosureOutboundSelector(
345: localFactory, Collections.singleton(c));
346: selector.traverseNodes(Collections.singleton(b_B_b));
347:
348: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
349: .size());
350: assertTrue("c.C in selection", selector.getCopiedNodes()
351: .contains(c_C));
352: assertTrue("c.C.c in selection", selector.getCopiedNodes()
353: .contains(c_C_c));
354:
355: assertEquals("b.B.b's outbounds", 2, localFactory
356: .createFeature("b.B.b").getOutboundDependencies()
357: .size());
358: }
359:
360: public void testTwoCopiedNodeWithClassInCoverage() {
361: b_B_b.addDependency(c);
362: b_B_b.addDependency(c_C);
363: b_B_b.addDependency(c_C_c);
364:
365: ClosureOutboundSelector selector = new ClosureOutboundSelector(
366: localFactory, Collections.singleton(c_C));
367: selector.traverseNodes(Collections.singleton(b_B_b));
368:
369: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
370: .size());
371: assertTrue("c in selection", selector.getCopiedNodes()
372: .contains(c));
373: assertTrue("c.C.c in selection", selector.getCopiedNodes()
374: .contains(c_C_c));
375:
376: assertEquals("b.B.b's outbounds", 2, localFactory
377: .createFeature("b.B.b").getOutboundDependencies()
378: .size());
379: }
380:
381: public void testTwoCopiedNodeWithFeatureInCoverage() {
382: b_B_b.addDependency(c);
383: b_B_b.addDependency(c_C);
384: b_B_b.addDependency(c_C_c);
385:
386: ClosureOutboundSelector selector = new ClosureOutboundSelector(
387: localFactory, Collections.singleton(c_C_c));
388: selector.traverseNodes(Collections.singleton(b_B_b));
389:
390: assertEquals("nodes in selection", 2, selector.getCopiedNodes()
391: .size());
392: assertTrue("c in selection", selector.getCopiedNodes()
393: .contains(c));
394: assertTrue("c.C in selection", selector.getCopiedNodes()
395: .contains(c_C));
396:
397: assertEquals("b.B.b's outbounds", 2, localFactory
398: .createFeature("b.B.b").getOutboundDependencies()
399: .size());
400: }
401:
402: public void testReset() {
403: NodeFactory localFactory = new NodeFactory();
404:
405: ClosureOutboundSelector selector = new ClosureOutboundSelector(
406: localFactory, Collections.EMPTY_SET);
407: selector.traverseNodes(Collections.singleton(b_B_b));
408:
409: assertEquals("nodes in selection", 1, selector
410: .getSelectedNodes().size());
411: assertEquals("copied nodes", 1, selector.getCopiedNodes()
412: .size());
413:
414: selector.reset();
415:
416: assertEquals("nodes in selection", 0, selector
417: .getSelectedNodes().size());
418: assertEquals("copied nodes", 0, selector.getCopiedNodes()
419: .size());
420: }
421:
422: public void testVisitInferredPackage() {
423: ClosureOutboundSelector selector = new ClosureOutboundSelector(
424: localFactory, Collections.EMPTY_SET);
425: selector.traverseNodes(Collections.singleton(b));
426:
427: assertEquals("package.isConfirmed()", b.isConfirmed(),
428: ((Node) localFactory.getPackages().get(b.getName()))
429: .isConfirmed());
430: }
431:
432: public void testVisitConfirmedPackage() {
433: b.setConfirmed(true);
434:
435: ClosureOutboundSelector selector = new ClosureOutboundSelector(
436: localFactory, Collections.EMPTY_SET);
437: selector.traverseNodes(Collections.singleton(b));
438:
439: assertEquals("package.isConfirmed()", b.isConfirmed(),
440: ((Node) localFactory.getPackages().get(b.getName()))
441: .isConfirmed());
442: }
443:
444: public void testFollowToInferredPackage() {
445: b.addDependency(c);
446:
447: ClosureOutboundSelector selector = new ClosureOutboundSelector(
448: localFactory, Collections.EMPTY_SET);
449: selector.traverseNodes(Collections.singleton(b));
450:
451: assertEquals("package.isConfirmed()", c.isConfirmed(),
452: ((Node) localFactory.getPackages().get(c.getName()))
453: .isConfirmed());
454: }
455:
456: public void testFollowToConfirmedPackage() {
457: b.addDependency(c);
458: c.setConfirmed(true);
459:
460: ClosureOutboundSelector selector = new ClosureOutboundSelector(
461: localFactory, Collections.EMPTY_SET);
462: selector.traverseNodes(Collections.singleton(b));
463:
464: assertEquals("package.isConfirmed()", c.isConfirmed(),
465: ((Node) localFactory.getPackages().get(c.getName()))
466: .isConfirmed());
467: }
468:
469: public void testVisitInferredClass() {
470: ClosureOutboundSelector selector = new ClosureOutboundSelector(
471: localFactory, Collections.EMPTY_SET);
472: selector.traverseNodes(Collections.singleton(b_B));
473:
474: assertEquals("class.isConfirmed()", b_B.isConfirmed(),
475: ((Node) localFactory.getClasses().get(b_B.getName()))
476: .isConfirmed());
477: }
478:
479: public void testVisitConfirmedClass() {
480: b_B.setConfirmed(true);
481:
482: ClosureOutboundSelector selector = new ClosureOutboundSelector(
483: localFactory, Collections.EMPTY_SET);
484: selector.traverseNodes(Collections.singleton(b_B));
485:
486: assertEquals("class.isConfirmed()", b_B.isConfirmed(),
487: ((Node) localFactory.getClasses().get(b_B.getName()))
488: .isConfirmed());
489: }
490:
491: public void testFollowToInferredClass() {
492: b_B.addDependency(c_C);
493:
494: ClosureOutboundSelector selector = new ClosureOutboundSelector(
495: localFactory, Collections.EMPTY_SET);
496: selector.traverseNodes(Collections.singleton(b_B));
497:
498: assertEquals("class.isConfirmed()", c_C.isConfirmed(),
499: ((Node) localFactory.getClasses().get(c_C.getName()))
500: .isConfirmed());
501: }
502:
503: public void testFollowToConfirmedClass() {
504: b_B.addDependency(c_C);
505: c_C.setConfirmed(true);
506:
507: ClosureOutboundSelector selector = new ClosureOutboundSelector(
508: localFactory, Collections.EMPTY_SET);
509: selector.traverseNodes(Collections.singleton(b_B));
510:
511: assertEquals("class.isConfirmed()", c_C.isConfirmed(),
512: ((Node) localFactory.getClasses().get(c_C.getName()))
513: .isConfirmed());
514: }
515:
516: public void testVisitInferredFeature() {
517: ClosureOutboundSelector selector = new ClosureOutboundSelector(
518: localFactory, Collections.EMPTY_SET);
519: selector.traverseNodes(Collections.singleton(b_B_b));
520:
521: assertEquals(
522: "feature.isConfirmed()",
523: b_B_b.isConfirmed(),
524: ((Node) localFactory.getFeatures().get(b_B_b.getName()))
525: .isConfirmed());
526: }
527:
528: public void testVisitConfirmedFeature() {
529: b_B_b.setConfirmed(true);
530:
531: ClosureOutboundSelector selector = new ClosureOutboundSelector(
532: localFactory, Collections.EMPTY_SET);
533: selector.traverseNodes(Collections.singleton(b_B_b));
534:
535: assertEquals(
536: "feature.isConfirmed()",
537: b_B_b.isConfirmed(),
538: ((Node) localFactory.getFeatures().get(b_B_b.getName()))
539: .isConfirmed());
540: }
541:
542: public void testFollowToInferredFeature() {
543: b_B_b.addDependency(c_C_c);
544:
545: ClosureOutboundSelector selector = new ClosureOutboundSelector(
546: localFactory, Collections.EMPTY_SET);
547: selector.traverseNodes(Collections.singleton(b_B_b));
548:
549: assertEquals(
550: "feature.isConfirmed()",
551: c_C_c.isConfirmed(),
552: ((Node) localFactory.getFeatures().get(c_C_c.getName()))
553: .isConfirmed());
554: }
555:
556: public void testFollowToConfirmedFeature() {
557: b_B_b.addDependency(c_C_c);
558: c_C_c.setConfirmed(true);
559:
560: ClosureOutboundSelector selector = new ClosureOutboundSelector(
561: localFactory, Collections.EMPTY_SET);
562: selector.traverseNodes(Collections.singleton(b_B_b));
563:
564: assertEquals(
565: "feature.isConfirmed()",
566: c_C_c.isConfirmed(),
567: ((Node) localFactory.getFeatures().get(c_C_c.getName()))
568: .isConfirmed());
569: }
570: }
|