Skip to content

Commit

Permalink
Migrate all shapefile tests to junit 4
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Apr 7, 2013
1 parent 37935ee commit c951a61
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
*/
package org.geotools.data.shapefile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geotools.TestData;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.dbf.DbaseFileHeader;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.data.shapefile.dbf.DbaseFileWriter;
import org.geotools.data.shapefile.files.ShpFiles;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
*
Expand All @@ -51,36 +57,31 @@ public class DbaseFileTest extends TestCaseSupport {

private ShpFiles shpFiles;

public DbaseFileTest(String testName) throws IOException {
super(testName);
}

public static void main(String[] args) {
// verbose = true;
junit.textui.TestRunner.run(suite(DbaseFileTest.class));
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
shpFiles = new ShpFiles(TestData.url(TEST_FILE));
dbf = new DbaseFileReader(shpFiles, false,
ShapefileDataStore.DEFAULT_STRING_CHARSET);
}

@Test
public void testNumberofColsLoaded() {
assertEquals("Number of attributes found incorect", 252, dbf
.getHeader().getNumFields());
}

protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
dbf.close();
super.tearDown();
}

@Test
public void testNumberofRowsLoaded() {
assertEquals("Number of rows", 49, dbf.getHeader().getNumRecords());
}

@Test
public void testDataLoaded() throws Exception {
Object[] attrs = new Object[dbf.getHeader().getNumFields()];
dbf.readEntry(attrs);
Expand All @@ -89,6 +90,7 @@ public void testDataLoaded() throws Exception {
((Double) attrs[4]).doubleValue(), 0.001);
}

@Test
public void testRowVsEntry() throws Exception {
Object[] attrs = new Object[dbf.getHeader().getNumFields()];
DbaseFileReader dbf2 = new DbaseFileReader(shpFiles, false,
Expand All @@ -105,6 +107,7 @@ public void testRowVsEntry() throws Exception {
dbf2.close();
}

@Test
public void testHeader() throws Exception {
DbaseFileHeader header = new DbaseFileHeader();

Expand All @@ -130,6 +133,7 @@ public void testHeader() throws Exception {
}
}

@Test
public void testAddColumn() throws Exception {
DbaseFileHeader header = new DbaseFileHeader();

Expand All @@ -149,6 +153,7 @@ public void testAddColumn() throws Exception {
}
}

@Test
public void testEmptyFields() throws Exception {
DbaseFileHeader header = new DbaseFileHeader();
header.addColumn("emptyString", 'C', 20, 0);
Expand Down Expand Up @@ -181,6 +186,7 @@ public void testEmptyFields() throws Exception {
f.delete();
}

@Test
public void testFieldFormatter() throws Exception {
DbaseFileWriter.FieldFormatter formatter = new DbaseFileWriter.FieldFormatter(Charset.defaultCharset(), TimeZone.getDefault());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.geotools.data.shapefile;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -12,11 +14,10 @@
import java.util.Date;
import java.util.TimeZone;

import junit.framework.TestCase;

import org.geotools.data.shapefile.dbf.DbaseFileHeader;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.data.shapefile.dbf.DbaseFileWriter;
import org.junit.Test;

/**
* Verifies that null String, Date, Boolean, Integer, Long, Float, and Double
Expand All @@ -29,7 +30,7 @@
*
* @source $URL$
*/
public class DbaseNullSupportTest extends TestCase {
public class DbaseNullSupportTest {
/** declare a specific charset for test portability */
private static final Charset cs;
private static final TimeZone tz;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class DbaseNullSupportTest extends TestCase {
public static void main(String[] args) throws IOException {
new DbaseNullSupportTest().testNulls();
}
@Test
public void testNulls() throws IOException {
File tmp = File.createTempFile("test", ".dbf");
if (!tmp.delete()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.geotools.data.shapefile;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;
Expand All @@ -8,11 +10,11 @@
import org.geotools.TestData;
import org.geotools.data.DataUtilities;
import org.geotools.data.Query;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;
Expand All @@ -33,10 +35,7 @@ public class DuplicateColumnNameReaderTest extends TestCaseSupport {

public final Integer expectedValue = new Integer(20);

public DuplicateColumnNameReaderTest(String name) throws IOException {
super(name);
}

@Test
public void testAttributeReader() throws IOException {
URL u = TestData.url(TestCaseSupport.class, SHPFILE);
File shpFile = DataUtilities.urlToFile(u);
Expand All @@ -57,6 +56,7 @@ public void testAttributeReader() throws IOException {
store.dispose();
}

@Test
public void testAttributeReaderIndexed() throws IOException {
URL u = TestData.url(TestCaseSupport.class, SHPFILE);
File shpFile = DataUtilities.urlToFile(u);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
*/
package org.geotools.data.shapefile;

import java.io.IOException;
import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Iterator;

import org.geotools.TestData;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.junit.Test;

/**
*
Expand All @@ -38,13 +39,10 @@ public class ServiceTest extends TestCaseSupport {

final String TEST_FILE = "shapes/statepop.shp";

public ServiceTest(String testName) throws IOException {
super(testName);
}

/**
* Make sure that the loading mechanism is working properly.
*/
@Test
public void testIsAvailable() {
Iterator list = DataStoreFinder.getAvailableDataStores();
boolean found = false;
Expand All @@ -62,6 +60,7 @@ public void testIsAvailable() {
/**
* Ensure that we can create a DataStore using url OR string url.
*/
@Test
public void testShapefileDataStore() throws Exception {
HashMap params = new HashMap();
params.put("url", TestData.url(TEST_FILE));
Expand All @@ -72,6 +71,7 @@ public void testShapefileDataStore() throws Exception {
ds.dispose();
}

@Test
public void testBadURL() {
HashMap params = new HashMap();
params.put("url", "aaa://bbb.ccc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import static org.geotools.data.shapefile.ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX;
import static org.geotools.data.shapefile.ShapefileDataStoreFactory.FSTYPE;
import static org.geotools.data.shapefile.ShapefileDataStoreFactory.URLP;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.HashMap;
Expand All @@ -32,6 +32,7 @@
import org.geotools.data.QueryCapabilities;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.util.KVP;
import org.junit.After;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.Name;
Expand All @@ -49,11 +50,8 @@ public class ShapefileDataStoreFactoryTest extends TestCaseSupport {
private ShapefileDataStore store = null;
private ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();

public ShapefileDataStoreFactoryTest(String testName) throws IOException {
super(testName);
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
if(store != null) {
store.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
package org.geotools.data.shapefile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -46,7 +54,6 @@
import org.geotools.TestData;
import org.geotools.data.DataStore;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultQuery;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureWriter;
Expand All @@ -67,6 +74,7 @@
import org.geotools.filter.IllegalFilterException;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.junit.After;
import org.junit.Test;
import org.opengis.feature.Feature;
import org.opengis.feature.FeatureVisitor;
Expand Down Expand Up @@ -114,12 +122,8 @@ public class ShapefileDataStoreTest extends TestCaseSupport {
.getFilterFactory2(null);
private ShapefileDataStore store;

public ShapefileDataStoreTest(String testName) throws IOException {
super(testName);
}

@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
if(store != null) {
store.dispose();
}
Expand Down Expand Up @@ -149,7 +153,7 @@ protected SimpleFeatureCollection loadLocalFeaturesM2() throws IOException {
protected SimpleFeatureCollection loadFeatures(String resource, Charset charset,
Query q) throws Exception {
if (q == null)
q = new DefaultQuery();
q = new Query();
URL url = TestData.url(resource);
store = new ShapefileDataStore(url);
store.setCharset(charset);
Expand Down Expand Up @@ -1171,7 +1175,7 @@ public void testGetReaderOptimizations() throws Exception {
ShapefileDataStore s = new ShapefileDataStore(url);

// attributes other than geometry can be ignored here
Query query = new DefaultQuery(s.getSchema().getTypeName(),
Query query = new Query(s.getSchema().getTypeName(),
Filter.INCLUDE, new String[] { "the_geom" });
FeatureReader<SimpleFeatureType, SimpleFeature> reader = s.getFeatureReader(query, Transaction.AUTO_COMMIT);
assertEquals(1, reader.getFeatureType().getAttributeCount());
Expand All @@ -1190,7 +1194,7 @@ public void testGetReaderOptimizations() throws Exception {
.getEnvelopeInternal(), null);
Filter gf = ff.bbox(ff.property("the_geom"), bounds);

query = new DefaultQuery(s.getSchema().getTypeName(), gf,
query = new Query(s.getSchema().getTypeName(), gf,
new String[] { "the_geom" });

reader.close();
Expand All @@ -1205,7 +1209,7 @@ public void testGetReaderOptimizations() throws Exception {
// file please
Filter cf = ff
.equals(ff.property("STATE_NAME"), ff.literal("Illinois"));
query = new DefaultQuery(s.getSchema().getTypeName(), cf,
query = new Query(s.getSchema().getTypeName(), cf,
new String[] { "the_geom" });
reader = s.getFeatureReader(query, Transaction.AUTO_COMMIT);
assertEquals(1, reader.getFeatureType().getAttributeCount());
Expand Down Expand Up @@ -1437,12 +1441,12 @@ public void testCountTransaction() throws Exception {
String typeName = store.getSchema().getTypeName();
Id id = ff.id(Collections.singleton(ff.featureId(fid)));

assertEquals(-1, store.getCount(new DefaultQuery(typeName, id)));
assertEquals(-1, store.getCount(new Query(typeName, id)));
assertEquals(1, count(ds, typeName, id, t));

store.removeFeatures(id);

assertEquals(-1, store.getCount(new DefaultQuery(store.getSchema().getTypeName(), id)));
assertEquals(-1, store.getCount(new Query(store.getSchema().getTypeName(), id)));
assertEquals(initialCount - 1, count(ds, typeName, Filter.INCLUDE, t));
assertEquals(0, count(ds, typeName, id, t));
ds.dispose();
Expand All @@ -1454,7 +1458,7 @@ private int count(DataStore ds, String typeName, Filter filter) throws Exception

private int count(DataStore ds, String typeName, Filter filter, Transaction t) throws Exception {
FeatureReader<SimpleFeatureType, SimpleFeature> reader;
reader = ds.getFeatureReader(new DefaultQuery(typeName, filter), t);
reader = ds.getFeatureReader(new Query(typeName, filter), t);
int count = 0;
try {
while (reader.hasNext()) {
Expand Down
Loading

0 comments on commit c951a61

Please sign in to comment.