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.opt;
018:
019: import java.util.Iterator;
020:
021: import junit.framework.TestCase;
022:
023: import org.geotools.graph.structure.Edge;
024:
025: public class OptEdgeTest extends TestCase {
026:
027: private OptNode m_nodeA;
028: private OptNode m_nodeB;
029: private OptNode m_otherNode1;
030: private OptNode m_otherNode2;
031: private OptNode m_otherNode3;
032: private OptNode m_otherNode4;
033:
034: private OptEdge m_edge;
035: private OptEdge m_other1;
036: private OptEdge m_other2;
037: private OptEdge m_other3;
038: private OptEdge m_other4;
039:
040: private OptEdge m_same;
041: private OptEdge m_diff;
042: private OptEdge m_opp;
043: private OptEdge m_loopA;
044: private OptEdge m_loopB;
045:
046: public OptEdgeTest(String name) {
047: super (name);
048: }
049:
050: protected void setUp() throws Exception {
051: m_nodeA = new OptNode();
052: m_nodeA.setDegree(3);
053: m_nodeB = new OptNode();
054: m_nodeB.setDegree(3);
055: m_otherNode1 = new OptNode();
056: m_otherNode1.setDegree(1);
057: m_otherNode2 = new OptNode();
058: m_otherNode2.setDegree(1);
059: m_otherNode3 = new OptNode();
060: m_otherNode3.setDegree(1);
061: m_otherNode4 = new OptNode();
062: m_otherNode4.setDegree(1);
063:
064: m_edge = new OptEdge(m_nodeA, m_nodeB);
065: m_nodeA.add(m_edge);
066: m_nodeB.add(m_edge);
067:
068: m_other1 = new OptEdge(m_nodeA, m_otherNode1);
069: m_nodeA.add(m_other1);
070: m_otherNode1.add(m_other1);
071:
072: m_other2 = new OptEdge(m_nodeB, m_otherNode2);
073: m_nodeB.add(m_other2);
074: m_otherNode2.add(m_other2);
075:
076: m_other3 = new OptEdge(m_otherNode3, m_nodeA);
077: m_otherNode3.add(m_other3);
078: m_nodeA.add(m_other3);
079:
080: m_other4 = new OptEdge(m_otherNode4, m_nodeB);
081: m_otherNode4.add(m_other4);
082: m_nodeB.add(m_other4);
083:
084: //dont add these to the graph yet
085: m_same = new OptEdge(m_nodeA, m_nodeB);
086: m_opp = new OptEdge(m_nodeB, m_nodeA);
087: m_loopA = new OptEdge(m_nodeA, m_nodeA);
088: m_loopB = new OptEdge(m_nodeB, m_nodeB);
089:
090: }
091:
092: protected void addSame() {
093: try {
094: setUp();
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098:
099: m_nodeA.setDegree(4);
100: m_nodeB.setDegree(4);
101:
102: m_nodeA.add(m_edge);
103: m_nodeA.add(m_other1);
104: m_nodeA.add(m_other3);
105: m_nodeA.add(m_same);
106:
107: m_nodeB.add(m_edge);
108: m_nodeB.add(m_other2);
109: m_nodeB.add(m_other4);
110: m_nodeB.add(m_same);
111: }
112:
113: protected void addOpp() {
114: try {
115: setUp();
116: } catch (Exception e) {
117: e.printStackTrace();
118: }
119:
120: m_nodeA.setDegree(4);
121: m_nodeB.setDegree(4);
122:
123: m_nodeA.add(m_edge);
124: m_nodeA.add(m_other1);
125: m_nodeA.add(m_other3);
126: m_nodeA.add(m_opp);
127:
128: m_nodeB.add(m_edge);
129: m_nodeB.add(m_other2);
130: m_nodeB.add(m_other4);
131: m_nodeB.add(m_opp);
132: }
133:
134: protected void addLoopA() {
135: try {
136: setUp();
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140:
141: m_nodeA.setDegree(4);
142:
143: m_nodeA.add(m_edge);
144: m_nodeA.add(m_other1);
145: m_nodeA.add(m_other3);
146: m_nodeA.add(m_loopA);
147: }
148:
149: protected void addLoopB() {
150: try {
151: setUp();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155:
156: m_nodeB.setDegree(4);
157:
158: m_nodeB.add(m_edge);
159: m_nodeB.add(m_other2);
160: m_nodeB.add(m_other4);
161: m_nodeB.add(m_loopB);
162: }
163:
164: protected void addLoopAB() {
165: try {
166: setUp();
167: } catch (Exception e) {
168: e.printStackTrace();
169: }
170:
171: m_nodeA.setDegree(4);
172:
173: m_nodeA.add(m_edge);
174: m_nodeA.add(m_other1);
175: m_nodeA.add(m_other3);
176: m_nodeA.add(m_loopA);
177:
178: m_nodeB.setDegree(4);
179:
180: m_nodeB.add(m_edge);
181: m_nodeB.add(m_other2);
182: m_nodeB.add(m_other4);
183: m_nodeB.add(m_loopB);
184: }
185:
186: public void test_getNodeA() {
187: assertSame(m_edge.getNodeA(), m_nodeA);
188: }
189:
190: public void test_getNodeB() {
191: assertSame(m_edge.getNodeB(), m_nodeB);
192: }
193:
194: public void test_getOtherNode() {
195: assertSame(m_edge.getOtherNode(m_nodeA), m_nodeB);
196: assertSame(m_edge.getOtherNode(m_nodeB), m_nodeA);
197: assertSame(m_edge.getOtherNode(new OptNode()), null);
198: }
199:
200: public void test_reverse() {
201: assertSame(m_nodeA, m_edge.getNodeA());
202: assertSame(m_nodeB, m_edge.getNodeB());
203:
204: m_edge.reverse();
205:
206: assertSame(m_nodeA, m_edge.getNodeB());
207: assertSame(m_nodeB, m_edge.getNodeA());
208: }
209:
210: public void test_getRelated() {
211: OptEdge be;
212: Iterator itr;
213:
214: //nodes share single edge
215: itr = m_edge.getRelated();
216: assertTrue(itr.hasNext());
217:
218: be = (OptEdge) itr.next();
219: assertTrue(be.equals(m_other1) || be.equals(m_other2)
220: || be.equals(m_other3) || be.equals(m_other4));
221: assertTrue(itr.hasNext());
222:
223: be = (OptEdge) itr.next();
224: assertTrue(be.equals(m_other1) || be.equals(m_other2)
225: || be.equals(m_other3) || be.equals(m_other4));
226: assertTrue(itr.hasNext());
227: be = (OptEdge) itr.next();
228: assertTrue(be.equals(m_other1) || be.equals(m_other2)
229: || be.equals(m_other3) || be.equals(m_other4));
230: assertTrue(itr.hasNext());
231:
232: be = (OptEdge) itr.next();
233: assertTrue(be.equals(m_other1) || be.equals(m_other2)
234: || be.equals(m_other3) || be.equals(m_other4));
235: assertTrue(!itr.hasNext());
236:
237: //nodes share multiple edges (same direction)
238: addSame();
239:
240: itr = m_edge.getRelated();
241: assertTrue(itr.hasNext());
242:
243: be = (OptEdge) itr.next();
244: assertTrue(be.equals(m_other1) || be.equals(m_other2)
245: || be.equals(m_other3) || be.equals(m_other4)
246: || be.equals(m_same));
247: assertTrue(itr.hasNext());
248:
249: be = (OptEdge) itr.next();
250: assertTrue(be.equals(m_other1) || be.equals(m_other2)
251: || be.equals(m_other3) || be.equals(m_other4)
252: || be.equals(m_same));
253: assertTrue(itr.hasNext());
254:
255: be = (OptEdge) itr.next();
256: assertTrue(be.equals(m_other1) || be.equals(m_other2)
257: || be.equals(m_other3) || be.equals(m_other4)
258: || be.equals(m_same));
259: assertTrue(itr.hasNext());
260:
261: be = (OptEdge) itr.next();
262: assertTrue(be.equals(m_other1) || be.equals(m_other2)
263: || be.equals(m_other3) || be.equals(m_other4)
264: || be.equals(m_same));
265: assertTrue(itr.hasNext());
266:
267: be = (OptEdge) itr.next();
268: assertTrue(be.equals(m_other1) || be.equals(m_other2)
269: || be.equals(m_other3) || be.equals(m_other4)
270: || be.equals(m_same));
271: assertTrue(!itr.hasNext());
272:
273: //nodes share multiple edges (differnt direction)
274: addOpp();
275:
276: itr = m_edge.getRelated();
277: assertTrue(itr.hasNext());
278:
279: be = (OptEdge) itr.next();
280: assertTrue(be.equals(m_other1) || be.equals(m_other2)
281: || be.equals(m_other3) || be.equals(m_other4)
282: || be.equals(m_opp));
283: assertTrue(itr.hasNext());
284:
285: be = (OptEdge) itr.next();
286: assertTrue(be.equals(m_other1) || be.equals(m_other2)
287: || be.equals(m_other3) || be.equals(m_other4)
288: || be.equals(m_opp));
289: assertTrue(itr.hasNext());
290:
291: be = (OptEdge) itr.next();
292: assertTrue(be.equals(m_other1) || be.equals(m_other2)
293: || be.equals(m_other3) || be.equals(m_other4)
294: || be.equals(m_opp));
295: assertTrue(itr.hasNext());
296:
297: be = (OptEdge) itr.next();
298: assertTrue(be.equals(m_other1) || be.equals(m_other2)
299: || be.equals(m_other3) || be.equals(m_other4)
300: || be.equals(m_opp));
301: assertTrue(itr.hasNext());
302:
303: be = (OptEdge) itr.next();
304: assertTrue(be.equals(m_other1) || be.equals(m_other2)
305: || be.equals(m_other3) || be.equals(m_other4)
306: || be.equals(m_opp));
307: assertTrue(!itr.hasNext());
308:
309: //loop on one of nodes
310: addLoopA();
311:
312: itr = m_edge.getRelated();
313: assertTrue(itr.hasNext());
314:
315: be = (OptEdge) itr.next();
316: assertTrue(be.equals(m_other1) || be.equals(m_other2)
317: || be.equals(m_other3) || be.equals(m_other4)
318: || be.equals(m_loopA));
319: assertTrue(itr.hasNext());
320:
321: be = (OptEdge) itr.next();
322: assertTrue(be.equals(m_other1) || be.equals(m_other2)
323: || be.equals(m_other3) || be.equals(m_other4)
324: || be.equals(m_loopA));
325:
326: be = (OptEdge) itr.next();
327: assertTrue(be.equals(m_other1) || be.equals(m_other2)
328: || be.equals(m_other3) || be.equals(m_other4)
329: || be.equals(m_loopA));
330:
331: be = (OptEdge) itr.next();
332: assertTrue(be.equals(m_other1) || be.equals(m_other2)
333: || be.equals(m_other3) || be.equals(m_other4)
334: || be.equals(m_loopA));
335: assertTrue(itr.hasNext());
336:
337: be = (OptEdge) itr.next();
338: assertTrue(be.equals(m_other1) || be.equals(m_other2)
339: || be.equals(m_other3) || be.equals(m_other4)
340: || be.equals(m_loopA));
341: assertTrue(!itr.hasNext());
342:
343: //loop on other node
344: addLoopB();
345:
346: itr = m_edge.getRelated();
347: assertTrue(itr.hasNext());
348:
349: be = (OptEdge) itr.next();
350: assertTrue(be.equals(m_other1) || be.equals(m_other2)
351: || be.equals(m_other3) || be.equals(m_other4)
352: || be.equals(m_loopB));
353: assertTrue(itr.hasNext());
354:
355: be = (OptEdge) itr.next();
356: assertTrue(be.equals(m_other1) || be.equals(m_other2)
357: || be.equals(m_other3) || be.equals(m_other4)
358: || be.equals(m_loopB));
359:
360: be = (OptEdge) itr.next();
361: assertTrue(be.equals(m_other1) || be.equals(m_other2)
362: || be.equals(m_other3) || be.equals(m_other4)
363: || be.equals(m_loopB));
364:
365: be = (OptEdge) itr.next();
366: assertTrue(be.equals(m_other1) || be.equals(m_other2)
367: || be.equals(m_other3) || be.equals(m_other4)
368: || be.equals(m_loopB));
369: assertTrue(itr.hasNext());
370:
371: be = (OptEdge) itr.next();
372: assertTrue(be.equals(m_other1) || be.equals(m_other2)
373: || be.equals(m_other3) || be.equals(m_other4)
374: || be.equals(m_loopB));
375: assertTrue(!itr.hasNext());
376:
377: //loop on both
378: addLoopAB();
379:
380: itr = m_edge.getRelated();
381: assertTrue(itr.hasNext());
382:
383: be = (OptEdge) itr.next();
384: assertTrue(be.equals(m_other1) || be.equals(m_other2)
385: || be.equals(m_loopA) || be.equals(m_other3)
386: || be.equals(m_other4) || be.equals(m_loopB));
387: assertTrue(itr.hasNext());
388:
389: be = (OptEdge) itr.next();
390: assertTrue(be.equals(m_other1) || be.equals(m_other2)
391: || be.equals(m_loopA) || be.equals(m_other3)
392: || be.equals(m_other4) || be.equals(m_loopB));
393:
394: be = (OptEdge) itr.next();
395: assertTrue(be.equals(m_other1) || be.equals(m_other2)
396: || be.equals(m_loopA) || be.equals(m_other3)
397: || be.equals(m_other4) || be.equals(m_loopB));
398:
399: be = (OptEdge) itr.next();
400: assertTrue(be.equals(m_other1) || be.equals(m_other2)
401: || be.equals(m_loopA) || be.equals(m_other3)
402: || be.equals(m_other4) || be.equals(m_loopA));
403: assertTrue(itr.hasNext());
404:
405: be = (OptEdge) itr.next();
406: assertTrue(be.equals(m_other1) || be.equals(m_other2)
407: || be.equals(m_loopA) || be.equals(m_other3)
408: || be.equals(m_other4) || be.equals(m_loopB));
409:
410: be = (OptEdge) itr.next();
411: assertTrue(be.equals(m_other1) || be.equals(m_other2)
412: || be.equals(m_loopA) || be.equals(m_other3)
413: || be.equals(m_other4) || be.equals(m_loopB));
414:
415: assertTrue(!itr.hasNext());
416: }
417:
418: public void test_compareTo() {
419: OptEdge same = new OptEdge(m_nodeA, m_nodeB);
420: OptEdge opp = new OptEdge(m_nodeB, m_nodeA);
421:
422: assertTrue(m_edge.compareNodes(same) == Edge.EQUAL_NODE_ORIENTATION);
423: assertTrue(m_edge.compareNodes(opp) == Edge.OPPOSITE_NODE_ORIENTATION);
424: assertTrue(m_edge.compareNodes(m_other1) == Edge.UNEQUAL_NODE_ORIENTATION);
425: }
426: }
|