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.io.BufferedReader;
021: import java.io.FileNotFoundException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.TestData;
033:
034: import com.vividsolutions.jts.geom.Geometry;
035: import com.vividsolutions.jts.io.ParseException;
036: import com.vividsolutions.jts.io.WKTReader;
037:
038: /**
039: * Base class for test suite. This class is not abstract for the purpose of
040: * {@link TestCaseSupportTest}, but should not be instantiated otherwise.
041: * It should be extented (which is why the constructor is protected).
042: * <p>
043: * Note: a nearly identical copy of this file exists in the {@code plugin/shapefile} module.
044: *
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/indexed/TestCaseSupport.java $
046: * @version $Id: TestCaseSupport.java 26202 2007-07-11 18:23:09Z jgarnett $
047: * @author Ian Schneider
048: * @author Martin Desruisseaux
049: */
050: public abstract class TestCaseSupport extends TestCase {
051: /**
052: * Set to {@code true} if {@code println} are wanted during normal execution.
053: * It doesn't apply to message displayed in case of errors.
054: */
055: protected static boolean verbose = false;
056:
057: /**
058: * Stores all temporary files here - delete on tear down.
059: */
060: private final List tmpFiles = new ArrayList();
061:
062: protected TestCaseSupport() {
063: }
064:
065: /**
066: * Creates a new instance of {@code TestCaseSupport} with the given name.
067: */
068: protected TestCaseSupport(final String name) {
069: super (name);
070: }
071:
072: /**
073: * Deletes all temporary files created by {@link #getTempFile}.
074: * This method is automatically run after each test.
075: */
076: protected void tearDown() throws Exception {
077: // it seems that not all files marked as temp will get erased, perhaps
078: // this is because they have been rewritten? Don't know, don't _really_
079: // care, so I'll just delete everything
080: final Iterator f = tmpFiles.iterator();
081: while (f.hasNext()) {
082: File tf = (File) f.next();
083: dieDieDIE(sibling(tf, "dbf"));
084: dieDieDIE(sibling(tf, "shx"));
085: dieDieDIE(sibling(tf, "fix"));
086: dieDieDIE(sibling(tf, "qix"));
087: dieDieDIE(sibling(tf, "grx"));
088: dieDieDIE(sibling(tf, "prj"));
089: tf.delete();
090: f.remove();
091: }
092: super .tearDown();
093: }
094:
095: private void dieDieDIE(File file) {
096: if (file.exists()) {
097: if (file.delete()) {
098: // dead
099: } else {
100: file.deleteOnExit(); // dead later
101: }
102: }
103: }
104:
105: /**
106: * Helper method for {@link #tearDown}.
107: */
108: protected static File sibling(final File f, final String ext) {
109: return new File(f.getParent(), sibling(f.getName(), ext));
110: }
111:
112: /**
113: * Helper method for {@link #copyShapefiles}.
114: */
115: protected static String sibling(String name, final String ext) {
116: final int s = name.lastIndexOf('.');
117: if (s >= 0) {
118: name = name.substring(0, s);
119: }
120: return name + '.' + ext;
121: }
122:
123: /**
124: * Read a geometry of the given name.
125: *
126: * @param wktResource The resource name to load, without its {@code .wkt} extension.
127: * @return The geometry.
128: * @throws IOException if reading failed.
129: */
130: protected Geometry readGeometry(final String wktResource)
131: throws IOException {
132: final BufferedReader stream = TestData.openReader("wkt/"
133: + wktResource + ".wkt");
134: final WKTReader reader = new WKTReader();
135: final Geometry geom;
136: try {
137: geom = reader.read(stream);
138: } catch (ParseException pe) {
139: IOException e = new IOException(
140: "parsing error in resource " + wktResource);
141: e.initCause(pe);
142: throw e;
143: }
144: stream.close();
145: return geom;
146: }
147:
148: /**
149: * Returns the first feature in the given feature collection.
150: */
151: protected Feature firstFeature(FeatureCollection fc) {
152: return fc.features().next();
153: }
154:
155: /**
156: * Creates a temporary file, to be automatically deleted at the end of the test suite.
157: */
158: protected File getTempFile() throws IOException {
159: File tmpFile = File.createTempFile("test-shp", ".shp");
160: assertTrue(tmpFile.isFile());
161: // keep track of all temp files so we can delete them
162: tmpFiles.add(tmpFile);
163: tmpFile.deleteOnExit();
164: return tmpFile;
165: }
166:
167: /**
168: * Copies the specified shape file into the {@code test-data} directory, together with its
169: * sibling ({@code .dbf}, {@code .shp}, {@code .shx} and {@code .prj} files).
170: */
171: protected File copyShapefiles(final String name) throws IOException {
172: if (this .getClass().getResourceAsStream("test-data") == null) {
173: String root = getClass().getResource(".").getFile();
174: File file = new File(root + "/" + "test-data");
175: if (file.mkdirs()) {
176: file.deleteOnExit();
177: }
178: }
179: assertTrue(TestData.copy(this , sibling(name, "dbf")).canRead());
180: assertTrue(TestData.copy(this , sibling(name, "shp")).canRead());
181: try {
182: assertTrue(TestData.copy(this , sibling(name, "shx"))
183: .canRead());
184: } catch (FileNotFoundException e) {
185: // Ignore: this file is optional.
186: }
187: try {
188: assertTrue(TestData.copy(this , sibling(name, "prj"))
189: .canRead());
190: } catch (FileNotFoundException e) {
191: // Ignore: this file is optional.
192: }
193: try {
194: assertTrue(TestData.copy(this , sibling(name, "grx"))
195: .canRead());
196: } catch (FileNotFoundException e) {
197: // Ignore: this file is optional.
198: }
199: try {
200: assertTrue(TestData.copy(this , sibling(name, "fix"))
201: .canRead());
202: } catch (FileNotFoundException e) {
203: // Ignore: this file is optional.
204: }
205: try {
206: assertTrue(TestData.copy(this , sibling(name, "qix"))
207: .canRead());
208: } catch (FileNotFoundException e) {
209: // Ignore: this file is optional.
210: }
211: return TestData.copy(this , name);
212: }
213:
214: /**
215: * Returns the test suite for the given class.
216: */
217: public static Test suite(Class c) {
218: return new TestSuite(c);
219: }
220: }
|