← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3904: Fixed bug with Grid.limitGrid method, throwing exception when limit was larger than number of row...

 

------------------------------------------------------------
revno: 3904
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-06-14 18:41:32 +0200
message:
  Fixed bug with Grid.limitGrid method, throwing exception when limit was larger than number of rows. Thanks to Olav for reporting.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-05-22 12:36:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-06-14 16:41:32 +0000
@@ -318,7 +318,12 @@
     
     public Grid limitGrid( int limit )
     {
-        if ( limit > 0 )
+        if ( limit < 0 )
+        {
+            throw new IllegalStateException( "Illegal limit: " + limit );
+        }
+        
+        if ( limit > 0 && limit <= getHeight() )
         {
             grid = grid.subList( 0, limit );
         }
@@ -328,7 +333,7 @@
     
     public Grid limitGrid( int startPos, int endPos )
     {
-        if ( startPos < 0 || endPos < startPos || getHeight() < endPos )
+        if ( startPos < 0 || endPos < startPos || endPos > getHeight() )
         {
             throw new IllegalStateException( "Illegal start / end pos: " + startPos + ", " + endPos + ", " + getHeight() );
         }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-02-28 19:51:49 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-06-14 16:41:32 +0000
@@ -237,6 +237,36 @@
     }
     
     @Test
+    public void testLimitShortList()
+    {
+        assertEquals( 4, grid.getRows().size() );
+        
+        grid.limitGrid( 6 );
+        
+        assertEquals( 4, grid.getRows().size() );
+
+        grid.limitGrid( 4 );
+        
+        assertEquals( 4, grid.getRows().size() );
+    }
+    
+    @Test
+    public void testLimits()
+    {
+        assertEquals( 4, grid.getRows().size() );
+        
+        grid.limitGrid( 1, 3 );
+        
+        assertEquals( 2, grid.getRows().size() );
+
+        List<Object> rowA = grid.getRow( 0 );
+        assertTrue( rowA.contains( 21 ) );
+
+        List<Object> rowB = grid.getRow( 1 );        
+        assertTrue( rowB.contains( 31 ) );        
+    }
+    
+    @Test
     public void testSortA()
     {
         Grid grid = new ListGrid();