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 TestCycle extends TestCase {
040: private Node a;
041: private Node b;
042: private Node c;
043: private Node d;
044: private Node e;
045:
046: protected void setUp() throws Exception {
047: NodeFactory factory = new NodeFactory();
048:
049: a = factory.createPackage("a");
050: b = factory.createPackage("b");
051: c = factory.createPackage("c");
052: d = factory.createPackage("d");
053: e = factory.createPackage("e");
054: }
055:
056: public void testConstructEmptyCycle() {
057: try {
058: new Cycle(new ArrayList<Node>());
059: fail("Constructed empty cycle");
060: } catch (Exception ex) {
061: // expected
062: }
063: }
064:
065: public void testConstructLength1Cycle() {
066: List<Node> path = new ArrayList<Node>();
067: path.add(a);
068: Cycle cycle = new Cycle(path);
069:
070: assertEquals("length", 1, cycle.getLength());
071: assertEquals("a", a, cycle.getPath().iterator().next());
072: }
073:
074: public void testEquals_Identical() {
075: List<Node> path1 = new ArrayList<Node>();
076: path1.add(a);
077: path1.add(b);
078: Cycle cycle1 = new Cycle(path1);
079:
080: List<Node> path2 = new ArrayList<Node>();
081: path2.add(a);
082: path2.add(b);
083: Cycle cycle2 = new Cycle(path2);
084:
085: assertTrue(cycle1.equals(cycle2));
086: assertTrue(cycle2.equals(cycle1));
087: }
088:
089: public void testEquals_Reversed() {
090: List<Node> path1 = new ArrayList<Node>();
091: path1.add(a);
092: path1.add(b);
093: Cycle cycle1 = new Cycle(path1);
094:
095: List<Node> path2 = new ArrayList<Node>();
096: path2.add(b);
097: path2.add(a);
098: Cycle cycle2 = new Cycle(path2);
099:
100: assertTrue(cycle1.equals(cycle2));
101: assertTrue(cycle2.equals(cycle1));
102: }
103:
104: public void testEquals_SameLength() {
105: List<Node> path1 = new ArrayList<Node>();
106: path1.add(a);
107: path1.add(b);
108: Cycle cycle1 = new Cycle(path1);
109:
110: List<Node> path2 = new ArrayList<Node>();
111: path2.add(c);
112: path2.add(d);
113: Cycle cycle2 = new Cycle(path2);
114:
115: assertFalse(cycle1.equals(cycle2));
116: assertFalse(cycle2.equals(cycle1));
117: }
118:
119: public void testEquals_DifferentLength() {
120: List<Node> path1 = new ArrayList<Node>();
121: path1.add(a);
122: path1.add(b);
123: Cycle cycle1 = new Cycle(path1);
124:
125: List<Node> path2 = new ArrayList<Node>();
126: path2.add(c);
127: path2.add(d);
128: path2.add(e);
129: Cycle cycle2 = new Cycle(path2);
130:
131: assertFalse(cycle1.equals(cycle2));
132: assertFalse(cycle2.equals(cycle1));
133: }
134:
135: public void testEquals_LengthTrumpsContent() {
136: List<Node> path1 = new ArrayList<Node>();
137: path1.add(a);
138: path1.add(b);
139: path1.add(c);
140: Cycle cycle1 = new Cycle(path1);
141:
142: List<Node> path2 = new ArrayList<Node>();
143: path2.add(d);
144: path2.add(e);
145: Cycle cycle2 = new Cycle(path2);
146:
147: assertFalse(cycle1.equals(cycle2));
148: assertFalse(cycle2.equals(cycle1));
149: }
150:
151: public void testCompareTo_Identical() {
152: List<Node> path1 = new ArrayList<Node>();
153: path1.add(a);
154: path1.add(b);
155: Cycle cycle1 = new Cycle(path1);
156:
157: List<Node> path2 = new ArrayList<Node>();
158: path2.add(a);
159: path2.add(b);
160: Cycle cycle2 = new Cycle(path2);
161:
162: assertEquals(0, cycle1.compareTo(cycle2));
163: assertEquals(0, cycle2.compareTo(cycle1));
164: }
165:
166: public void testCompareTo_Reversed() {
167: List<Node> path1 = new ArrayList<Node>();
168: path1.add(a);
169: path1.add(b);
170: Cycle cycle1 = new Cycle(path1);
171:
172: List<Node> path2 = new ArrayList<Node>();
173: path2.add(b);
174: path2.add(a);
175: Cycle cycle2 = new Cycle(path2);
176:
177: assertEquals(0, cycle1.compareTo(cycle2));
178: assertEquals(0, cycle2.compareTo(cycle1));
179: }
180:
181: public void testCompareTo_SameLength() {
182: List<Node> path1 = new ArrayList<Node>();
183: path1.add(a);
184: path1.add(b);
185: Cycle cycle1 = new Cycle(path1);
186:
187: List<Node> path2 = new ArrayList<Node>();
188: path2.add(c);
189: path2.add(d);
190: Cycle cycle2 = new Cycle(path2);
191:
192: assertTrue(cycle1.compareTo(cycle2) < 0);
193: assertTrue(cycle2.compareTo(cycle1) > 0);
194: }
195:
196: public void testCompareTo_DifferentLength() {
197: List<Node> path1 = new ArrayList<Node>();
198: path1.add(a);
199: path1.add(b);
200: Cycle cycle1 = new Cycle(path1);
201:
202: List<Node> path2 = new ArrayList<Node>();
203: path2.add(c);
204: path2.add(d);
205: path2.add(e);
206: Cycle cycle2 = new Cycle(path2);
207:
208: assertTrue(cycle1.compareTo(cycle2) < 0);
209: assertTrue(cycle2.compareTo(cycle1) > 0);
210: }
211:
212: public void testCompareTo_LengthTrumpsContent() {
213: List<Node> path1 = new ArrayList<Node>();
214: path1.add(a);
215: path1.add(b);
216: path1.add(c);
217: Cycle cycle1 = new Cycle(path1);
218:
219: List<Node> path2 = new ArrayList<Node>();
220: path2.add(d);
221: path2.add(e);
222: Cycle cycle2 = new Cycle(path2);
223:
224: assertTrue(cycle1.compareTo(cycle2) > 0);
225: assertTrue(cycle2.compareTo(cycle1) < 0);
226: }
227: }
|