|
From: <svn...@os...> - 2011-12-13 11:07:53
|
Author: aaime
Date: 2011-12-13 03:07:42 -0800 (Tue, 13 Dec 2011)
New Revision: 38420
Modified:
trunk/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java
trunk/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java
Log:
[GEOT-3976] MaxSimpleFeatureCollection goes into infinite recursion if start != 0
Modified: trunk/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java
===================================================================
--- trunk/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java 2011-12-13 11:07:14 UTC (rev 38419)
+++ trunk/modules/library/main/src/main/java/org/geotools/feature/collection/MaxSimpleFeatureCollection.java 2011-12-13 11:07:42 UTC (rev 38420)
@@ -99,7 +99,12 @@
}
public int size() {
- return (int) Math.min( Math.max(0, delegate.size() - start), max );
+ int size = delegate.size();
+ if(size < start) {
+ return 0;
+ } else {
+ return (int) Math.min( size - start, max );
+ }
}
public boolean isEmpty() {
Modified: trunk/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java
===================================================================
--- trunk/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java 2011-12-13 11:07:14 UTC (rev 38419)
+++ trunk/modules/library/main/src/test/java/org/geotools/data/store/MaxFeaturesFeatureCollectionTest.java 2011-12-13 11:07:42 UTC (rev 38420)
@@ -28,25 +28,42 @@
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());
+ }
}
|