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