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.io.standard;
018:
019: import java.io.File;
020: import java.util.Map;
021:
022: import junit.framework.TestCase;
023:
024: import org.geotools.graph.GraphTestUtil;
025: import org.geotools.graph.build.opt.OptDirectedGraphBuilder;
026: import org.geotools.graph.build.opt.OptGraphBuilder;
027: import org.geotools.graph.structure.DirectedNode;
028: import org.geotools.graph.structure.Graph;
029: import org.geotools.graph.structure.GraphVisitor;
030: import org.geotools.graph.structure.Graphable;
031:
032: public class OptDirectedGraphSerializerTest extends TestCase {
033: private OptDirectedGraphBuilder m_builder;
034: private OptDirectedGraphBuilder m_rebuilder;
035: private SerializedReaderWriter m_serializer;
036:
037: public OptDirectedGraphSerializerTest(String name) {
038: super (name);
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: m_builder = createBuilder();
045: m_rebuilder = createBuilder();
046: m_serializer = new SerializedReaderWriter();
047: m_serializer.setProperty(SerializedReaderWriter.BUILDER,
048: rebuilder());
049: }
050:
051: /**
052: * Create a simple graph with no bifurcations and serialize, then deserialize
053: * <BR>
054: * <BR>
055: * Expected: 1. before and after graph should have same structure.
056: *
057: */
058: public void test_0() {
059: final int nnodes = 100;
060: Object[] obj = GraphTestUtil.buildNoBifurcations(builder(),
061: nnodes);
062:
063: final Map node2id = (Map) obj[2];
064: final Map edge2id = (Map) obj[3];
065:
066: try {
067: File victim = File.createTempFile("graph", null);
068: victim.deleteOnExit();
069: serializer().setProperty(SerializedReaderWriter.FILENAME,
070: victim.getAbsolutePath());
071:
072: serializer().write(builder().getGraph());
073:
074: Graph before = builder().getGraph();
075: Graph after = serializer().read();
076:
077: //ensure same number of nodes and edges
078: assertTrue(before.getNodes().size() == after.getNodes()
079: .size());
080: assertTrue(before.getEdges().size() == after.getEdges()
081: .size());
082:
083: //ensure two nodes of degree 1, and nnodes-2 nodes of degree 2
084: GraphVisitor visitor = new GraphVisitor() {
085: public int visit(Graphable component) {
086: DirectedNode node = (DirectedNode) component;
087: if (node.getInDegree() == 0
088: || node.getOutDegree() == 0)
089: return (Graph.PASS_AND_CONTINUE);
090: return (Graph.FAIL_QUERY);
091: }
092: };
093: assertTrue(after.queryNodes(visitor).size() == 2);
094:
095: visitor = new GraphVisitor() {
096: public int visit(Graphable component) {
097: DirectedNode node = (DirectedNode) component;
098: if (node.getInDegree() == 1
099: || node.getOutDegree() == 1)
100: return (Graph.PASS_AND_CONTINUE);
101: return (Graph.FAIL_QUERY);
102: }
103: };
104:
105: assertTrue(after.getNodesOfDegree(2).size() == nnodes - 2);
106:
107: } catch (Exception e) {
108: e.printStackTrace();
109: assertTrue(false);
110: }
111: }
112:
113: /**
114: * Create a perfect binary tree, serialize it and deserialize it. <BR>
115: * <BR>
116: * Expected: 1. Same structure before and after.
117: *
118: */
119: public void test_1() {
120: final int k = 5;
121: GraphTestUtil.buildPerfectBinaryTree(builder(), k);
122:
123: try {
124: File victim = File.createTempFile("graph", null);
125: victim.deleteOnExit();
126: serializer().setProperty(SerializedReaderWriter.FILENAME,
127: victim.getAbsolutePath());
128:
129: serializer().write(builder().getGraph());
130:
131: Graph before = builder().getGraph();
132: Graph after = serializer().read();
133:
134: //ensure same number of nodes and edges
135: assertTrue(before.getNodes().size() == after.getNodes()
136: .size());
137: assertTrue(before.getEdges().size() == after.getEdges()
138: .size());
139:
140: GraphVisitor visitor = new GraphVisitor() {
141: public int visit(Graphable component) {
142: DirectedNode node = (DirectedNode) component;
143: if (node.getInDegree() == 0
144: && node.getOutDegree() == 2)
145: return (Graph.PASS_AND_CONTINUE);
146: return (Graph.FAIL_QUERY);
147: }
148: };
149: assertTrue(after.queryNodes(visitor).size() == 1); //root
150:
151: visitor = new GraphVisitor() {
152: public int visit(Graphable component) {
153: DirectedNode node = (DirectedNode) component;
154: if (node.getInDegree() == 1
155: && node.getOutDegree() == 2)
156: return (Graph.PASS_AND_CONTINUE);
157: return (Graph.FAIL_QUERY);
158: }
159: };
160: assertTrue(after.queryNodes(visitor).size() == Math.pow(2,
161: k) - 2); //internal
162:
163: visitor = new GraphVisitor() {
164: public int visit(Graphable component) {
165: DirectedNode node = (DirectedNode) component;
166: if (node.getInDegree() == 1
167: && node.getOutDegree() == 0)
168: return (Graph.PASS_AND_CONTINUE);
169: return (Graph.FAIL_QUERY);
170: }
171: };
172: assertTrue(after.queryNodes(visitor).size() == Math.pow(2,
173: k)); //leaves
174: } catch (Exception e) {
175: e.printStackTrace();
176: assertTrue(false);
177: }
178: }
179:
180: protected OptDirectedGraphBuilder createBuilder() {
181: return (new OptDirectedGraphBuilder());
182: }
183:
184: protected OptDirectedGraphBuilder builder() {
185: return (m_builder);
186: }
187:
188: protected OptGraphBuilder createRebuilder() {
189: return (new OptGraphBuilder());
190: }
191:
192: protected OptDirectedGraphBuilder rebuilder() {
193: return (m_rebuilder);
194: }
195:
196: protected SerializedReaderWriter serializer() {
197: return (m_serializer);
198: }
199: }
|