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.build.basic;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.geotools.graph.structure.Edge;
025: import org.geotools.graph.structure.Graph;
026: import org.geotools.graph.structure.Node;
027:
028: public class BasicGraphBuilderTest extends TestCase {
029:
030: private BasicGraphBuilder m_builder;
031:
032: public BasicGraphBuilderTest(String name) {
033: super (name);
034: }
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038:
039: m_builder = createBuilder();
040: }
041:
042: public void test_buildNode() {
043: assertTrue(m_builder.getNodes().isEmpty());
044:
045: Node n = m_builder.buildNode();
046:
047: assertTrue(n != null);
048: }
049:
050: public void test_buildEdge() {
051: Node n1 = m_builder.buildNode();
052: Node n2 = m_builder.buildNode();
053:
054: assertTrue(m_builder.getEdges().isEmpty());
055:
056: Edge e = m_builder.buildEdge(n1, n2);
057:
058: assertTrue(e != null);
059: assertTrue(e.getNodeA() == n1 || e.getNodeA() == n2);
060: assertTrue(e.getNodeB() == n1 || e.getNodeB() == n2);
061: }
062:
063: public void test_addNode() {
064: Node n1 = m_builder.buildNode();
065: m_builder.addNode(n1);
066: assertTrue(m_builder.getNodes().size() == 1);
067: assertTrue(m_builder.getNodes().contains(n1));
068: }
069:
070: public void test_addEdge_0() {
071: Node n1 = m_builder.buildNode();
072: Node n2 = m_builder.buildNode();
073:
074: Edge e = m_builder.buildEdge(n1, n2);
075:
076: m_builder.addNode(n1);
077: m_builder.addNode(n2);
078:
079: m_builder.addEdge(e);
080:
081: assertTrue(m_builder.getEdges().size() == 1);
082: assertTrue(m_builder.getEdges().contains(e));
083:
084: assertTrue(n1.getEdges().contains(e));
085: assertTrue(n2.getEdges().contains(e));
086: }
087:
088: public void test_addEdge_1() {
089: //add a loop edge, edge list size ==1 but degree == 2
090: Node n1 = m_builder.buildNode();
091: Edge loop = m_builder.buildEdge(n1, n1);
092:
093: m_builder.addNode(n1);
094: m_builder.addEdge(loop);
095:
096: assertTrue(n1.getEdges().size() == 1);
097: assertTrue(n1.getDegree() == 2);
098: }
099:
100: public void test_removeNode() {
101: Node n1 = m_builder.buildNode();
102: m_builder.addNode(n1);
103:
104: assertTrue(m_builder.getNodes().contains(n1));
105:
106: m_builder.removeNode(n1);
107: assertTrue(m_builder.getNodes().isEmpty());
108: }
109:
110: public void test_removeNodes() {
111: Node n1 = m_builder.buildNode();
112: Node n2 = m_builder.buildNode();
113:
114: m_builder.addNode(n1);
115: m_builder.addNode(n2);
116:
117: assertTrue(m_builder.getNodes().size() == 2);
118: assertTrue(m_builder.getNodes().contains(n1));
119: assertTrue(m_builder.getNodes().contains(n2));
120:
121: List toRemove = new ArrayList();
122: toRemove.add(n1);
123: toRemove.add(n2);
124:
125: m_builder.removeNodes(toRemove);
126:
127: assertTrue(m_builder.getNodes().isEmpty());
128: }
129:
130: public void test_removeEdge_0() {
131: Node n1 = m_builder.buildNode();
132: Node n2 = m_builder.buildNode();
133:
134: Edge e = m_builder.buildEdge(n1, n2);
135:
136: m_builder.addNode(n1);
137: m_builder.addNode(n2);
138: m_builder.addEdge(e);
139:
140: assertTrue(n1.getEdges().contains(e));
141: assertTrue(n2.getEdges().contains(e));
142:
143: m_builder.removeEdge(e);
144: assertTrue(!n1.getEdges().contains(e));
145: assertTrue(!n2.getEdges().contains(e));
146: assertTrue(m_builder.getEdges().isEmpty());
147: }
148:
149: public void test_removeEdge_1() {
150: //test a loop edge
151: Node n1 = m_builder.buildNode();
152:
153: Edge e = m_builder.buildEdge(n1, n1);
154:
155: m_builder.addNode(n1);
156: m_builder.addEdge(e);
157:
158: assertTrue(n1.getEdges().contains(e));
159:
160: m_builder.removeEdge(e);
161: assertTrue(!n1.getEdges().contains(e));
162: assertTrue(m_builder.getEdges().isEmpty());
163: }
164:
165: public void test_removeEdges() {
166: Node n1 = m_builder.buildNode();
167: Node n2 = m_builder.buildNode();
168: Node n3 = m_builder.buildNode();
169: Node n4 = m_builder.buildNode();
170:
171: Edge e1 = m_builder.buildEdge(n1, n2);
172: Edge e2 = m_builder.buildEdge(n3, n4);
173:
174: m_builder.addNode(n1);
175: m_builder.addNode(n2);
176: m_builder.addNode(n3);
177: m_builder.addNode(n4);
178:
179: m_builder.addEdge(e1);
180: m_builder.addEdge(e2);
181:
182: List toRemove = new ArrayList();
183: toRemove.add(e1);
184: toRemove.add(e2);
185:
186: assertTrue(m_builder.getEdges().contains(e1));
187: assertTrue(m_builder.getEdges().contains(e2));
188:
189: m_builder.removeEdges(toRemove);
190:
191: assertTrue(m_builder.getEdges().isEmpty());
192: }
193:
194: public void test_getGraph() {
195: Graph graph = m_builder.getGraph();
196: assertTrue(graph != null);
197: }
198:
199: private BasicGraphBuilder createBuilder() {
200: return (new BasicGraphBuilder());
201: }
202: }
|