Author: aaime
Date: 2011-12-13 01:39:43 -0800 (Tue, 13 Dec 2011)
New Revision: 38415
Modified:
branches/2.7.x/modules/library/main/src/main/java/org/geotools/data/store/MaxFeaturesIterator.java
branches/2.7.x/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java
branches/2.7.x/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java
Log:
[GEOT-3976] MaxSimpleFeatureCollection goes into infinite recursion if start != 0
Modified: branches/2.7.x/modules/library/main/src/main/java/org/geotools/data/store/MaxFeaturesIterator.java
===================================================================
--- branches/2.7.x/modules/library/main/src/main/java/org/geotools/data/store/MaxFeaturesIterator.java 2011-12-13 09:39:22 UTC (rev 38414)
+++ branches/2.7.x/modules/library/main/src/main/java/org/geotools/data/store/MaxFeaturesIterator.java 2011-12-13 09:39:43 UTC (rev 38415)
@@ -83,7 +83,7 @@
private void skip() {
if (counter < start) {
- while (hasNext() && counter < start) {
+ while (delegate.hasNext() && counter < start) {
counter++;
F skip = delegate.next(); // skip!
}
Modified: branches/2.7.x/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java
===================================================================
--- branches/2.7.x/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java 2011-12-13 09:39:22 UTC (rev 38414)
+++ branches/2.7.x/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java 2011-12-13 09:39:43 UTC (rev 38415)
@@ -98,7 +98,13 @@
}
public int size() {
- return (int) Math.min( delegate.size(), max );
+ int size = delegate.size();
+ if(size < start) {
+ return 0;
+ } else {
+ return (int) Math.min( size - start, max );
+ }
+
}
public boolean isEmpty() {
Modified: branches/2.7.x/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java
===================================================================
--- branches/2.7.x/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java 2011-12-13 09:39:22 UTC (rev 38414)
+++ branches/2.7.x/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java 2011-12-13 09:39:43 UTC (rev 38415)
@@ -20,28 +20,44 @@
import org.geotools.feature.collection.MaxSimpleFeatureCollection;
-public class MaxFeaturesFeatureCollectionTest extends
- FeatureCollectionWrapperTestSupport {
+public class MaxFeaturesFeatureCollectionTest extends FeatureCollectionWrapperTestSupport {
- MaxSimpleFeatureCollection max;
-
- protected void setUp() throws Exception {
- super.setUp();
- max = new MaxSimpleFeatureCollection( delegate, 2 );
- }
-
- public void testSize() throws Exception {
- assertEquals( 2, max.size() );
- }
-
- public void testIterator() throws Exception {
-
- Iterator i = max.iterator();
- for ( int x = 0; x < 2; x++ ) {
- assertTrue( i.hasNext() );
- i.next();
- }
-
- assertFalse( i.hasNext() );
- }
+ public void testSize() throws Exception {
+ // in the common case it's as big as the max
+ MaxSimpleFeatureCollection max = new MaxSimpleFeatureCollection(delegate, 2);
+ assertEquals(2, max.size());
+
+ // however if we skip much it's going to be just as big as the remainder
+ max = new MaxSimpleFeatureCollection(delegate, delegate.size() - 1, 10);
+ assertEquals(1, max.size());
+
+ // and if we skip more than the size
+ max = new MaxSimpleFeatureCollection(delegate, delegate.size() + 1, 10);
+ assertEquals(0, max.size());
+ }
+
+ public void testIteratorMax() throws Exception {
+ MaxSimpleFeatureCollection max = new MaxSimpleFeatureCollection(delegate, 2);
+ Iterator i = max.iterator();
+ for (int x = 0; x < 2; x++) {
+ assertTrue(i.hasNext());
+ i.next();
+ }
+
+ assertFalse(i.hasNext());
+ }
+
+ public void testIteratorSkipMax() throws Exception {
+ MaxSimpleFeatureCollection max = new MaxSimpleFeatureCollection(delegate, delegate.size() - 1, 2);
+ Iterator i = max.iterator();
+ assertTrue(i.hasNext());
+ i.next();
+ assertFalse(i.hasNext());
+ }
+
+ public void testIteratorSkipMoreSize() throws Exception {
+ MaxSimpleFeatureCollection max = new MaxSimpleFeatureCollection(delegate, delegate.size() + 1, 2);
+ Iterator i = max.iterator();
+ assertFalse(i.hasNext());
+ }
}
|