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 TestCycleDetector extends TestCase {
040: private NodeFactory factory;
041:
042: private Node a_package;
043: private Node a_A_a_feature;
044:
045: private Node b_package;
046: private Node b_B_b_feature;
047:
048: private Node c_package;
049: private Node d_package;
050: private Node e_package;
051:
052: private CycleDetector detector;
053:
054: protected void setUp() throws Exception {
055: factory = new NodeFactory();
056:
057: a_package = factory.createPackage("a");
058: a_A_a_feature = factory.createFeature("a.A.a");
059:
060: b_package = factory.createPackage("b");
061: b_B_b_feature = factory.createFeature("b.B.b");
062:
063: c_package = factory.createPackage("c");
064: d_package = factory.createPackage("d");
065: e_package = factory.createPackage("e");
066:
067: detector = new CycleDetector();
068: }
069:
070: public void testNoDependencies() {
071: detector.traverseNodes(factory.getPackages().values());
072: assertEquals("Nb cycles", 0, detector.getCycles().size());
073: }
074:
075: public void testNoCycles() {
076: a_package.addDependency(b_package);
077: detector.traverseNodes(factory.getPackages().values());
078: assertEquals("Nb cycles", 0, detector.getCycles().size());
079: }
080:
081: public void testOneLength2PackageCycle() {
082: a_package.addDependency(b_package);
083: b_package.addDependency(a_package);
084: detector.traverseNodes(factory.getPackages().values());
085: assertEquals("Nb cycles", 1, detector.getCycles().size());
086:
087: Iterator cycles = detector.getCycles().iterator();
088:
089: Cycle cycle = (Cycle) cycles.next();
090: assertEquals("cycle length", 2, cycle.getLength());
091: Iterator i = cycle.getPath().iterator();
092: assertEquals("a", a_package, i.next());
093: assertEquals("b", b_package, i.next());
094: }
095:
096: public void testOneLength3PackageCycle() {
097: a_package.addDependency(b_package);
098: b_package.addDependency(c_package);
099: c_package.addDependency(a_package);
100: detector.traverseNodes(factory.getPackages().values());
101: assertEquals("Nb cycles", 1, detector.getCycles().size());
102:
103: Iterator cycles = detector.getCycles().iterator();
104:
105: Cycle cycle = (Cycle) cycles.next();
106: assertEquals("cycle length", 3, cycle.getLength());
107: Iterator i = cycle.getPath().iterator();
108: assertEquals("a", a_package, i.next());
109: assertEquals("b", b_package, i.next());
110: assertEquals("c", c_package, i.next());
111: }
112:
113: public void testTwoLength3PackageCycles() {
114: a_package.addDependency(b_package);
115: b_package.addDependency(c_package);
116: c_package.addDependency(a_package);
117: c_package.addDependency(d_package);
118: d_package.addDependency(e_package);
119: e_package.addDependency(c_package);
120: detector.traverseNodes(factory.getPackages().values());
121: assertEquals("Nb cycles", 2, detector.getCycles().size());
122:
123: Iterator cycles = detector.getCycles().iterator();
124:
125: Cycle cycle;
126: Iterator i;
127:
128: cycle = (Cycle) cycles.next();
129: assertEquals("cycle length", 3, cycle.getLength());
130: i = cycle.getPath().iterator();
131: assertEquals("a", a_package, i.next());
132: assertEquals("b", b_package, i.next());
133: assertEquals("c", c_package, i.next());
134:
135: cycle = (Cycle) cycles.next();
136: assertEquals("cycle length", 3, cycle.getLength());
137: i = cycle.getPath().iterator();
138: assertEquals("c", c_package, i.next());
139: assertEquals("d", d_package, i.next());
140: assertEquals("e", e_package, i.next());
141: }
142:
143: public void testOneLength2AndOneLength3PackageCycles() {
144: a_package.addDependency(b_package);
145: b_package.addDependency(a_package);
146: c_package.addDependency(d_package);
147: d_package.addDependency(e_package);
148: e_package.addDependency(c_package);
149: detector.traverseNodes(factory.getPackages().values());
150: assertEquals("Nb cycles", 2, detector.getCycles().size());
151:
152: Iterator cycles = detector.getCycles().iterator();
153:
154: Cycle cycle;
155: Iterator i;
156:
157: cycle = (Cycle) cycles.next();
158: assertEquals("cycle length", 2, cycle.getLength());
159: i = cycle.getPath().iterator();
160: assertEquals("a", a_package, i.next());
161: assertEquals("b", b_package, i.next());
162:
163: cycle = (Cycle) cycles.next();
164: assertEquals("cycle length", 3, cycle.getLength());
165: i = cycle.getPath().iterator();
166: assertEquals("c", c_package, i.next());
167: assertEquals("d", d_package, i.next());
168: assertEquals("e", e_package, i.next());
169: }
170:
171: public void testMaximumLength() {
172: a_package.addDependency(b_package);
173: b_package.addDependency(a_package);
174: c_package.addDependency(d_package);
175: d_package.addDependency(e_package);
176: e_package.addDependency(c_package);
177: detector.setMaximumCycleLength(2);
178: detector.traverseNodes(factory.getPackages().values());
179: assertEquals("Nb cycles", 1, detector.getCycles().size());
180:
181: Iterator cycles = detector.getCycles().iterator();
182:
183: Cycle cycle = (Cycle) cycles.next();
184: assertEquals("cycle length", 2, cycle.getLength());
185: Iterator i = cycle.getPath().iterator();
186: assertEquals("a", a_package, i.next());
187: assertEquals("b", b_package, i.next());
188: }
189:
190: public void testOneLength2FeatureCycle() {
191: a_A_a_feature.addDependency(b_B_b_feature);
192: b_B_b_feature.addDependency(a_A_a_feature);
193: detector.traverseNodes(factory.getPackages().values());
194: assertEquals("Nb cycles", 1, detector.getCycles().size());
195:
196: Iterator cycles = detector.getCycles().iterator();
197:
198: Cycle cycle = (Cycle) cycles.next();
199: assertEquals("cycle length", 2, cycle.getLength());
200: Iterator i = cycle.getPath().iterator();
201: assertEquals("a.A.a", a_A_a_feature, i.next());
202: assertEquals("b.B.b", b_B_b_feature, i.next());
203: }
204: }
|