001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.shapefile.indexed;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021:
022: import junit.framework.AssertionFailedError;
023:
024: import org.geotools.TestData;
025: import org.geotools.data.FeatureSource;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.data.Query;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureCollection;
030: import org.geotools.feature.FeatureIterator;
031: import org.geotools.feature.FeatureType;
032:
033: import com.vividsolutions.jts.geom.Geometry;
034:
035: /**
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/indexed/ShapefileRTreeReadWriteTest.java $
037: * @version $Id: ShapefileRTreeReadWriteTest.java 22666 2006-11-09 03:50:28Z jgarnett $
038: * @author Ian Schneider
039: */
040: public class ShapefileRTreeReadWriteTest extends TestCaseSupport {
041: final String[] files = { "shapes/statepop.shp",
042: "shapes/polygontest.shp", "shapes/pointtest.shp",
043: "shapes/holeTouchEdge.shp", "shapes/stream.shp" };
044: boolean readStarted = false;
045: Exception exception = null;
046:
047: /**
048: * Creates a new instance of ShapefileReadWriteTest
049: */
050: public ShapefileRTreeReadWriteTest(String name) throws IOException {
051: super (name);
052: }
053:
054: public void testAll() throws Throwable {
055: StringBuffer errors = new StringBuffer();
056: Exception bad = null;
057:
058: for (int i = 0, ii = files.length; i < ii; i++) {
059: try {
060: test(files[i]);
061: } catch (Exception e) {
062: e.printStackTrace();
063: errors.append("\nFile " + files[i] + " : "
064: + e.getMessage());
065: bad = e;
066: }
067: }
068:
069: if (errors.length() > 0) {
070: fail(errors.toString(), bad);
071: }
072: }
073:
074: public void fail(String message, Throwable cause) throws Throwable {
075: Throwable fail = new AssertionFailedError(message);
076: fail.initCause(cause);
077: throw fail;
078: }
079:
080: public void testWriteTwice() throws Exception {
081: copyShapefiles("shapes/stream.shp");
082: IndexedShapefileDataStore s1 = new IndexedShapefileDataStore(
083: TestData.url(this , "shapes/stream.shp"));
084: String typeName = s1.getTypeNames()[0];
085: FeatureSource source = s1.getFeatureSource(typeName);
086: FeatureType type = source.getSchema();
087: FeatureCollection one = source.getFeatures();
088:
089: IndexedShapefileDataStoreFactory maker = new IndexedShapefileDataStoreFactory();
090:
091: doubleWrite(type, one, getTempFile(), maker, false);
092: doubleWrite(type, one, getTempFile(), maker, true);
093: }
094:
095: private void doubleWrite(FeatureType type, FeatureCollection one,
096: File tmp, IndexedShapefileDataStoreFactory maker,
097: boolean memorymapped) throws IOException,
098: MalformedURLException {
099: IndexedShapefileDataStore s;
100: s = (IndexedShapefileDataStore) maker.createDataStore(tmp
101: .toURL(), memorymapped);
102:
103: s.createSchema(type);
104: FeatureStore store = (FeatureStore) s.getFeatureSource(type
105: .getTypeName());
106:
107: store.addFeatures(one);
108: store.addFeatures(one);
109:
110: s = new IndexedShapefileDataStore(tmp.toURL());
111: assertEquals(one.size() * 2, store.getCount(Query.ALL));
112: }
113:
114: void test(String f) throws Exception {
115: copyShapefiles(f); // Work on File rather than URL from JAR.
116: IndexedShapefileDataStore s = new IndexedShapefileDataStore(
117: TestData.url(f));
118: String typeName = s.getTypeNames()[0];
119: FeatureSource source = s.getFeatureSource(typeName);
120: FeatureType type = source.getSchema();
121: FeatureCollection one = source.getFeatures();
122:
123: IndexedShapefileDataStoreFactory maker = new IndexedShapefileDataStoreFactory();
124: test(type, one, getTempFile(), maker, false);
125: test(type, one, getTempFile(), maker, true);
126: }
127:
128: private void test(FeatureType type, FeatureCollection one,
129: File tmp, IndexedShapefileDataStoreFactory maker,
130: boolean memorymapped) throws IOException,
131: MalformedURLException, Exception {
132: IndexedShapefileDataStore s;
133: String typeName;
134: s = (IndexedShapefileDataStore) maker.createDataStore(tmp
135: .toURL(), memorymapped);
136:
137: s.createSchema(type);
138:
139: FeatureStore store = (FeatureStore) s.getFeatureSource(type
140: .getTypeName());
141: store.addFeatures(one);
142:
143: s = new IndexedShapefileDataStore(tmp.toURL());
144: typeName = s.getTypeNames()[0];
145:
146: FeatureCollection two = s.getFeatureSource(typeName)
147: .getFeatures();
148:
149: compare(one.features(), two.features());
150: }
151:
152: static void compare(FeatureIterator fs1, FeatureIterator fs2)
153: throws Exception {
154:
155: int i = 0;
156:
157: while (fs1.hasNext()) {
158: Feature f1 = fs1.next();
159: Feature f2 = fs2.next();
160:
161: if ((i++ % 50) == 0) {
162: if (verbose) {
163: System.out.print("*");
164: }
165: }
166:
167: compare(f1, f2);
168: }
169: }
170:
171: static void compare(Feature f1, Feature f2) throws Exception {
172: if (f1.getNumberOfAttributes() != f2.getNumberOfAttributes()) {
173: throw new Exception("Unequal number of attributes");
174: }
175:
176: for (int i = 0; i < f1.getNumberOfAttributes(); i++) {
177: Object att1 = f1.getAttribute(i);
178: Object att2 = f2.getAttribute(i);
179:
180: if (att1 instanceof Geometry && att2 instanceof Geometry) {
181: Geometry g1 = ((Geometry) att1);
182: Geometry g2 = ((Geometry) att2);
183: g1.normalize();
184: g2.normalize();
185:
186: if (!g1.equalsExact(g2)) {
187: throw new Exception("Different geometries (" + i
188: + "):\n" + g1 + "\n" + g2);
189: }
190: } else {
191: if (!att1.equals(att2)) {
192: throw new Exception("Different attribute (" + i
193: + "): [" + att1 + "] - [" + att2 + "]");
194: }
195: }
196: }
197: }
198:
199: public static final void main(String[] args) throws Exception {
200: verbose = true;
201: junit.textui.TestRunner
202: .run(suite(ShapefileRTreeReadWriteTest.class));
203: }
204: }
|