001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.structure.basic;
018:
019: import java.util.Iterator;
020:
021: import junit.framework.TestCase;
022:
023: import org.geotools.graph.structure.Edge;
024:
025: public class BasicEdgeTest extends TestCase {
026:
027: private BasicNode m_nodeA;
028: private BasicNode m_nodeB;
029: private BasicNode m_otherNode1;
030: private BasicNode m_otherNode2;
031: private BasicNode m_otherNode3;
032: private BasicNode m_otherNode4;
033:
034: private BasicEdge m_edge;
035: private BasicEdge m_other1;
036: private BasicEdge m_other2;
037: private BasicEdge m_other3;
038: private BasicEdge m_other4;
039:
040: private BasicEdge m_same;
041: private BasicEdge m_same2;
042: private BasicEdge m_diff;
043: private BasicEdge m_opp;
044: private BasicEdge m_loopA;
045: private BasicEdge m_loopB;
046:
047: public BasicEdgeTest(String name) {
048: super (name);
049: }
050:
051: protected void setUp() throws Exception {
052: m_nodeA = new BasicNode();
053: m_nodeB = new BasicNode();
054: m_otherNode1 = new BasicNode();
055: m_otherNode2 = new BasicNode();
056: m_otherNode3 = new BasicNode();
057: m_otherNode4 = new BasicNode();
058:
059: m_edge = new BasicEdge(m_nodeA, m_nodeB);
060: m_nodeA.add(m_edge);
061: m_nodeB.add(m_edge);
062:
063: m_other1 = new BasicEdge(m_nodeA, m_otherNode1);
064: m_nodeA.add(m_other1);
065: m_otherNode1.add(m_other1);
066:
067: m_other2 = new BasicEdge(m_nodeB, m_otherNode2);
068: m_nodeB.add(m_other2);
069: m_otherNode2.add(m_other2);
070:
071: m_other3 = new BasicEdge(m_otherNode3, m_nodeA);
072: m_otherNode3.add(m_other3);
073: m_nodeA.add(m_other3);
074:
075: m_other4 = new BasicEdge(m_otherNode4, m_nodeB);
076: m_otherNode4.add(m_other4);
077: m_nodeB.add(m_other4);
078:
079: //dont add these to the graph yet
080: m_same = new BasicEdge(m_nodeA, m_nodeB);
081: m_same2 = new BasicEdge(m_nodeA, m_nodeB);
082: m_opp = new BasicEdge(m_nodeB, m_nodeA);
083: m_loopA = new BasicEdge(m_nodeA, m_nodeA);
084: m_loopB = new BasicEdge(m_nodeB, m_nodeB);
085: }
086:
087: public void test_getNodeA() {
088: assertSame(m_edge.getNodeA(), m_nodeA);
089: }
090:
091: public void test_getNodeB() {
092: assertSame(m_edge.getNodeB(), m_nodeB);
093: }
094:
095: public void test_getOtherNode() {
096: assertSame(m_edge.getOtherNode(m_nodeA), m_nodeB);
097: assertSame(m_edge.getOtherNode(m_nodeB), m_nodeA);
098: assertSame(m_edge.getOtherNode(new BasicNode()), null);
099: }
100:
101: public void test_reverse() {
102: assertSame(m_nodeA, m_edge.getNodeA());
103: assertSame(m_nodeB, m_edge.getNodeB());
104:
105: m_edge.reverse();
106:
107: assertSame(m_nodeA, m_edge.getNodeB());
108: assertSame(m_nodeB, m_edge.getNodeA());
109: }
110:
111: public void test_getRelated() {
112: BasicEdge be;
113: Iterator itr;
114:
115: //nodes share single edge
116: itr = m_edge.getRelated();
117: assertTrue(itr.hasNext());
118:
119: be = (BasicEdge) itr.next();
120: assertTrue(be.equals(m_other1) || be.equals(m_other2)
121: || be.equals(m_other3) || be.equals(m_other4));
122: assertTrue(itr.hasNext());
123:
124: be = (BasicEdge) itr.next();
125: assertTrue(be.equals(m_other1) || be.equals(m_other2)
126: || be.equals(m_other3) || be.equals(m_other4));
127: assertTrue(itr.hasNext());
128: be = (BasicEdge) itr.next();
129: assertTrue(be.equals(m_other1) || be.equals(m_other2)
130: || be.equals(m_other3) || be.equals(m_other4));
131: assertTrue(itr.hasNext());
132:
133: be = (BasicEdge) itr.next();
134: assertTrue(be.equals(m_other1) || be.equals(m_other2)
135: || be.equals(m_other3) || be.equals(m_other4));
136: assertTrue(!itr.hasNext());
137:
138: //nodes share multiple edges (same direction)
139: m_nodeA.add(m_same);
140: m_nodeB.add(m_same);
141:
142: itr = m_edge.getRelated();
143: assertTrue(itr.hasNext());
144:
145: be = (BasicEdge) itr.next();
146: assertTrue(be.equals(m_other1) || be.equals(m_other2)
147: || be.equals(m_other3) || be.equals(m_other4)
148: || be.equals(m_same));
149: assertTrue(itr.hasNext());
150:
151: be = (BasicEdge) itr.next();
152: assertTrue(be.equals(m_other1) || be.equals(m_other2)
153: || be.equals(m_other3) || be.equals(m_other4)
154: || be.equals(m_same));
155: assertTrue(itr.hasNext());
156:
157: be = (BasicEdge) itr.next();
158: assertTrue(be.equals(m_other1) || be.equals(m_other2)
159: || be.equals(m_other3) || be.equals(m_other4)
160: || be.equals(m_same));
161: assertTrue(itr.hasNext());
162:
163: be = (BasicEdge) itr.next();
164: assertTrue(be.equals(m_other1) || be.equals(m_other2)
165: || be.equals(m_other3) || be.equals(m_other4)
166: || be.equals(m_same));
167: assertTrue(itr.hasNext());
168:
169: be = (BasicEdge) itr.next();
170: assertTrue(be.equals(m_other1) || be.equals(m_other2)
171: || be.equals(m_other3) || be.equals(m_other4)
172: || be.equals(m_same));
173: assertTrue(!itr.hasNext());
174: m_nodeA.remove(m_same);
175: m_nodeB.remove(m_same);
176:
177: //nodes share multiple edges (differnt direction)
178: m_nodeB.add(m_opp);
179: m_nodeA.add(m_opp);
180:
181: itr = m_edge.getRelated();
182: assertTrue(itr.hasNext());
183:
184: be = (BasicEdge) itr.next();
185: assertTrue(be.equals(m_other1) || be.equals(m_other2)
186: || be.equals(m_other3) || be.equals(m_other4)
187: || be.equals(m_opp));
188: assertTrue(itr.hasNext());
189:
190: be = (BasicEdge) itr.next();
191: assertTrue(be.equals(m_other1) || be.equals(m_other2)
192: || be.equals(m_other3) || be.equals(m_other4)
193: || be.equals(m_opp));
194: assertTrue(itr.hasNext());
195:
196: be = (BasicEdge) itr.next();
197: assertTrue(be.equals(m_other1) || be.equals(m_other2)
198: || be.equals(m_other3) || be.equals(m_other4)
199: || be.equals(m_opp));
200: assertTrue(itr.hasNext());
201:
202: be = (BasicEdge) itr.next();
203: assertTrue(be.equals(m_other1) || be.equals(m_other2)
204: || be.equals(m_other3) || be.equals(m_other4)
205: || be.equals(m_opp));
206: assertTrue(itr.hasNext());
207:
208: be = (BasicEdge) itr.next();
209: assertTrue(be.equals(m_other1) || be.equals(m_other2)
210: || be.equals(m_other3) || be.equals(m_other4)
211: || be.equals(m_opp));
212: assertTrue(!itr.hasNext());
213:
214: m_nodeA.remove(m_opp);
215: m_nodeB.remove(m_opp);
216:
217: //loop on one of nodes
218: m_nodeA.add(m_loopA);
219:
220: itr = m_edge.getRelated();
221: assertTrue(itr.hasNext());
222:
223: be = (BasicEdge) itr.next();
224: assertTrue(be.equals(m_other1) || be.equals(m_other2)
225: || be.equals(m_other3) || be.equals(m_other4)
226: || be.equals(m_loopA));
227: assertTrue(itr.hasNext());
228:
229: be = (BasicEdge) itr.next();
230: assertTrue(be.equals(m_other1) || be.equals(m_other2)
231: || be.equals(m_other3) || be.equals(m_other4)
232: || be.equals(m_loopA));
233:
234: be = (BasicEdge) itr.next();
235: assertTrue(be.equals(m_other1) || be.equals(m_other2)
236: || be.equals(m_other3) || be.equals(m_other4)
237: || be.equals(m_loopA));
238:
239: be = (BasicEdge) itr.next();
240: assertTrue(be.equals(m_other1) || be.equals(m_other2)
241: || be.equals(m_other3) || be.equals(m_other4)
242: || be.equals(m_loopA));
243: assertTrue(itr.hasNext());
244:
245: be = (BasicEdge) itr.next();
246: assertTrue(be.equals(m_other1) || be.equals(m_other2)
247: || be.equals(m_other3) || be.equals(m_other4)
248: || be.equals(m_loopA));
249: assertTrue(!itr.hasNext());
250:
251: m_nodeA.remove(m_loopA);
252:
253: //test loop on other node
254: m_nodeB.add(m_loopB);
255:
256: itr = m_edge.getRelated();
257: assertTrue(itr.hasNext());
258:
259: be = (BasicEdge) itr.next();
260: assertTrue(be.equals(m_other1) || be.equals(m_other2)
261: || be.equals(m_other3) || be.equals(m_other4)
262: || be.equals(m_loopB));
263: assertTrue(itr.hasNext());
264:
265: be = (BasicEdge) itr.next();
266: assertTrue(be.equals(m_other1) || be.equals(m_other2)
267: || be.equals(m_other3) || be.equals(m_other4)
268: || be.equals(m_loopB));
269:
270: be = (BasicEdge) itr.next();
271: assertTrue(be.equals(m_other1) || be.equals(m_other2)
272: || be.equals(m_other3) || be.equals(m_other4)
273: || be.equals(m_loopB));
274:
275: be = (BasicEdge) itr.next();
276: assertTrue(be.equals(m_other1) || be.equals(m_other2)
277: || be.equals(m_other3) || be.equals(m_other4)
278: || be.equals(m_loopB));
279: assertTrue(itr.hasNext());
280:
281: be = (BasicEdge) itr.next();
282: assertTrue(be.equals(m_other1) || be.equals(m_other2)
283: || be.equals(m_other3) || be.equals(m_other4)
284: || be.equals(m_loopB));
285: assertTrue(!itr.hasNext());
286:
287: //test loop on both nodes
288: m_nodeA.add(m_loopA);
289: itr = m_edge.getRelated();
290: assertTrue(itr.hasNext());
291:
292: be = (BasicEdge) itr.next();
293: assertTrue(be.equals(m_other1) || be.equals(m_other2)
294: || be.equals(m_loopA) || be.equals(m_other3)
295: || be.equals(m_other4) || be.equals(m_loopB));
296: assertTrue(itr.hasNext());
297:
298: be = (BasicEdge) itr.next();
299: assertTrue(be.equals(m_other1) || be.equals(m_other2)
300: || be.equals(m_loopA) || be.equals(m_other3)
301: || be.equals(m_other4) || be.equals(m_loopB));
302:
303: be = (BasicEdge) itr.next();
304: assertTrue(be.equals(m_other1) || be.equals(m_other2)
305: || be.equals(m_loopA) || be.equals(m_other3)
306: || be.equals(m_other4) || be.equals(m_loopB));
307:
308: be = (BasicEdge) itr.next();
309: assertTrue(be.equals(m_other1) || be.equals(m_other2)
310: || be.equals(m_loopA) || be.equals(m_other3)
311: || be.equals(m_other4) || be.equals(m_loopB));
312: assertTrue(itr.hasNext());
313:
314: be = (BasicEdge) itr.next();
315: assertTrue(be.equals(m_other1) || be.equals(m_other2)
316: || be.equals(m_loopA) || be.equals(m_other3)
317: || be.equals(m_other4) || be.equals(m_loopB));
318: assertTrue(itr.hasNext());
319:
320: be = (BasicEdge) itr.next();
321: assertTrue(be.equals(m_other1) || be.equals(m_other2)
322: || be.equals(m_loopA) || be.equals(m_other3)
323: || be.equals(m_other4) || be.equals(m_loopB));
324: assertTrue(!itr.hasNext());
325:
326: }
327:
328: public void test_compareTo() {
329: BasicEdge same = new BasicEdge(m_nodeA, m_nodeB);
330: BasicEdge opp = new BasicEdge(m_nodeB, m_nodeA);
331:
332: assertTrue(m_edge.compareNodes(same) == Edge.EQUAL_NODE_ORIENTATION);
333: assertTrue(m_edge.compareNodes(opp) == Edge.OPPOSITE_NODE_ORIENTATION);
334: assertTrue(m_edge.compareNodes(m_other1) == Edge.UNEQUAL_NODE_ORIENTATION);
335: }
336: }
|