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: public class BasicDirectedEdgeTest extends TestCase {
024:
025: private BasicDirectedNode m_inNode;
026: private BasicDirectedNode m_outNode;
027: private BasicDirectedNode m_otherInNode;
028: private BasicDirectedNode m_otherOutNode;
029:
030: private BasicDirectedEdge m_edge;
031:
032: private BasicDirectedEdge m_inEdge;
033: private BasicDirectedEdge m_outEdge;
034:
035: private BasicDirectedEdge m_same;
036: private BasicDirectedEdge m_same2;
037: private BasicDirectedEdge m_opp;
038: private BasicDirectedEdge m_opp2;
039: private BasicDirectedEdge m_inloop;
040: private BasicDirectedEdge m_outloop;
041: private BasicDirectedEdge m_inoutEdge;
042: private BasicDirectedEdge m_outinEdge;
043:
044: public BasicDirectedEdgeTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: m_inNode = new BasicDirectedNode();
050: m_outNode = new BasicDirectedNode();
051: m_edge = new BasicDirectedEdge(m_inNode, m_outNode);
052: m_inNode.addOut(m_edge);
053: m_outNode.addIn(m_edge);
054:
055: m_otherInNode = new BasicDirectedNode();
056: m_otherOutNode = new BasicDirectedNode();
057:
058: m_inEdge = new BasicDirectedEdge(m_otherInNode, m_inNode);
059: m_otherInNode.addOut(m_inEdge);
060: m_inNode.addIn(m_inEdge);
061:
062: m_outEdge = new BasicDirectedEdge(m_outNode, m_otherOutNode);
063: m_outNode.addOut(m_outEdge);
064: m_otherOutNode.addIn(m_outEdge);
065:
066: m_same = new BasicDirectedEdge(m_inNode, m_outNode);
067: m_same2 = new BasicDirectedEdge(m_inNode, m_outNode);
068:
069: m_opp = new BasicDirectedEdge(m_outNode, m_inNode);
070: m_opp2 = new BasicDirectedEdge(m_outNode, m_inNode);
071:
072: m_inloop = new BasicDirectedEdge(m_inNode, m_inNode);
073: m_outloop = new BasicDirectedEdge(m_outNode, m_outNode);
074:
075: m_inoutEdge = new BasicDirectedEdge(m_inNode, m_otherInNode);
076: m_outinEdge = new BasicDirectedEdge(m_otherOutNode, m_outNode);
077: }
078:
079: public void test_getInNode() {
080: assertTrue(m_inNode == m_edge.getNodeA());
081: assertTrue(m_inNode == m_edge.getInNode());
082: }
083:
084: public void test_getOutNode() {
085: assertTrue(m_outNode == m_edge.getNodeB());
086: assertTrue(m_outNode == m_edge.getOutNode());
087: }
088:
089: public void test_getOtherNode() {
090: assertTrue(m_inNode == m_edge.getOtherNode(m_outNode));
091: assertTrue(m_outNode == m_edge.getOtherNode(m_inNode));
092: assertTrue(null == m_edge.getOtherNode(new BasicDirectedNode()));
093: }
094:
095: public void test_reverse() {
096: assertTrue(m_edge.getInNode().getOutEdges().contains(m_edge));
097: assertTrue(m_edge.getOutNode().getInEdges().contains(m_edge));
098:
099: m_edge.reverse();
100:
101: assertTrue(m_inNode == m_edge.getOutNode());
102: assertTrue(m_outNode == m_edge.getInNode());
103:
104: assertTrue(m_edge.getInNode().getOutEdges().contains(m_edge));
105: assertTrue(m_edge.getOutNode().getInEdges().contains(m_edge));
106: }
107:
108: public void test_getRelated() {
109: Iterator itr = m_edge.getRelated();
110:
111: BasicDirectedEdge de = (BasicDirectedEdge) itr.next();
112: assertTrue(de == m_inEdge || de == m_outEdge);
113:
114: de = (BasicDirectedEdge) itr.next();
115: assertTrue(de == m_inEdge || de == m_outEdge);
116:
117: assertTrue(!itr.hasNext());
118:
119: //add an edge that has the same nodes, in the same direction
120: m_edge.getInNode().addOut(m_same);
121: m_edge.getOutNode().addIn(m_same);
122:
123: itr = m_edge.getRelated();
124:
125: de = (BasicDirectedEdge) itr.next();
126: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
127:
128: de = (BasicDirectedEdge) itr.next();
129: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
130:
131: de = (BasicDirectedEdge) itr.next();
132: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
133:
134: assertTrue(!itr.hasNext());
135:
136: //add another edge in the same direction
137: m_edge.getInNode().addOut(m_same2);
138: m_edge.getOutNode().addIn(m_same2);
139:
140: itr = m_edge.getRelated();
141:
142: de = (BasicDirectedEdge) itr.next();
143: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
144: || de == m_same2);
145:
146: de = (BasicDirectedEdge) itr.next();
147: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
148: || de == m_same2);
149:
150: de = (BasicDirectedEdge) itr.next();
151: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
152: || de == m_same2);
153:
154: de = (BasicDirectedEdge) itr.next();
155: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
156: || de == m_same2);
157:
158: assertTrue(!itr.hasNext());
159:
160: m_edge.getInNode().removeOut(m_same);
161: m_edge.getOutNode().removeIn(m_same);
162:
163: m_edge.getInNode().removeOut(m_same2);
164: m_edge.getOutNode().removeIn(m_same2);
165:
166: //add an edge that has the same nodes, opposite direction
167: m_edge.getInNode().addIn(m_opp);
168: m_edge.getOutNode().addOut(m_opp);
169:
170: itr = m_edge.getRelated();
171:
172: de = (BasicDirectedEdge) itr.next();
173: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
174:
175: de = (BasicDirectedEdge) itr.next();
176: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
177:
178: de = (BasicDirectedEdge) itr.next();
179: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
180:
181: assertTrue(!itr.hasNext());
182:
183: //add another edge in opposite direction
184: m_edge.getInNode().addIn(m_opp2);
185: m_edge.getOutNode().addOut(m_opp2);
186:
187: itr = m_edge.getRelated();
188:
189: de = (BasicDirectedEdge) itr.next();
190: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
191: || de == m_opp2);
192:
193: de = (BasicDirectedEdge) itr.next();
194: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
195: || de == m_opp2);
196:
197: de = (BasicDirectedEdge) itr.next();
198: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
199: || de == m_opp2);
200:
201: de = (BasicDirectedEdge) itr.next();
202: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
203: || de == m_opp2);
204:
205: assertTrue(!itr.hasNext());
206:
207: m_edge.getInNode().removeIn(m_opp);
208: m_edge.getOutNode().removeOut(m_opp);
209:
210: m_edge.getInNode().removeIn(m_opp2);
211: m_edge.getOutNode().removeOut(m_opp2);
212:
213: //add loops
214: m_edge.getInNode().addIn(m_inloop);
215: m_edge.getInNode().addOut(m_inloop);
216:
217: m_edge.getOutNode().addIn(m_outloop);
218: m_edge.getOutNode().addOut(m_outloop);
219:
220: itr = m_edge.getRelated();
221:
222: de = (BasicDirectedEdge) itr.next();
223: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
224: || de == m_outloop);
225:
226: de = (BasicDirectedEdge) itr.next();
227: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
228: || de == m_outloop);
229:
230: de = (BasicDirectedEdge) itr.next();
231: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
232: || de == m_outloop);
233:
234: de = (BasicDirectedEdge) itr.next();
235: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
236: || de == m_outloop);
237: assertTrue(!itr.hasNext());
238:
239: m_edge.getInNode().removeIn(m_inloop);
240: m_edge.getInNode().removeOut(m_inloop);
241:
242: m_edge.getOutNode().removeIn(m_outloop);
243: m_edge.getOutNode().removeOut(m_outloop);
244:
245: //add an incoming edge to the out node and an outgoing edge to the in node
246: m_edge.getInNode().addOut(m_inoutEdge);
247: m_otherInNode.addIn(m_inoutEdge);
248:
249: m_edge.getOutNode().addIn(m_outinEdge);
250: m_otherOutNode.addOut(m_outinEdge);
251:
252: itr = m_edge.getRelated();
253:
254: de = (BasicDirectedEdge) itr.next();
255: assertTrue(de == m_inEdge || de == m_outEdge
256: || de == m_inoutEdge || de == m_outinEdge);
257:
258: de = (BasicDirectedEdge) itr.next();
259: assertTrue(de == m_inEdge || de == m_outEdge
260: || de == m_inoutEdge || de == m_outinEdge);
261:
262: de = (BasicDirectedEdge) itr.next();
263: assertTrue(de == m_inEdge || de == m_outEdge
264: || de == m_inoutEdge || de == m_outinEdge);
265:
266: de = (BasicDirectedEdge) itr.next();
267: assertTrue(de == m_inEdge || de == m_outEdge
268: || de == m_inoutEdge || de == m_outinEdge);
269:
270: assertTrue(!itr.hasNext());
271: }
272:
273: public void test_getInRelated() {
274: Iterator itr = m_edge.getInRelated();
275:
276: BasicDirectedEdge de = (BasicDirectedEdge) itr.next();
277: assertTrue(de == m_inEdge);
278:
279: assertTrue(!itr.hasNext());
280:
281: //add same edge (shouldn't show up in iterator this time)
282: m_edge.getInNode().addOut(m_same);
283: m_edge.getOutNode().addIn(m_same);
284:
285: itr = m_edge.getInRelated();
286:
287: de = (BasicDirectedEdge) itr.next();
288: assertTrue(de == m_inEdge);
289:
290: assertTrue(!itr.hasNext());
291:
292: m_edge.getInNode().removeOut(m_same);
293: m_edge.getOutNode().removeIn(m_same);
294:
295: //add opposite edge
296: m_edge.getInNode().addIn(m_opp);
297: m_edge.getOutNode().addOut(m_opp);
298:
299: itr = m_edge.getInRelated();
300:
301: de = (BasicDirectedEdge) itr.next();
302: assertTrue(de == m_inEdge || de == m_opp);
303:
304: de = (BasicDirectedEdge) itr.next();
305: assertTrue(de == m_inEdge || de == m_opp);
306:
307: assertTrue(!itr.hasNext());
308:
309: //add multiple opposites
310: m_edge.getInNode().addIn(m_opp2);
311: m_edge.getOutNode().addOut(m_opp2);
312:
313: itr = m_edge.getInRelated();
314:
315: de = (BasicDirectedEdge) itr.next();
316: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
317:
318: de = (BasicDirectedEdge) itr.next();
319: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
320:
321: de = (BasicDirectedEdge) itr.next();
322: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
323:
324: assertTrue(!itr.hasNext());
325:
326: m_edge.getInNode().removeIn(m_opp);
327: m_edge.getOutNode().removeOut(m_opp);
328:
329: m_edge.getInNode().removeIn(m_opp2);
330: m_edge.getOutNode().removeOut(m_opp2);
331:
332: //add loop
333: m_edge.getInNode().addIn(m_inloop);
334: m_edge.getInNode().addOut(m_inloop);
335:
336: itr = m_edge.getInRelated();
337:
338: de = (BasicDirectedEdge) itr.next();
339: assertTrue(de == m_inEdge || de == m_inloop);
340:
341: de = (BasicDirectedEdge) itr.next();
342: assertTrue(de == m_inEdge || de == m_inloop);
343:
344: assertTrue(!itr.hasNext());
345: }
346:
347: public void test_getOutRelated() {
348: Iterator itr = m_edge.getOutRelated();
349:
350: BasicDirectedEdge de = (BasicDirectedEdge) itr.next();
351: assertTrue(de == m_outEdge);
352:
353: assertTrue(!itr.hasNext());
354:
355: //add same edge (shouldn't show up in iterator this time)
356: m_edge.getInNode().addOut(m_same);
357: m_edge.getOutNode().addIn(m_same);
358:
359: itr = m_edge.getOutRelated();
360:
361: de = (BasicDirectedEdge) itr.next();
362: assertTrue(de == m_outEdge);
363:
364: assertTrue(!itr.hasNext());
365:
366: m_edge.getInNode().removeOut(m_same);
367: m_edge.getOutNode().removeIn(m_same);
368:
369: //add opposite edge
370: m_edge.getInNode().addIn(m_opp);
371: m_edge.getOutNode().addOut(m_opp);
372:
373: itr = m_edge.getOutRelated();
374:
375: de = (BasicDirectedEdge) itr.next();
376: assertTrue(de == m_outEdge || de == m_opp);
377:
378: de = (BasicDirectedEdge) itr.next();
379: assertTrue(de == m_outEdge || de == m_opp);
380:
381: assertTrue(!itr.hasNext());
382:
383: //add another opposite edge
384: m_edge.getInNode().addIn(m_opp2);
385: m_edge.getOutNode().addOut(m_opp2);
386:
387: itr = m_edge.getOutRelated();
388:
389: de = (BasicDirectedEdge) itr.next();
390: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
391:
392: de = (BasicDirectedEdge) itr.next();
393: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
394:
395: de = (BasicDirectedEdge) itr.next();
396: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
397:
398: assertTrue(!itr.hasNext());
399:
400: m_edge.getInNode().removeIn(m_opp);
401: m_edge.getOutNode().removeOut(m_opp);
402:
403: m_edge.getInNode().removeIn(m_opp2);
404: m_edge.getOutNode().removeOut(m_opp2);
405:
406: //add loop
407: m_edge.getOutNode().addIn(m_outloop);
408: m_edge.getOutNode().addOut(m_outloop);
409:
410: itr = m_edge.getOutRelated();
411:
412: de = (BasicDirectedEdge) itr.next();
413: assertTrue(de == m_outEdge || de == m_outloop);
414:
415: de = (BasicDirectedEdge) itr.next();
416: assertTrue(de == m_outEdge || de == m_outloop);
417:
418: assertTrue(!itr.hasNext());
419: }
420: }
|