You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by jh...@apache.org on 2004/10/14 05:38:20 UTC

cvs commit: jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel TestHSSFRow.java

jheight     2004/10/13 20:38:20

  Modified:    src/java/org/apache/poi/hssf/record RowRecord.java
               src/java/org/apache/poi/hssf/usermodel HSSFRow.java
                        HSSFSheet.java
               src/testcases/org/apache/poi/hssf/usermodel TestHSSFRow.java
  Log:
  Implemented row bounds. Will raise an IndexOutOfBoundsException if the excel limits are not observerd.
  
  Interestingly column bounds were previouly implemented, but row bounds were not.
  
  Anyhow this fixes bug 15102
  
  Revision  Changes    Path
  1.10      +6 -0      jakarta-poi/src/java/org/apache/poi/hssf/record/RowRecord.java
  
  Index: RowRecord.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/record/RowRecord.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- RowRecord.java	23 Aug 2004 08:52:33 -0000	1.9
  +++ RowRecord.java	14 Oct 2004 03:38:20 -0000	1.10
  @@ -35,6 +35,12 @@
       implements Comparable
   {
       public final static short sid = 0x208;
  +    
  +    /** The maximum row number that excel can handle (zero bazed) ie 65536 rows is
  +     *  max number of rows.
  +     */
  +    public final static int MAX_ROW_NUMBER = 65535;
  +    
       //private short             field_1_row_number;
       private int             field_1_row_number;
       private short             field_2_first_col;
  
  
  
  1.17      +3 -7      jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
  
  Index: HSSFRow.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- HSSFRow.java	23 Aug 2004 08:52:42 -0000	1.16
  +++ HSSFRow.java	14 Oct 2004 03:38:20 -0000	1.17
  @@ -84,7 +84,6 @@
       //protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
       protected HSSFRow(Workbook book, Sheet sheet, int rowNum)
       {
  -        this.rowNum = rowNum;
           cells = new HashMap(10);   // new ArrayList(INITIAL_CAPACITY);
           this.book = book;
           this.sheet = sheet;
  @@ -94,7 +93,6 @@
           row.setLastCol((short) -1);
           row.setFirstCol((short) -1);
   
  -        // row.setRowNumber(rowNum);
           setRowNum(rowNum);
       }
   
  @@ -110,17 +108,12 @@
   
       protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)
       {
  -        //this.rowNum = rowNum;
           cells = new HashMap();   // ArrayList(INITIAL_CAPACITY);
           this.book = book;
           this.sheet = sheet;
           row = record;
   
  -        // row.setHeight(record.getHeight());
  -        // row.setRowNumber(rowNum);
           setRowNum(record.getRowNumber());
  -
  -//        addColumns(book, sheet, record);
       }
   
       /**
  @@ -206,11 +199,14 @@
       /**
        * set the row number of this row.
        * @param rowNum  the row number (0-based)
  +     * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
        */
   
       //public void setRowNum(short rowNum)
       public void setRowNum(int rowNum)
       {
  +        if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER))
  +          throw new IndexOutOfBoundsException("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">");
           this.rowNum = rowNum;
           if (row != null)
           {
  
  
  
  1.29      +3 -1      jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
  
  Index: HSSFSheet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- HSSFSheet.java	23 Aug 2004 08:52:42 -0000	1.28
  +++ HSSFSheet.java	14 Oct 2004 03:38:20 -0000	1.29
  @@ -242,10 +242,12 @@
           int rownum = lastrow - 1;
           HSSFRow r = getRow(rownum);
   
  -        while (r == null && rownum >= 0)
  +        while (r == null && rownum > 0)
           {
               r = getRow(--rownum);
           }
  +        if (r == null)
  +          return -1;
           return rownum;
       }
   
  
  
  
  1.10      +130 -102  jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java
  
  Index: TestHSSFRow.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestHSSFRow.java	12 Oct 2004 05:49:01 -0000	1.9
  +++ TestHSSFRow.java	14 Oct 2004 03:38:20 -0000	1.10
  @@ -1,102 +1,130 @@
  -
  -/* ====================================================================
  -   Copyright 2002-2004   Apache Software Foundation
  -
  -   Licensed under the Apache License, Version 2.0 (the "License");
  -   you may not use this file except in compliance with the License.
  -   You may obtain a copy of the License at
  -
  -       http://www.apache.org/licenses/LICENSE-2.0
  -
  -   Unless required by applicable law or agreed to in writing, software
  -   distributed under the License is distributed on an "AS IS" BASIS,
  -   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -   See the License for the specific language governing permissions and
  -   limitations under the License.
  -==================================================================== */
  -        
  -
  -package org.apache.poi.hssf.usermodel;
  -
  -import junit.framework.TestCase;
  -
  -import java.io.File;
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
  -
  -import org.apache.poi.util.TempFile;
  -
  -/**
  - * Test HSSFRow is okay.
  - *
  - * @author Glen Stampoultzis (glens at apache.org)
  - */
  -public class TestHSSFRow
  -        extends TestCase
  -{
  -    public TestHSSFRow(String s)
  -    {
  -        super(s);
  -    }
  -
  -    public void testLastAndFirstColumns()
  -            throws Exception
  -    {
  -        HSSFWorkbook workbook = new HSSFWorkbook();
  -        HSSFSheet sheet = workbook.createSheet();
  -        HSSFRow row = sheet.createRow((short) 0);
  -        assertEquals(-1, row.getFirstCellNum());
  -        assertEquals(-1, row.getLastCellNum());
  -
  -        row.createCell((short) 2);
  -        assertEquals(2, row.getFirstCellNum());
  -        assertEquals(2, row.getLastCellNum());
  -
  -        row.createCell((short) 1);
  -        assertEquals(1, row.getFirstCellNum());
  -        assertEquals(2, row.getLastCellNum());
  -
  -    }
  -
  -    public void testRemoveCell()
  -            throws Exception
  -    {
  -        HSSFWorkbook workbook = new HSSFWorkbook();
  -        HSSFSheet sheet = workbook.createSheet();
  -        HSSFRow row = sheet.createRow((short) 0);
  -        assertEquals(-1, row.getLastCellNum());
  -        assertEquals(-1, row.getFirstCellNum());
  -        row.createCell((short) 1);
  -        assertEquals(1, row.getLastCellNum());
  -        assertEquals(1, row.getFirstCellNum());
  -        row.createCell((short) 3);
  -        assertEquals(3, row.getLastCellNum());
  -        assertEquals(1, row.getFirstCellNum());
  -        row.removeCell(row.getCell((short) 3));
  -        assertEquals(1, row.getLastCellNum());
  -        assertEquals(1, row.getFirstCellNum());
  -        row.removeCell(row.getCell((short) 1));
  -        assertEquals(-1, row.getLastCellNum());
  -        assertEquals(-1, row.getFirstCellNum());
  -
  -        // check the row record actually writes it out as 0's
  -        byte[] data = new byte[100];
  -        row.getRowRecord().serialize(0, data);
  -        assertEquals(0, data[6]);
  -        assertEquals(0, data[8]);
  -
  -        File file = TempFile.createTempFile("XXX", "XLS");
  -        FileOutputStream stream = new FileOutputStream(file);
  -        workbook.write(stream);
  -        stream.close();
  -        FileInputStream inputStream = new FileInputStream(file);
  -        workbook = new HSSFWorkbook(inputStream);
  -        sheet = workbook.getSheetAt(0);
  -        stream.close();
  -        file.delete();
  -        assertEquals(-1, sheet.getRow((short) 0).getLastCellNum());
  -        assertEquals(-1, sheet.getRow((short) 0).getFirstCellNum());
  -
  -
  -    }
  -}
  +
  +/* ====================================================================
  +   Copyright 2002-2004   Apache Software Foundation
  +
  +   Licensed under the Apache License, Version 2.0 (the "License");
  +   you may not use this file except in compliance with the License.
  +   You may obtain a copy of the License at
  +
  +       http://www.apache.org/licenses/LICENSE-2.0
  +
  +   Unless required by applicable law or agreed to in writing, software
  +   distributed under the License is distributed on an "AS IS" BASIS,
  +   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  +   See the License for the specific language governing permissions and
  +   limitations under the License.
  +==================================================================== */
  +        
  +
  +package org.apache.poi.hssf.usermodel;
  +
  +import junit.framework.TestCase;
  +
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +
  +import org.apache.poi.util.TempFile;
  +
  +/**
  + * Test HSSFRow is okay.
  + *
  + * @author Glen Stampoultzis (glens at apache.org)
  + */
  +public class TestHSSFRow
  +        extends TestCase
  +{
  +    public TestHSSFRow(String s)
  +    {
  +        super(s);
  +    }
  +
  +    public void testLastAndFirstColumns()
  +            throws Exception
  +    {
  +        HSSFWorkbook workbook = new HSSFWorkbook();
  +        HSSFSheet sheet = workbook.createSheet();
  +        HSSFRow row = sheet.createRow((short) 0);
  +        assertEquals(-1, row.getFirstCellNum());
  +        assertEquals(-1, row.getLastCellNum());
  +
  +        row.createCell((short) 2);
  +        assertEquals(2, row.getFirstCellNum());
  +        assertEquals(2, row.getLastCellNum());
  +
  +        row.createCell((short) 1);
  +        assertEquals(1, row.getFirstCellNum());
  +        assertEquals(2, row.getLastCellNum());
  +
  +    }
  +
  +    public void testRemoveCell()
  +            throws Exception
  +    {
  +        HSSFWorkbook workbook = new HSSFWorkbook();
  +        HSSFSheet sheet = workbook.createSheet();
  +        HSSFRow row = sheet.createRow((short) 0);
  +        assertEquals(-1, row.getLastCellNum());
  +        assertEquals(-1, row.getFirstCellNum());
  +        row.createCell((short) 1);
  +        assertEquals(1, row.getLastCellNum());
  +        assertEquals(1, row.getFirstCellNum());
  +        row.createCell((short) 3);
  +        assertEquals(3, row.getLastCellNum());
  +        assertEquals(1, row.getFirstCellNum());
  +        row.removeCell(row.getCell((short) 3));
  +        assertEquals(1, row.getLastCellNum());
  +        assertEquals(1, row.getFirstCellNum());
  +        row.removeCell(row.getCell((short) 1));
  +        assertEquals(-1, row.getLastCellNum());
  +        assertEquals(-1, row.getFirstCellNum());
  +
  +        // check the row record actually writes it out as 0's
  +        byte[] data = new byte[100];
  +        row.getRowRecord().serialize(0, data);
  +        assertEquals(0, data[6]);
  +        assertEquals(0, data[8]);
  +
  +        File file = TempFile.createTempFile("XXX", "XLS");
  +        FileOutputStream stream = new FileOutputStream(file);
  +        workbook.write(stream);
  +        stream.close();
  +        FileInputStream inputStream = new FileInputStream(file);
  +        workbook = new HSSFWorkbook(inputStream);
  +        sheet = workbook.getSheetAt(0);
  +        stream.close();
  +        file.delete();
  +        assertEquals(-1, sheet.getRow((short) 0).getLastCellNum());
  +        assertEquals(-1, sheet.getRow((short) 0).getFirstCellNum());
  +
  +
  +    }
  +    
  +    public void testRowBounds()
  +            throws Exception
  +    {
  +      HSSFWorkbook workbook = new HSSFWorkbook();
  +      HSSFSheet sheet = workbook.createSheet();
  +      //Test low row bound
  +      HSSFRow row = sheet.createRow( (short) 0);
  +      //Test low row bound exception      
  +      boolean caughtException = false;
  +      try {
  +        row = sheet.createRow(-1);        
  +      } catch (IndexOutOfBoundsException ex) {
  +        caughtException = true;
  +      }      
  +      assertTrue(caughtException);
  +      //Test high row bound      
  +      row = sheet.createRow(65535);     
  +      //Test high row bound exception           
  +      caughtException = false;
  +      try {
  +        row = sheet.createRow(65536);        
  +      } catch (IndexOutOfBoundsException ex) {
  +        caughtException = true;
  +      }      
  +      assertTrue(caughtException);
  +    }
  +    
  +}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org