001: /*
002: * Geotools2 - 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: */
017: package org.geotools.data.shapefile;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.net.MalformedURLException;
024:
025: import junit.framework.AssertionFailedError;
026:
027: import org.geotools.data.DataUtilities;
028: import org.geotools.data.FeatureReader;
029: import org.geotools.data.FeatureSource;
030: import org.geotools.data.FeatureStore;
031: import org.geotools.feature.Feature;
032: import org.geotools.feature.FeatureCollection;
033: import org.geotools.feature.FeatureIterator;
034: import org.geotools.feature.FeatureType;
035: import org.geotools.TestData;
036:
037: import com.vividsolutions.jts.geom.Geometry;
038:
039: /**
040: *
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/ShapefileReadWriteTest.java $
042: * @version $Id: ShapefileReadWriteTest.java 23485 2006-12-15 07:34:28Z jgarnett $
043: * @author Ian Schneider
044: */
045: public class ShapefileReadWriteTest extends TestCaseSupport {
046: final String[] files = { "shapes/statepop.shp",
047: "shapes/polygontest.shp", "shapes/pointtest.shp",
048: "shapes/holeTouchEdge.shp", "shapes/stream.shp" };
049:
050: /** Creates a new instance of ShapefileReadWriteTest */
051: public ShapefileReadWriteTest(String name) throws IOException {
052: super (name);
053: }
054:
055: public void testAll() {
056: StringBuffer errors = new StringBuffer();
057: Exception bad = null;
058: for (int i = 0, ii = files.length; i < ii; i++) {
059: try {
060: test(files[i]);
061: } catch (Exception e) {
062: System.out.println("File failed:" + files[i] + " " + e);
063: e.printStackTrace();
064: errors.append("\nFile " + files[i] + " : "
065: + e.getMessage());
066: bad = e;
067: }
068: }
069: if (errors.length() > 0) {
070: fail(errors.toString(), bad);
071: }
072: }
073:
074: boolean readStarted = false;
075:
076: Exception exception = null;
077:
078: public void testConcurrentReadWrite() throws Exception {
079: System.gc();
080: System.runFinalization(); // If some streams are still open, it may help to close them.
081: final File file = getTempFile();
082: Runnable reader = new Runnable() {
083: public void run() {
084: int cutoff = 0;
085: FileInputStream fr = null;
086: try {
087: fr = new FileInputStream(file);
088: try {
089: fr.read();
090: } catch (IOException e1) {
091: exception = e1;
092: return;
093: }
094: // if (verbose) {
095: // System.out.println("locked");
096: // }
097: readStarted = true;
098: while (cutoff < 10) {
099: synchronized (this ) {
100: try {
101: try {
102: fr.read();
103: } catch (IOException e) {
104: exception = e;
105: return;
106: }
107: wait(500);
108: cutoff++;
109: } catch (InterruptedException e) {
110: cutoff = 10;
111: }
112: }
113: }
114: } catch (FileNotFoundException e) {
115: assertTrue(false);
116: } finally {
117: if (fr != null) {
118: try {
119: fr.close();
120: } catch (IOException e) {
121: exception = e;
122: return;
123: }
124: }
125: }
126: }
127: };
128: Thread readThread = new Thread(reader);
129: readThread.start();
130: while (!readStarted) {
131: if (exception != null) {
132: throw exception;
133: }
134: Thread.sleep(100);
135: }
136: test(files[0]);
137: }
138:
139: private static void fail(String message, Throwable cause) {
140: AssertionFailedError fail = new AssertionFailedError(message);
141: fail.initCause(cause);
142: throw fail;
143: }
144:
145: private void test(String f) throws Exception {
146: copyShapefiles(f); // Work on File rather than URL from JAR.
147: ShapefileDataStore s = new ShapefileDataStore(TestData.url(
148: this , f));
149: String typeName = s.getTypeNames()[0];
150: FeatureSource source = s.getFeatureSource(typeName);
151: FeatureType type = source.getSchema();
152: FeatureCollection one = source.getFeatures();
153: File tmp = getTempFile();
154:
155: ShapefileDataStoreFactory maker = new ShapefileDataStoreFactory();
156: test(type, one, tmp, maker, true);
157:
158: File tmp2 = getTempFile(); // TODO consider reuse tmp results in failure
159: test(type, one, tmp2, maker, false);
160: }
161:
162: private void test(FeatureType type, FeatureCollection original,
163: File tmp, ShapefileDataStoreFactory maker,
164: boolean memorymapped) throws IOException,
165: MalformedURLException, Exception {
166:
167: ShapefileDataStore shapefile;
168: String typeName;
169: shapefile = (ShapefileDataStore) maker.createDataStore(tmp
170: .toURL(), memorymapped);
171:
172: shapefile.createSchema(type);
173:
174: FeatureStore store = (FeatureStore) shapefile
175: .getFeatureSource(type.getTypeName());
176:
177: store.addFeatures(original);
178:
179: FeatureCollection copy = store.getFeatures();
180: compare(original, copy);
181:
182: if (true) {
183: // review open
184: ShapefileDataStore review = new ShapefileDataStore(tmp
185: .toURL(), tmp.toURI(), memorymapped);
186: typeName = review.getTypeNames()[0];
187: FeatureSource featureSource = featureSource = review
188: .getFeatureSource(typeName);
189: FeatureCollection again = featureSource.getFeatures();
190:
191: compare(copy, again);
192: compare(original, again);
193: }
194: }
195:
196: static void compare(FeatureCollection one, FeatureCollection two)
197: throws Exception {
198:
199: if (one.size() != two.size()) {
200: throw new Exception("Number of Features unequal : "
201: + one.size() + " != " + two.size());
202: }
203:
204: FeatureIterator iterator1 = one.features();
205: FeatureIterator iterator2 = two.features();
206:
207: int i = 0;
208: while (iterator1.hasNext()) {
209: Feature f1 = iterator1.next();
210: Feature f2 = iterator2.next();
211: compare(f1, f2);
212: }
213: iterator1.close();
214: iterator2.close();
215: }
216:
217: static void compare(Feature f1, Feature f2) throws Exception {
218:
219: if (f1.getNumberOfAttributes() != f2.getNumberOfAttributes()) {
220: throw new Exception("Unequal number of attributes");
221: }
222:
223: for (int i = 0; i < f1.getNumberOfAttributes(); i++) {
224: Object att1 = f1.getAttribute(i);
225: Object att2 = f2.getAttribute(i);
226: if (att1 instanceof Geometry && att2 instanceof Geometry) {
227: Geometry g1 = ((Geometry) att1);
228: Geometry g2 = ((Geometry) att2);
229: g1.normalize();
230: g2.normalize();
231: if (!g1.equalsExact(g2)) {
232: throw new Exception("Different geometries (" + i
233: + "):\n" + g1 + "\n" + g2);
234: }
235: } else {
236: if (!att1.equals(att2)) {
237: throw new Exception("Different attribute (" + i
238: + "): [" + att1 + "] - [" + att2 + "]");
239: }
240: }
241: }
242:
243: }
244:
245: public static final void main(String[] args) throws Exception {
246: //verbose = true;
247: junit.textui.TestRunner
248: .run(suite(ShapefileReadWriteTest.class));
249: }
250: }
|