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 BasicNodeTest extends TestCase {
026:
027: private BasicNode m_node;
028: private BasicNode m_otherNode;
029: private Edge m_edge;
030: private Edge m_otherEdge;
031: private Edge m_loop;
032:
033: public BasicNodeTest(String name) {
034: super (name);
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: m_node = new BasicNode();
040: m_otherNode = new BasicNode();
041:
042: m_edge = new BasicEdge(m_node, m_otherNode);
043: m_otherEdge = new BasicEdge(m_node, m_otherNode);
044: m_loop = new BasicEdge(m_node, m_node);
045: }
046:
047: public void test_add() {
048: //test addition of single edge
049: m_node.add(m_edge);
050: assertTrue(m_node.getEdges().contains(m_edge));
051:
052: //test addition of multiple edges, same edge
053: m_node.add(m_edge);
054: assertTrue(m_node.getEdges().size() == 2);
055:
056: //test addition of multiple edges, different
057: Edge other = new BasicEdge(m_node, m_otherNode);
058: m_node.add(other);
059: assertTrue(m_node.getEdges().size() == 3);
060: assertTrue(m_node.getEdges().contains(m_edge));
061: assertTrue(m_node.getEdges().contains(other));
062: }
063:
064: public void test_remove() {
065: m_node.add(m_edge);
066: assertTrue(m_node.getEdges().contains(m_edge));
067:
068: //test removal of a single edge
069: m_node.remove(m_edge);
070: assertTrue(m_node.getEdges().size() == 0);
071:
072: //test removal of multiple edges, same
073: m_node.add(m_edge);
074: m_node.add(m_edge);
075: assertTrue(m_node.getEdges().size() == 2);
076:
077: m_node.remove(m_edge);
078: assertTrue(m_node.getEdges().size() == 1);
079:
080: m_node.remove(m_edge);
081: assertTrue(m_node.getEdges().size() == 0);
082:
083: //test removal of multiple edges, different
084: m_node.add(m_edge);
085: m_node.add(m_otherEdge);
086:
087: m_node.remove(m_edge);
088: assertTrue(m_node.getEdges().size() == 1);
089:
090: m_node.remove(m_otherEdge);
091: assertTrue(m_node.getEdges().size() == 0);
092: }
093:
094: public void test_getDegree() {
095: //intially degree should be zero
096: assertTrue(m_node.getDegree() == 0);
097:
098: //add single edge making degree 1
099: m_node.add(m_edge);
100: assertTrue(m_node.getDegree() == 1);
101:
102: //add the same edge, should be degree 2
103: m_node.add(m_edge);
104: assertTrue(m_node.getDegree() == 2);
105:
106: //add different edge, should be degree 3
107: m_node.add(m_otherEdge);
108: assertTrue(m_node.getDegree() == 3);
109:
110: m_node.getEdges().clear();
111:
112: //add a loop edge, degree should be two, but size will be one
113: m_node.add(m_loop);
114: assertTrue(m_node.getEdges().size() == 1);
115: assertTrue(m_node.getDegree() == 2);
116: }
117:
118: public void test_getEdge() {
119: m_node.add(m_edge);
120: assertSame(m_edge, m_node.getEdge(m_otherNode));
121:
122: //add another edge that has the same other node, since the underlying
123: // structure is a list, first one should be returned
124: m_node.add(m_otherEdge);
125: assertSame(m_edge, m_node.getEdge(m_otherNode));
126:
127: //remove first edge
128: m_node.remove(m_edge);
129:
130: //the call should now return other edge
131: assertSame(m_otherEdge, m_node.getEdge(m_otherNode));
132: }
133:
134: public void test_getEdges() {
135: m_node.add(m_edge);
136: assertTrue(m_node.getEdges(m_otherNode).contains(m_edge));
137:
138: //add the same edge
139: m_node.add(m_edge);
140: assertTrue(m_node.getEdges(m_otherNode).size() == 2);
141:
142: //add a different edge
143: m_node.add(m_otherEdge);
144: assertTrue(m_node.getEdges(m_otherNode).contains(m_edge));
145: assertTrue(m_node.getEdges(m_otherNode).contains(m_otherEdge));
146: }
147:
148: public void test_getRelated() {
149: // no edges should be empty
150: assertTrue(!m_node.getRelated().hasNext());
151:
152: //single edge
153: m_node.add(m_edge);
154: Iterator itr = m_node.getRelated();
155: assertSame(itr.next(), m_otherNode);
156: assertTrue(!itr.hasNext());
157:
158: //multiple edges, same, same node should be returned twice
159: m_node.add(m_edge);
160: itr = m_node.getRelated();
161: assertSame(itr.next(), m_otherNode);
162: assertSame(itr.next(), m_otherNode);
163: assertTrue(!itr.hasNext());
164:
165: }
166: }
|