001: package jdepend.framework;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: /**
008: * @author <b>Mike Clark</b>
009: * @author Clarkware Consulting, Inc.
010: */
011:
012: public class CycleTest extends JDependTestCase {
013:
014: public CycleTest(String name) {
015: super (name);
016: }
017:
018: public void testNoCycles() {
019:
020: JavaPackage a = new JavaPackage("A");
021: JavaPackage b = new JavaPackage("B");
022:
023: a.dependsUpon(b);
024:
025: List aCycles = new ArrayList();
026: assertEquals(false, a.containsCycle());
027: assertEquals(false, a.collectCycle(aCycles));
028: assertListEquals(aCycles, new String[] {});
029:
030: List bCycles = new ArrayList();
031: assertEquals(false, b.containsCycle());
032: assertEquals(false, b.collectCycle(bCycles));
033: assertListEquals(bCycles, new String[] {});
034: }
035:
036: public void test2Node1BranchCycle() {
037:
038: JavaPackage a = new JavaPackage("A");
039: JavaPackage b = new JavaPackage("B");
040:
041: a.dependsUpon(b);
042: b.dependsUpon(a);
043:
044: List aCycles = new ArrayList();
045: assertEquals(true, a.containsCycle());
046: assertEquals(true, a.collectCycle(aCycles));
047: assertListEquals(aCycles, new String[] { "A", "B", "A" });
048:
049: List bCycles = new ArrayList();
050: assertEquals(true, b.containsCycle());
051: assertEquals(true, b.collectCycle(bCycles));
052: assertListEquals(bCycles, new String[] { "B", "A", "B" });
053: }
054:
055: public void test3Node1BranchCycle() {
056:
057: JavaPackage a = new JavaPackage("A");
058: JavaPackage b = new JavaPackage("B");
059: JavaPackage c = new JavaPackage("C");
060:
061: a.dependsUpon(b);
062: b.dependsUpon(c);
063: c.dependsUpon(a);
064:
065: List aCycles = new ArrayList();
066: assertEquals(true, a.containsCycle());
067: assertEquals(true, a.collectCycle(aCycles));
068: assertListEquals(aCycles, new String[] { "A", "B", "C", "A" });
069:
070: List bCycles = new ArrayList();
071: assertEquals(true, b.containsCycle());
072: assertEquals(true, b.collectCycle(bCycles));
073: assertListEquals(bCycles, new String[] { "B", "C", "A", "B" });
074:
075: List cCycles = new ArrayList();
076: assertEquals(true, c.containsCycle());
077: assertEquals(true, c.collectCycle(cCycles));
078: assertListEquals(cCycles, new String[] { "C", "A", "B", "C" });
079: }
080:
081: public void test3Node1BranchSubCycle() {
082:
083: JavaPackage a = new JavaPackage("A");
084: JavaPackage b = new JavaPackage("B");
085: JavaPackage c = new JavaPackage("C");
086:
087: a.dependsUpon(b);
088: b.dependsUpon(c);
089: c.dependsUpon(b);
090:
091: List aCycles = new ArrayList();
092: assertEquals(true, a.containsCycle());
093: assertEquals(true, a.collectCycle(aCycles));
094: assertListEquals(aCycles, new String[] { "A", "B", "C", "B" });
095:
096: List bCycles = new ArrayList();
097: assertEquals(true, b.containsCycle());
098: assertEquals(true, b.collectCycle(bCycles));
099: assertListEquals(bCycles, new String[] { "B", "C", "B" });
100:
101: List cCycles = new ArrayList();
102: assertEquals(true, c.containsCycle());
103: assertEquals(true, c.collectCycle(cCycles));
104: assertListEquals(cCycles, new String[] { "C", "B", "C" });
105: }
106:
107: public void test3Node2BranchCycle() {
108:
109: JavaPackage a = new JavaPackage("A");
110: JavaPackage b = new JavaPackage("B");
111: JavaPackage c = new JavaPackage("C");
112:
113: a.dependsUpon(b);
114: b.dependsUpon(a);
115:
116: a.dependsUpon(c);
117: c.dependsUpon(a);
118:
119: List aCycles = new ArrayList();
120: assertEquals(true, a.containsCycle());
121: assertEquals(true, a.collectCycle(aCycles));
122: assertListEquals(aCycles, new String[] { "A", "B", "A" });
123:
124: List bCycles = new ArrayList();
125: assertEquals(true, b.containsCycle());
126: assertEquals(true, b.collectCycle(bCycles));
127: assertListEquals(bCycles, new String[] { "B", "A", "B" });
128:
129: List cCycles = new ArrayList();
130: assertEquals(true, c.containsCycle());
131: assertEquals(true, c.collectCycle(cCycles));
132: assertListEquals(cCycles, new String[] { "C", "A", "B", "A" });
133: }
134:
135: public void test5Node2BranchCycle() {
136:
137: JavaPackage a = new JavaPackage("A");
138: JavaPackage b = new JavaPackage("B");
139: JavaPackage c = new JavaPackage("C");
140: JavaPackage d = new JavaPackage("D");
141: JavaPackage e = new JavaPackage("E");
142:
143: a.dependsUpon(b);
144: b.dependsUpon(c);
145: c.dependsUpon(a);
146:
147: a.dependsUpon(d);
148: d.dependsUpon(e);
149: e.dependsUpon(a);
150:
151: List aCycles = new ArrayList();
152: assertEquals(true, a.containsCycle());
153: assertEquals(true, a.collectCycle(aCycles));
154: assertListEquals(aCycles, new String[] { "A", "B", "C", "A" });
155:
156: List bCycles = new ArrayList();
157: assertEquals(true, b.containsCycle());
158: assertEquals(true, b.collectCycle(bCycles));
159: assertListEquals(bCycles, new String[] { "B", "C", "A", "B" });
160:
161: List cCycles = new ArrayList();
162: assertEquals(true, c.containsCycle());
163: assertEquals(true, c.collectCycle(cCycles));
164: assertListEquals(cCycles, new String[] { "C", "A", "B", "C" });
165:
166: List dCycles = new ArrayList();
167: assertEquals(true, d.containsCycle());
168: assertEquals(true, d.collectCycle(dCycles));
169: assertListEquals(dCycles, new String[] { "D", "E", "A", "B",
170: "C", "A" });
171:
172: List eCycles = new ArrayList();
173: assertEquals(true, e.containsCycle());
174: assertEquals(true, e.collectCycle(eCycles));
175: assertListEquals(eCycles, new String[] { "E", "A", "B", "C",
176: "A" });
177: }
178:
179: protected void assertListEquals(List list, String names[]) {
180:
181: assertEquals(names.length, list.size());
182:
183: for (int i = 0; i < names.length; i++) {
184: assertEquals(names[i], ((JavaPackage) list.get(i))
185: .getName());
186: }
187: }
188:
189: protected void printCycles(List list) {
190: Iterator i = list.iterator();
191: while (i.hasNext()) {
192: JavaPackage p = (JavaPackage) i.next();
193: if (i.hasNext()) {
194: System.out.print(p.getName() + "->");
195: } else {
196: System.out.println(p.getName());
197: }
198: }
199: }
200: }
|