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