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