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.OptGraphBuilder;
026: import org.geotools.graph.structure.Graph;
027:
028: public class OptGraphSerializerTest extends TestCase {
029: private OptGraphBuilder m_builder;
030: private OptGraphBuilder m_rebuilder;
031: private SerializedReaderWriter m_serializer;
032:
033: public OptGraphSerializerTest(String name) {
034: super (name);
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039:
040: m_builder = createBuilder();
041: m_rebuilder = createBuilder();
042: m_serializer = new SerializedReaderWriter();
043: m_serializer.setProperty(SerializedReaderWriter.BUILDER,
044: rebuilder());
045: }
046:
047: /**
048: * Create a simple graph with no bifurcations and serialize, then deserialize
049: * <BR>
050: * <BR>
051: * Expected: 1. before and after graph should have same structure.
052: *
053: */
054: public void test_0() {
055: final int nnodes = 100;
056: Object[] obj = GraphTestUtil.buildNoBifurcations(builder(),
057: nnodes);
058:
059: final Map node2id = (Map) obj[2];
060: final Map edge2id = (Map) obj[3];
061:
062: try {
063: File victim = File.createTempFile("graph", null);
064: victim.deleteOnExit();
065:
066: serializer().setProperty(SerializedReaderWriter.FILENAME,
067: victim.getAbsolutePath());
068:
069: serializer().write(builder().getGraph());
070:
071: Graph before = builder().getGraph();
072: Graph after = serializer().read();
073:
074: //ensure same number of nodes and edges
075: assertTrue(before.getNodes().size() == after.getNodes()
076: .size());
077: assertTrue(before.getEdges().size() == after.getEdges()
078: .size());
079:
080: //ensure two nodes of degree 1, and nnodes-2 nodes of degree 2
081: assertTrue(after.getNodesOfDegree(1).size() == 2);
082: assertTrue(after.getNodesOfDegree(2).size() == nnodes - 2);
083:
084: } catch (Exception e) {
085: e.printStackTrace();
086: assertTrue(false);
087: }
088: }
089:
090: /**
091: * Create a perfect binary tree, serialize it and deserialize it. <BR>
092: * <BR>
093: * Expected: 1. Same structure before and after.
094: *
095: */
096: public void test_1() {
097: final int k = 5;
098: GraphTestUtil.buildPerfectBinaryTree(builder(), k);
099:
100: try {
101: File victim = File.createTempFile("graph", null);
102: victim.deleteOnExit();
103: serializer().setProperty(SerializedReaderWriter.FILENAME,
104: victim.getAbsolutePath());
105:
106: serializer().write(builder().getGraph());
107:
108: Graph before = builder().getGraph();
109: Graph after = serializer().read();
110:
111: assertTrue(after.getNodesOfDegree(2).size() == 1); //root
112: assertTrue(after.getNodesOfDegree(3).size() == Math.pow(2,
113: k) - 2); //internal
114: assertTrue(after.getNodesOfDegree(1).size() == Math.pow(2,
115: k)); //leaves
116: } catch (Exception e) {
117: e.printStackTrace();
118:
119: }
120:
121: }
122:
123: protected OptGraphBuilder createBuilder() {
124: return (new OptGraphBuilder());
125: }
126:
127: protected OptGraphBuilder builder() {
128: return (m_builder);
129: }
130:
131: protected OptGraphBuilder createRebuilder() {
132: return (new OptGraphBuilder());
133: }
134:
135: protected OptGraphBuilder rebuilder() {
136: return (m_rebuilder);
137: }
138:
139: protected SerializedReaderWriter serializer() {
140: return (m_serializer);
141: }
142: }
|