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