Permalink
Browse files

sort out jdbc module, mostly use of FeatureIterator with a few cases …

…of needing to use FeatureStore to add to a collection
  • Loading branch information...
1 parent 6c6efd4 commit 20bc02c762742921fa7096b6de0fb5265dc533fa @jodygarnett jodygarnett committed Oct 30, 2012
@@ -775,7 +775,7 @@ public Object getGmlObject(GmlObjectId id, Hints hints) throws IOException {
}
}
finally {
- features.close( fi );
+ fi.close();
}
}
@@ -23,6 +23,7 @@
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
+import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
@@ -79,7 +80,7 @@ public void testListenerCalled() throws IOException {
b.set(aname("geometry"), new GeometryFactory().createPoint(new Coordinate(i, i)));
collection.add(b.buildFeature(null));
}
- featureStore.addFeatures(collection);
+ featureStore.addFeatures((SimpleFeatureCollection)collection);
t.commit();
assertTrue(mockListener.onBorrowCalled);
assertTrue(mockListener.onReleaseCalled);
@@ -1018,9 +1018,12 @@ public void testGetFeatureStoreModifyFeatures1() throws IOException {
SimpleFeatureCollection results = road.getFeatures(td.rd1Filter);
SimpleFeatureIterator features = results.features();
- assertTrue(features.hasNext());
- assertEquals(5, ((Number)features.next().getAttribute(aname("id"))).intValue());
- results.close(features);
+ try {
+ assertTrue(features.hasNext());
+ assertEquals(5, ((Number) features.next().getAttribute(aname("id"))).intValue());
+ } finally {
+ features.close();
+ }
}
public void testGetFeatureStoreModifyFeatures2() throws IOException {
@@ -1038,9 +1041,12 @@ public void testGetFeatureStoreModifyFeatures2() throws IOException {
SimpleFeatureCollection results = road.getFeatures(td.rd1Filter);
SimpleFeatureIterator features = results.features();
- assertTrue(features.hasNext());
- assertEquals("changed", features.next().getAttribute(aname("name")));
- results.close(features);
+ try {
+ assertTrue(features.hasNext());
+ assertEquals("changed", features.next().getAttribute(aname("name")));
+ } finally {
+ features.close();
+ }
}
/**
@@ -1651,12 +1657,15 @@ public void testGeneralization() throws Exception {
return;
SimpleFeatureCollection fColl = fs.getFeatures();
- SimpleFeatureIterator iterator =fColl.features();
- Geometry original = null;
- if (iterator.hasNext())
- original = (Geometry) iterator.next().getDefaultGeometry();
- fColl.close(iterator);
-
+ SimpleFeatureIterator iterator = fColl.features();
+ Geometry original = null;
+ try {
+ if (iterator.hasNext()) {
+ original = (Geometry) iterator.next().getDefaultGeometry();
+ }
+ } finally {
+ iterator.close();
+ }
double width = original.getEnvelope().getEnvelopeInternal().getWidth();
DefaultQuery query = new DefaultQuery();
@@ -1665,11 +1674,14 @@ public void testGeneralization() throws Exception {
Geometry generalized = null;
fColl = fs.getFeatures(query);
- iterator =fColl.features();
- if (iterator.hasNext())
- generalized = (Geometry) iterator.next().getDefaultGeometry();
- fColl.close(iterator);
-
+ iterator = fColl.features();
+ try {
+ if (iterator.hasNext()) {
+ generalized = (Geometry) iterator.next().getDefaultGeometry();
+ }
+ } finally {
+ iterator.close();
+ }
assertTrue(original.getNumPoints()>=generalized.getNumPoints());
}
@@ -1680,25 +1692,30 @@ public void testSimplification() throws Exception {
return;
SimpleFeatureCollection fColl = fs.getFeatures();
- SimpleFeatureIterator iterator =fColl.features();
- Geometry original = null;
- if (iterator.hasNext())
- original = (Geometry) iterator.next().getDefaultGeometry();
- fColl.close(iterator);
-
+ SimpleFeatureIterator iterator = fColl.features();
+ Geometry original = null;
+ try {
+ if (iterator.hasNext()) {
+ original = (Geometry) iterator.next().getDefaultGeometry();
+ }
+ } finally {
+ iterator.close();
+ }
double width = original.getEnvelope().getEnvelopeInternal().getWidth();
- DefaultQuery query = new DefaultQuery();
+ Query query = new Query();
Hints hints = new Hints(Hints.GEOMETRY_SIMPLIFICATION,width/2);
query.setHints(hints);
Geometry simplified = null;
fColl = fs.getFeatures(query);
- iterator =fColl.features();
- if (iterator.hasNext())
- simplified = (Geometry) iterator.next().getDefaultGeometry();
- fColl.close(iterator);
-
+ iterator = fColl.features();
+ try {
+ if (iterator.hasNext())
+ simplified = (Geometry) iterator.next().getDefaultGeometry();
+ } finally {
+ iterator.close();
+ }
assertTrue(original.getNumPoints()>=simplified.getNumPoints());
}
}
@@ -68,9 +68,11 @@ public void testFeatureCollection() throws Exception {
assertEquals( 0, features.size() );
FeatureIterator i = features.features();
- assertFalse( i.hasNext() );
- features.close( i );
-
+ try {
+ assertFalse(i.hasNext());
+ } finally {
+ i.close();
+ }
}
}
@@ -20,7 +20,10 @@
import java.util.Iterator;
import com.vividsolutions.jts.geom.Point;
+
+import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection;
+import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
@@ -41,11 +44,12 @@
*/
public abstract class JDBCFeatureCollectionTest extends JDBCTestSupport {
SimpleFeatureCollection collection;
+ JDBCFeatureStore source;
protected void connect() throws Exception {
super.connect();
- JDBCFeatureStore source = (JDBCFeatureStore) dataStore.getFeatureSource(tname("ft1"));
+ source = (JDBCFeatureStore) dataStore.getFeatureSource(tname("ft1"));
collection = source.getFeatures();
}
@@ -92,7 +96,6 @@ public void testSubCollection() throws Exception {
SimpleFeatureCollection sub = collection.subCollection(f);
assertNotNull(sub);
-
assertEquals(1, sub.size());
ReferencedEnvelope exp = new ReferencedEnvelope(1, 1, 1, 1, CRS.decode("EPSG:4326"));
@@ -102,9 +105,6 @@ public void testSubCollection() throws Exception {
assertEquals(exp.getMinY(), act.getMinY(), 0.1);
assertEquals(exp.getMaxX(), act.getMaxX(), 0.1);
assertEquals(exp.getMaxY(), act.getMaxY(), 0.1);
-
- sub.clear();
- assertEquals(2, collection.size());
}
public void testAdd() throws IOException {
@@ -117,37 +117,32 @@ public void testAdd() throws IOException {
SimpleFeature feature = b.buildFeature(null);
assertEquals(3, collection.size());
- collection.add(feature);
- assertEquals(4, collection.size());
-
- Iterator i = collection.iterator();
- boolean found = false;
+ source.addFeatures( DataUtilities.collection( feature ));
- while (i.hasNext()) {
- SimpleFeature f = (SimpleFeature) i.next();
+ assertEquals(4, collection.size());
- if ("three".equals(f.getAttribute(aname("stringProperty")))) {
- assertEquals(feature.getAttribute(aname("doubleProperty")),
- f.getAttribute(aname("doubleProperty")));
- assertEquals(feature.getAttribute(aname("stringProperty")),
- f.getAttribute(aname("stringProperty")));
- assertEquals(feature.getAttribute(aname("geometry")),
- f.getAttribute(aname("geometry")));
- found = true;
+ SimpleFeatureIterator i = collection.features();
+ try {
+ boolean found = false;
+
+ while (i.hasNext()) {
+ SimpleFeature f = (SimpleFeature) i.next();
+
+ if ("three".equals(f.getAttribute(aname("stringProperty")))) {
+ assertEquals(feature.getAttribute(aname("doubleProperty")),
+ f.getAttribute(aname("doubleProperty")));
+ assertEquals(feature.getAttribute(aname("stringProperty")),
+ f.getAttribute(aname("stringProperty")));
+ assertEquals(feature.getAttribute(aname("geometry")),
+ f.getAttribute(aname("geometry")));
+ found = true;
+ }
}
+ assertTrue(found);
+ }
+ finally {
+ i.close();
}
-
- assertTrue(found);
-
- collection.close(i);
}
- public void testClear() throws IOException {
- collection.clear();
-
- Iterator i = collection.iterator();
- assertFalse(i.hasNext());
-
- collection.close(i);
- }
}
Oops, something went wrong.

0 comments on commit 20bc02c

Please sign in to comment.