41187 - fixed HSSFSheet to properly read xls files without ROW records

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@655278 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-05-11 08:15:39 +00:00
parent 0277016ab5
commit c1a11a8b7c
16 changed files with 644 additions and 1389 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.1-beta2" date="2008-05-??"> <release version="3.1-beta2" date="2008-05-??">
<action dev="POI-DEVELOPERS" type="fix">41187 - fixed HSSFSheet to properly read xls files without ROW records</action>
<action dev="POI-DEVELOPERS" type="fix">44950 - fixed HSSFFormulaEvaluator.evaluateInCell() and Area3DEval.getValue() also added validation for number of elements in AreaEvals</action> <action dev="POI-DEVELOPERS" type="fix">44950 - fixed HSSFFormulaEvaluator.evaluateInCell() and Area3DEval.getValue() also added validation for number of elements in AreaEvals</action>
<action dev="POI-DEVELOPERS" type="fix">42570 - fixed LabelRecord to use empty string instead of null when the length is zero.</action> <action dev="POI-DEVELOPERS" type="fix">42570 - fixed LabelRecord to use empty string instead of null when the length is zero.</action>
<action dev="POI-DEVELOPERS" type="fix">42564 - fixed ArrayPtg to use ConstantValueParser. Fixed a few other ArrayPtg encoding issues.</action> <action dev="POI-DEVELOPERS" type="fix">42564 - fixed ArrayPtg to use ConstantValueParser. Fixed a few other ArrayPtg encoding issues.</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.1-beta2" date="2008-05-??"> <release version="3.1-beta2" date="2008-05-??">
<action dev="POI-DEVELOPERS" type="fix">41187 - fixed HSSFSheet to properly read xls files without ROW records</action>
<action dev="POI-DEVELOPERS" type="fix">44950 - fixed HSSFFormulaEvaluator.evaluateInCell() and Area3DEval.getValue() also added validation for number of elements in AreaEvals</action> <action dev="POI-DEVELOPERS" type="fix">44950 - fixed HSSFFormulaEvaluator.evaluateInCell() and Area3DEval.getValue() also added validation for number of elements in AreaEvals</action>
<action dev="POI-DEVELOPERS" type="fix">42570 - fixed LabelRecord to use empty string instead of null when the length is zero.</action> <action dev="POI-DEVELOPERS" type="fix">42570 - fixed LabelRecord to use empty string instead of null when the length is zero.</action>
<action dev="POI-DEVELOPERS" type="fix">42564 - fixed ArrayPtg to use ConstantValueParser. Fixed a few other ArrayPtg encoding issues.</action> <action dev="POI-DEVELOPERS" type="fix">42564 - fixed ArrayPtg to use ConstantValueParser. Fixed a few other ArrayPtg encoding issues.</action>

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -15,7 +14,6 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
@ -31,20 +29,18 @@ import org.apache.poi.util.LittleEndian;
* @author Jason Height (jheight at chariot dot net dot au) * @author Jason Height (jheight at chariot dot net dot au)
* @version 2.0-pre * @version 2.0-pre
*/ */
public final class RowRecord extends Record implements Comparable {
public class RowRecord public final static short sid = 0x208;
extends Record
implements Comparable
{
public final static short sid = 0x208;
/** The maximum row number that excel can handle (zero bazed) ie 65536 rows is private static final int OPTION_BITS_ALWAYS_SET = 0x0100;
private static final int DEFAULT_HEIGHT_BIT = 0x8000;
/** The maximum row number that excel can handle (zero based) ie 65536 rows is
* max number of rows. * max number of rows.
*/ */
public final static int MAX_ROW_NUMBER = 65535; public final static int MAX_ROW_NUMBER = 65535;
//private short field_1_row_number; private int field_1_row_number;
private int field_1_row_number;
private short field_2_first_col; private short field_2_first_col;
private short field_3_last_col; // plus 1 private short field_3_last_col; // plus 1
private short field_4_height; private short field_4_height;
@ -52,7 +48,8 @@ public class RowRecord
// for generated sheets. // for generated sheets.
private short field_6_reserved; private short field_6_reserved;
private short field_7_option_flags; /** 16 bit options flags */
private int field_7_option_flags;
private static final BitField outlineLevel = BitFieldFactory.getInstance(0x07); private static final BitField outlineLevel = BitFieldFactory.getInstance(0x07);
// bit 3 reserved // bit 3 reserved
@ -62,8 +59,17 @@ public class RowRecord
private static final BitField formatted = BitFieldFactory.getInstance(0x80); private static final BitField formatted = BitFieldFactory.getInstance(0x80);
private short field_8_xf_index; // only if isFormatted private short field_8_xf_index; // only if isFormatted
public RowRecord() public RowRecord(int rowNumber) {
{ field_1_row_number = rowNumber;
field_2_first_col = -1;
field_3_last_col = -1;
field_4_height = (short)DEFAULT_HEIGHT_BIT;
field_4_height = (short)DEFAULT_HEIGHT_BIT;
field_5_optimize = ( short ) 0;
field_6_reserved = ( short ) 0;
field_7_option_flags = OPTION_BITS_ALWAYS_SET; // seems necessary for outlining
field_8_xf_index = ( short ) 0xf;
} }
/** /**
@ -86,7 +92,6 @@ public class RowRecord
protected void fillFields(RecordInputStream in) protected void fillFields(RecordInputStream in)
{ {
//field_1_row_number = LittleEndian.getShort(data, 0 + offset);
field_1_row_number = in.readUShort(); field_1_row_number = in.readUShort();
field_2_first_col = in.readShort(); field_2_first_col = in.readShort();
field_3_last_col = in.readShort(); field_3_last_col = in.readShort();
@ -156,7 +161,7 @@ public class RowRecord
public void setOptionFlags(short options) public void setOptionFlags(short options)
{ {
field_7_option_flags = options; field_7_option_flags = options | OPTION_BITS_ALWAYS_SET;
} }
// option bitfields // option bitfields
@ -169,20 +174,18 @@ public class RowRecord
public void setOutlineLevel(short ol) public void setOutlineLevel(short ol)
{ {
field_7_option_flags = field_7_option_flags = outlineLevel.setValue(field_7_option_flags, ol);
outlineLevel.setShortValue(field_7_option_flags, ol);
} }
/** /**
* set whether or not to colapse this row * set whether or not to collapse this row
* @param c - colapse or not * @param c - collapse or not
* @see #setOptionFlags(short) * @see #setOptionFlags(short)
*/ */
public void setColapsed(boolean c) public void setColapsed(boolean c)
{ {
field_7_option_flags = colapsed.setShortBoolean(field_7_option_flags, field_7_option_flags = colapsed.setBoolean(field_7_option_flags, c);
c);
} }
/** /**
@ -193,8 +196,7 @@ public class RowRecord
public void setZeroHeight(boolean z) public void setZeroHeight(boolean z)
{ {
field_7_option_flags = field_7_option_flags = zeroHeight.setBoolean(field_7_option_flags, z);
zeroHeight.setShortBoolean(field_7_option_flags, z);
} }
/** /**
@ -205,8 +207,7 @@ public class RowRecord
public void setBadFontHeight(boolean f) public void setBadFontHeight(boolean f)
{ {
field_7_option_flags = field_7_option_flags = badFontHeight.setBoolean(field_7_option_flags, f);
badFontHeight.setShortBoolean(field_7_option_flags, f);
} }
/** /**
@ -217,8 +218,7 @@ public class RowRecord
public void setFormatted(boolean f) public void setFormatted(boolean f)
{ {
field_7_option_flags = formatted.setShortBoolean(field_7_option_flags, field_7_option_flags = formatted.setBoolean(field_7_option_flags, f);
f);
} }
// end bitfields // end bitfields
@ -293,7 +293,7 @@ public class RowRecord
public short getOptionFlags() public short getOptionFlags()
{ {
return field_7_option_flags; return (short)field_7_option_flags;
} }
// option bitfields // option bitfields
@ -306,7 +306,7 @@ public class RowRecord
public short getOutlineLevel() public short getOutlineLevel()
{ {
return outlineLevel.getShortValue(field_7_option_flags); return (short)outlineLevel.getValue(field_7_option_flags);
} }
/** /**
@ -410,7 +410,6 @@ public class RowRecord
{ {
LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 0 + offset, sid);
LittleEndian.putShort(data, 2 + offset, ( short ) 16); LittleEndian.putShort(data, 2 + offset, ( short ) 16);
//LittleEndian.putShort(data, 4 + offset, getRowNumber());
LittleEndian.putShort(data, 4 + offset, ( short ) getRowNumber()); LittleEndian.putShort(data, 4 + offset, ( short ) getRowNumber());
LittleEndian.putShort(data, 6 + offset, getFirstCol() == -1 ? (short)0 : getFirstCol()); LittleEndian.putShort(data, 6 + offset, getFirstCol() == -1 ? (short)0 : getFirstCol());
LittleEndian.putShort(data, 8 + offset, getLastCol() == -1 ? (short)0 : getLastCol()); LittleEndian.putShort(data, 8 + offset, getLastCol() == -1 ? (short)0 : getLastCol());
@ -419,7 +418,6 @@ public class RowRecord
LittleEndian.putShort(data, 14 + offset, field_6_reserved); LittleEndian.putShort(data, 14 + offset, field_6_reserved);
LittleEndian.putShort(data, 16 + offset, getOptionFlags()); LittleEndian.putShort(data, 16 + offset, getOptionFlags());
// LittleEndian.putShort(data,18,getOutlineLevel());
LittleEndian.putShort(data, 18 + offset, getXFIndex()); LittleEndian.putShort(data, 18 + offset, getXFIndex());
return getRecordSize(); return getRecordSize();
} }
@ -469,8 +467,7 @@ public class RowRecord
} }
public Object clone() { public Object clone() {
RowRecord rec = new RowRecord(); RowRecord rec = new RowRecord(field_1_row_number);
rec.field_1_row_number = field_1_row_number;
rec.field_2_first_col = field_2_first_col; rec.field_2_first_col = field_2_first_col;
rec.field_3_last_col = field_3_last_col; rec.field_3_last_col = field_3_last_col;
rec.field_4_height = field_4_height; rec.field_4_height = field_4_height;

View File

@ -35,19 +35,17 @@ import java.util.TreeMap;
* @author Jason Height (jheight at chariot dot net dot au) * @author Jason Height (jheight at chariot dot net dot au)
*/ */
public class RowRecordsAggregate public final class RowRecordsAggregate extends Record {
extends Record private int firstrow = -1;
{ private int lastrow = -1;
int firstrow = -1; private Map records = null; // TODO - use a proper key in this map
int lastrow = -1; private int size = 0;
Map records = null;
int size = 0;
/** Creates a new instance of ValueRecordsAggregate */ /** Creates a new instance of ValueRecordsAggregate */
public RowRecordsAggregate() public RowRecordsAggregate()
{ {
records = new TreeMap(); records = new TreeMap();
} }
public void insertRow(RowRecord row) public void insertRow(RowRecord row)
@ -74,15 +72,13 @@ public class RowRecordsAggregate
records.remove(row); records.remove(row);
} }
public RowRecord getRow(int rownum) public RowRecord getRow(int rownum) {
{ // Row must be between 0 and 65535
// Row must be between 0 and 65535 if(rownum < 0 || rownum > 65535) {
if(rownum < 0 || rownum > 65535) { throw new IllegalArgumentException("The row number must be between 0 and 65535");
throw new IllegalArgumentException("The row number must be between 0 and 65535"); }
}
RowRecord row = new RowRecord(); RowRecord row = new RowRecord(rownum);
row.setRowNumber(rownum);
return ( RowRecord ) records.get(row); return ( RowRecord ) records.get(row);
} }
@ -333,7 +329,7 @@ public class RowRecordsAggregate
// Find the start of the group. // Find the start of the group.
int startRow = findStartOfRowOutlineGroup( rowNumber ); int startRow = findStartOfRowOutlineGroup( rowNumber );
RowRecord rowRecord = (RowRecord) getRow( startRow ); RowRecord rowRecord = getRow( startRow );
// Hide all the columns until the end of the group // Hide all the columns until the end of the group
int lastRow = writeHidden( rowRecord, startRow, true ); int lastRow = writeHidden( rowRecord, startRow, true );
@ -358,17 +354,8 @@ public class RowRecordsAggregate
* @return RowRecord created for the passed in row number * @return RowRecord created for the passed in row number
* @see org.apache.poi.hssf.record.RowRecord * @see org.apache.poi.hssf.record.RowRecord
*/ */
public static RowRecord createRow(int row) public static RowRecord createRow(int rowNumber) {
{ return new RowRecord(rowNumber);
RowRecord rowrec = new RowRecord();
//rowrec.setRowNumber(( short ) row);
rowrec.setRowNumber(row);
rowrec.setHeight(( short ) 0xff);
rowrec.setOptimize(( short ) 0x0);
rowrec.setOptionFlags(( short ) 0x100); // seems necessary for outlining
rowrec.setXFIndex(( short ) 0xf);
return rowrec;
} }
public boolean isRowGroupCollapsed( int row ) public boolean isRowGroupCollapsed( int row )
@ -399,12 +386,12 @@ public class RowRecordsAggregate
int endIdx = findEndOfRowOutlineGroup( idx ); int endIdx = findEndOfRowOutlineGroup( idx );
// expand: // expand:
// colapsed bit must be unset // collapsed bit must be unset
// hidden bit gets unset _if_ surrounding groups are expanded you can determine // hidden bit gets unset _if_ surrounding groups are expanded you can determine
// this by looking at the hidden bit of the enclosing group. You will have // this by looking at the hidden bit of the enclosing group. You will have
// to look at the start and the end of the current group to determine which // to look at the start and the end of the current group to determine which
// is the enclosing group // is the enclosing group
// hidden bit only is altered for this outline level. ie. don't uncollapse contained groups // hidden bit only is altered for this outline level. ie. don't un-collapse contained groups
if ( !isRowGroupHiddenByParent( idx ) ) if ( !isRowGroupHiddenByParent( idx ) )
{ {
for ( int i = startIdx; i <= endIdx; i++ ) for ( int i = startIdx; i <= endIdx; i++ )

View File

@ -21,7 +21,6 @@ import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.RowRecord;
@ -37,11 +36,9 @@ public final class HSSFRow implements Comparable {
// used for collections // used for collections
public final static int INITIAL_CAPACITY = 5; public final static int INITIAL_CAPACITY = 5;
//private short rowNum;
private int rowNum; private int rowNum;
private HSSFCell[] cells=new HSSFCell[INITIAL_CAPACITY]; private HSSFCell[] cells=new HSSFCell[INITIAL_CAPACITY];
// private short firstcell = -1;
// private short lastcell = -1;
/** /**
* reference to low level representation * reference to low level representation
@ -61,7 +58,8 @@ public final class HSSFRow implements Comparable {
private Sheet sheet; private Sheet sheet;
protected HSSFRow() // TODO - ditch this constructor
HSSFRow()
{ {
} }
@ -73,18 +71,12 @@ public final class HSSFRow implements Comparable {
* @param rowNum the row number of this row (0 based) * @param rowNum the row number of this row (0 based)
* @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int) * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
*/ */
HSSFRow(HSSFWorkbook book, Sheet sheet, int rowNum)
//protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
protected HSSFRow(HSSFWorkbook book, Sheet sheet, int rowNum)
{ {
this.rowNum = rowNum; this.rowNum = rowNum;
this.book = book; this.book = book;
this.sheet = sheet; this.sheet = sheet;
row = new RowRecord(); row = new RowRecord(rowNum);
row.setOptionFlags( (short)0x100 ); // seems necessary for outlining to work.
row.setHeight((short) 0xff);
row.setLastCol((short) -1);
row.setFirstCol((short) -1);
setRowNum(rowNum); setRowNum(rowNum);
} }
@ -98,8 +90,7 @@ public final class HSSFRow implements Comparable {
* @param record the low level api object this row should represent * @param record the low level api object this row should represent
* @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int) * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
*/ */
HSSFRow(HSSFWorkbook book, Sheet sheet, RowRecord record)
protected HSSFRow(HSSFWorkbook book, Sheet sheet, RowRecord record)
{ {
this.book = book; this.book = book;
this.sheet = sheet; this.sheet = sheet;
@ -200,12 +191,11 @@ public final class HSSFRow implements Comparable {
* @param rowNum the row number (0-based) * @param rowNum the row number (0-based)
* @throws IndexOutOfBoundsException if the row number is not within the range 0-65535. * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
*/ */
public void setRowNum(int rowNum) {
//public void setRowNum(short rowNum) if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER)) {
public void setRowNum(int rowNum) throw new IllegalArgumentException("Invalid row number (" + rowNum
{ + ") outside allowable range (0.." + RowRecord.MAX_ROW_NUMBER + ")");
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; this.rowNum = rowNum;
if (row != null) if (row != null)
{ {
@ -217,8 +207,6 @@ public final class HSSFRow implements Comparable {
* get row number this row represents * get row number this row represents
* @return the row number (0 based) * @return the row number (0 based)
*/ */
//public short getRowNum()
public int getRowNum() public int getRowNum()
{ {
return rowNum; return rowNum;

View File

@ -136,6 +136,7 @@ public final class HSSFSheet {
{ {
int sloc = sheet.getLoc(); int sloc = sheet.getLoc();
RowRecord row = sheet.getNextRow(); RowRecord row = sheet.getNextRow();
boolean rowRecordsAlreadyPresent = row!=null;
while (row != null) while (row != null)
{ {
@ -160,6 +161,18 @@ public final class HSSFSheet {
if ( ( lastrow == null ) || ( lastrow.getRowNum() != cval.getRow() ) ) if ( ( lastrow == null ) || ( lastrow.getRowNum() != cval.getRow() ) )
{ {
hrow = getRow( cval.getRow() ); hrow = getRow( cval.getRow() );
if (hrow == null) {
// Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords
// Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
if (rowRecordsAlreadyPresent) {
// if at least one row record is present, all should be present.
throw new RuntimeException("Unexpected missing row when some rows already present");
}
// create the row record on the fly now.
RowRecord rowRec = new RowRecord(cval.getRow());
sheet.addRow(rowRec);
hrow = createRowFromRecord(rowRec);
}
} }
if ( hrow != null ) if ( hrow != null )
{ {

View File

@ -22,9 +22,7 @@ import junit.framework.TestSuite;
import org.apache.poi.hssf.eventmodel.TestEventRecordFactory; import org.apache.poi.hssf.eventmodel.TestEventRecordFactory;
import org.apache.poi.hssf.eventmodel.TestModelFactory; import org.apache.poi.hssf.eventmodel.TestModelFactory;
import org.apache.poi.hssf.model.TestDrawingManager; import org.apache.poi.hssf.model.AllModelTests;
import org.apache.poi.hssf.model.TestFormulaParser;
import org.apache.poi.hssf.model.TestSheet;
import org.apache.poi.hssf.record.AllRecordTests; import org.apache.poi.hssf.record.AllRecordTests;
import org.apache.poi.hssf.usermodel.AllUserModelTests; import org.apache.poi.hssf.usermodel.AllUserModelTests;
import org.apache.poi.hssf.util.TestAreaReference; import org.apache.poi.hssf.util.TestAreaReference;
@ -50,10 +48,10 @@ public final class HSSFTests {
TestSuite suite = new TestSuite("Tests for org.apache.poi.hssf"); TestSuite suite = new TestSuite("Tests for org.apache.poi.hssf");
// $JUnit-BEGIN$ // $JUnit-BEGIN$
suite.addTest(AllModelTests.suite());
suite.addTest(AllUserModelTests.suite()); suite.addTest(AllUserModelTests.suite());
suite.addTest(AllRecordTests.suite()); suite.addTest(AllRecordTests.suite());
suite.addTest(new TestSuite(TestFormulaParser.class));
suite.addTest(new TestSuite(TestAreaReference.class)); suite.addTest(new TestSuite(TestAreaReference.class));
suite.addTest(new TestSuite(TestCellReference.class)); suite.addTest(new TestSuite(TestCellReference.class));
suite.addTest(new TestSuite(TestRangeAddress.class)); suite.addTest(new TestSuite(TestRangeAddress.class));
@ -61,8 +59,6 @@ public final class HSSFTests {
suite.addTest(new TestSuite(TestSheetReferences.class)); suite.addTest(new TestSuite(TestSheetReferences.class));
suite.addTest(new TestSuite(TestEventRecordFactory.class)); suite.addTest(new TestSuite(TestEventRecordFactory.class));
suite.addTest(new TestSuite(TestModelFactory.class)); suite.addTest(new TestSuite(TestModelFactory.class));
suite.addTest(new TestSuite(TestDrawingManager.class));
suite.addTest(new TestSuite(TestSheet.class));
// $JUnit-END$ // $JUnit-END$
return suite; return suite;
} }

View File

@ -0,0 +1,40 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.model;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Collects all tests for <tt>org.apache.poi.hssf.model</tt>.
*
* @author Josh Micich
*/
public final class AllModelTests {
public static Test suite() {
TestSuite result = new TestSuite(AllModelTests.class.getName());
result.addTestSuite(TestDrawingManager.class);
result.addTestSuite(TestDrawingManager2.class);
result.addTestSuite(TestFormulaParser.class);
result.addTestSuite(TestFormulaParserEval.class);
result.addTestSuite(TestSheet.class);
result.addTestSuite(TestSheetAdditional.class);
return result;
}
}

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -15,7 +14,6 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.model; package org.apache.poi.hssf.model;
@ -34,8 +32,7 @@ import java.util.List;
* *
* @author Glen Stampoultzis (glens at apache.org) * @author Glen Stampoultzis (glens at apache.org)
*/ */
public class TestSheet extends TestCase public final class TestSheet extends TestCase {
{
public void testCreateSheet() throws Exception public void testCreateSheet() throws Exception
{ {
// Check we're adding row and cell aggregates // Check we're adding row and cell aggregates
@ -76,6 +73,21 @@ public class TestSheet extends TestCase
if ((regionsToAdd % 1027) != 0) if ((regionsToAdd % 1027) != 0)
recordsExpected++; recordsExpected++;
assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected); assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected);
// Check we can't add one with invalid date
try {
sheet.addMergedRegion(10, (short)10, 9, (short)12);
fail("Expected an exception to occur");
} catch(IllegalArgumentException e) {
// occurs during successful test
assertEquals("The 'to' row (9) must not be less than the 'from' row (10)", e.getMessage());
}
try {
sheet.addMergedRegion(10, (short)10, 12, (short)9);
fail("Expected an exception to occur");
} catch(IllegalArgumentException e) {
// occurs during successful test
assertEquals("The 'to' col (9) must not be less than the 'from' col (10)", e.getMessage());
}
} }
public void testRemoveMergedRegion() public void testRemoveMergedRegion()
@ -113,9 +125,9 @@ public class TestSheet extends TestCase
MergeCellsRecord merged = new MergeCellsRecord(); MergeCellsRecord merged = new MergeCellsRecord();
merged.addArea(0, (short)0, 1, (short)2); merged.addArea(0, (short)0, 1, (short)2);
records.add(new RowRecord()); records.add(new RowRecord(0));
records.add(new RowRecord()); records.add(new RowRecord(1));
records.add(new RowRecord()); records.add(new RowRecord(2));
records.add(merged); records.add(merged);
Sheet sheet = Sheet.createSheet(records, 0); Sheet sheet = Sheet.createSheet(records, 0);
@ -142,20 +154,11 @@ public class TestSheet extends TestCase
*/ */
public void testRowAggregation() { public void testRowAggregation() {
List records = new ArrayList(); List records = new ArrayList();
RowRecord row = new RowRecord();
row.setRowNumber(0);
records.add(row);
row = new RowRecord();
row.setRowNumber(1);
records.add(row);
records.add(new RowRecord(0));
records.add(new RowRecord(1));
records.add(new StringRecord()); records.add(new StringRecord());
records.add(new RowRecord(2));
row = new RowRecord();
row.setRowNumber(2);
records.add(row);
Sheet sheet = Sheet.createSheet(records, 0); Sheet sheet = Sheet.createSheet(records, 0);
assertNotNull("Row [2] was skipped", sheet.getRow(2)); assertNotNull("Row [2] was skipped", sheet.getRow(2));
@ -197,9 +200,9 @@ public class TestSheet extends TestCase
Iterator iterator = sheet.getRowBreaks(); Iterator iterator = sheet.getRowBreaks();
while (iterator.hasNext()) { while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next(); PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
int main = (int)breakItem.main; int main = breakItem.main;
if (main != 0 && main != 10 && main != 11) fail("Invalid page break"); if (main != 0 && main != 10 && main != 11) fail("Invalid page break");
if (main == 0) is0 = true; if (main == 0) is0 = true;
if (main == 10) is10= true; if (main == 10) is10= true;
if (main == 11) is11 = true; if (main == 11) is11 = true;
} }
@ -216,8 +219,6 @@ public class TestSheet extends TestCase
assertFalse("row should be removed", sheet.isRowBroken(10)); assertFalse("row should be removed", sheet.isRowBroken(10));
assertEquals("no more breaks", 0, sheet.getNumRowBreaks()); assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
} }
/** /**
@ -256,10 +257,10 @@ public class TestSheet extends TestCase
Iterator iterator = sheet.getColumnBreaks(); Iterator iterator = sheet.getColumnBreaks();
while (iterator.hasNext()) { while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next(); PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
int main = (int)breakItem.main; int main = breakItem.main;
if (main != 0 && main != 1 && main != 10 && main != 15) fail("Invalid page break"); if (main != 0 && main != 1 && main != 10 && main != 15) fail("Invalid page break");
if (main == 0) is0 = true; if (main == 0) is0 = true;
if (main == 1) is1 = true; if (main == 1) is1 = true;
if (main == 10) is10= true; if (main == 10) is10= true;
if (main == 15) is15 = true; if (main == 15) is15 = true;
} }
@ -286,72 +287,69 @@ public class TestSheet extends TestCase
* works as designed. * works as designed.
*/ */
public void testXFIndexForColumn() { public void testXFIndexForColumn() {
try{ final short TEST_IDX = 10;
final short TEST_IDX = 10; final short DEFAULT_IDX = 0xF; // 15
final short DEFAULT_IDX = 0xF; // 15 short xfindex = Short.MIN_VALUE;
short xfindex = Short.MIN_VALUE; Sheet sheet = Sheet.createSheet();
Sheet sheet = Sheet.createSheet();
// without ColumnInfoRecord
// without ColumnInfoRecord xfindex = sheet.getXFIndexForColAt((short) 0);
xfindex = sheet.getXFIndexForColAt((short) 0); assertEquals(DEFAULT_IDX, xfindex);
assertEquals(DEFAULT_IDX, xfindex); xfindex = sheet.getXFIndexForColAt((short) 1);
xfindex = sheet.getXFIndexForColAt((short) 1); assertEquals(DEFAULT_IDX, xfindex);
assertEquals(DEFAULT_IDX, xfindex);
ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo();
ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo(); sheet.columns.insertColumn(nci);
sheet.columns.insertColumn(nci);
// single column ColumnInfoRecord
// single column ColumnInfoRecord nci.setFirstColumn((short) 2);
nci.setFirstColumn((short) 2); nci.setLastColumn((short) 2);
nci.setLastColumn((short) 2); nci.setXFIndex(TEST_IDX);
nci.setXFIndex(TEST_IDX); xfindex = sheet.getXFIndexForColAt((short) 0);
xfindex = sheet.getXFIndexForColAt((short) 0); assertEquals(DEFAULT_IDX, xfindex);
assertEquals(DEFAULT_IDX, xfindex); xfindex = sheet.getXFIndexForColAt((short) 1);
xfindex = sheet.getXFIndexForColAt((short) 1); assertEquals(DEFAULT_IDX, xfindex);
assertEquals(DEFAULT_IDX, xfindex); xfindex = sheet.getXFIndexForColAt((short) 2);
xfindex = sheet.getXFIndexForColAt((short) 2); assertEquals(TEST_IDX, xfindex);
assertEquals(TEST_IDX, xfindex); xfindex = sheet.getXFIndexForColAt((short) 3);
xfindex = sheet.getXFIndexForColAt((short) 3); assertEquals(DEFAULT_IDX, xfindex);
assertEquals(DEFAULT_IDX, xfindex);
// ten column ColumnInfoRecord // ten column ColumnInfoRecord
nci.setFirstColumn((short) 2); nci.setFirstColumn((short) 2);
nci.setLastColumn((short) 11); nci.setLastColumn((short) 11);
nci.setXFIndex(TEST_IDX); nci.setXFIndex(TEST_IDX);
xfindex = sheet.getXFIndexForColAt((short) 1); xfindex = sheet.getXFIndexForColAt((short) 1);
assertEquals(DEFAULT_IDX, xfindex); assertEquals(DEFAULT_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 2); xfindex = sheet.getXFIndexForColAt((short) 2);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 6); xfindex = sheet.getXFIndexForColAt((short) 6);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 11); xfindex = sheet.getXFIndexForColAt((short) 11);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 12); xfindex = sheet.getXFIndexForColAt((short) 12);
assertEquals(DEFAULT_IDX, xfindex); assertEquals(DEFAULT_IDX, xfindex);
// single column ColumnInfoRecord starting at index 0 // single column ColumnInfoRecord starting at index 0
nci.setFirstColumn((short) 0); nci.setFirstColumn((short) 0);
nci.setLastColumn((short) 0); nci.setLastColumn((short) 0);
nci.setXFIndex(TEST_IDX); nci.setXFIndex(TEST_IDX);
xfindex = sheet.getXFIndexForColAt((short) 0); xfindex = sheet.getXFIndexForColAt((short) 0);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 1); xfindex = sheet.getXFIndexForColAt((short) 1);
assertEquals(DEFAULT_IDX, xfindex); assertEquals(DEFAULT_IDX, xfindex);
// ten column ColumnInfoRecord starting at index 0 // ten column ColumnInfoRecord starting at index 0
nci.setFirstColumn((short) 0); nci.setFirstColumn((short) 0);
nci.setLastColumn((short) 9); nci.setLastColumn((short) 9);
nci.setXFIndex(TEST_IDX); nci.setXFIndex(TEST_IDX);
xfindex = sheet.getXFIndexForColAt((short) 0); xfindex = sheet.getXFIndexForColAt((short) 0);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 7); xfindex = sheet.getXFIndexForColAt((short) 7);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 9); xfindex = sheet.getXFIndexForColAt((short) 9);
assertEquals(TEST_IDX, xfindex); assertEquals(TEST_IDX, xfindex);
xfindex = sheet.getXFIndexForColAt((short) 10); xfindex = sheet.getXFIndexForColAt((short) 10);
assertEquals(DEFAULT_IDX, xfindex); assertEquals(DEFAULT_IDX, xfindex);
}
catch(Exception e){e.printStackTrace();fail(e.getMessage());}
} }
} }

View File

@ -19,125 +19,18 @@ package org.apache.poi.hssf.model;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.record.ColumnInfoRecord; import org.apache.poi.hssf.record.ColumnInfoRecord;
import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.PageBreakRecord;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.record.StringRecord;
/** /**
* @author Tony Poppleton * @author Tony Poppleton
*/ */
public class TestSheetAdditional extends TestCase public final class TestSheetAdditional extends TestCase {
{
/**
* Constructor for SheetTest.
* @param arg0
*/
public TestSheetAdditional(String arg0)
{
super(arg0);
}
public void testAddMergedRegion() public void testGetCellWidth() {
{
Sheet sheet = Sheet.createSheet();
int regionsToAdd = 4096;
int startRecords = sheet.getRecords().size();
//simple test that adds a load of regions
for (int n = 0; n < regionsToAdd; n++)
{
int index = sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
assertTrue("Merged region index expected to be " + n + " got " + index, index == n);
}
//test all the regions were indeed added
assertTrue(sheet.getNumMergedRegions() == regionsToAdd);
//test that the regions were spread out over the appropriate number of records
int recordsAdded = sheet.getRecords().size() - startRecords;
int recordsExpected = regionsToAdd/1027;
if ((regionsToAdd % 1027) != 0)
recordsExpected++;
assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected);
// Check we can't add one with invalud date
try {
sheet.addMergedRegion(10, (short)10, 9, (short)12);
fail();
} catch(IllegalArgumentException e) {}
try {
sheet.addMergedRegion(10, (short)10, 12, (short)9);
fail();
} catch(IllegalArgumentException e) {}
}
public void testRemoveMergedRegion()
{
Sheet sheet = Sheet.createSheet();
int regionsToAdd = 4096;
for (int n = 0; n < regionsToAdd; n++)
sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
int records = sheet.getRecords().size();
//remove a third from the beginning
for (int n = 0; n < regionsToAdd/3; n++)
{
sheet.removeMergedRegion(0);
//assert they have been deleted
assertTrue("Num of regions should be " + (regionsToAdd - n - 1) + " not " + sheet.getNumMergedRegions(), sheet.getNumMergedRegions() == regionsToAdd - n - 1);
}
//assert any record removing was done
int recordsRemoved = (regionsToAdd/3)/1027; //doesn't work for particular values of regionsToAdd
assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ". Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved);
}
/**
* Bug: 22922 (Reported by Xuemin Guan)
* <p>
* Remove mergedregion fails when a sheet loses records after an initial CreateSheet
* fills up the records.
*
*/
public void testMovingMergedRegion() {
List records = new ArrayList();
MergeCellsRecord merged = new MergeCellsRecord();
merged.addArea(0, (short)0, 1, (short)2);
records.add(new RowRecord());
records.add(new RowRecord());
records.add(new RowRecord());
records.add(merged);
Sheet sheet = Sheet.createSheet(records, 0);
sheet.records.remove(0);
//stub object to throw off list INDEX operations
sheet.removeMergedRegion(0);
assertEquals("Should be no more merged regions", 0, sheet.getNumMergedRegions());
}
public void testGetMergedRegionAt()
{
//TODO
}
public void testGetNumMergedRegions()
{
//TODO
}
public void DISBALEDtestGetCellWidth() throws Exception
{
Sheet sheet = Sheet.createSheet(); Sheet sheet = Sheet.createSheet();
ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo(); ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo();
@ -146,14 +39,8 @@ public class TestSheetAdditional extends TestCase
nci.setLastColumn((short)10); nci.setLastColumn((short)10);
nci.setColumnWidth((short)100); nci.setColumnWidth((short)100);
Field f = null;
f = Sheet.class.getDeclaredField("columnSizes"); sheet.columns.insertColumn(nci);
f.setAccessible(true);
List columnSizes = new ArrayList();
f.set(sheet,columnSizes);
columnSizes.add(nci);
sheet.records.add(1 + sheet.dimsloc, nci);
sheet.dimsloc++;
assertEquals((short)100,sheet.getColumnWidth((short)5)); assertEquals((short)100,sheet.getColumnWidth((short)5));
assertEquals((short)100,sheet.getColumnWidth((short)6)); assertEquals((short)100,sheet.getColumnWidth((short)6));
@ -172,151 +59,6 @@ public class TestSheetAdditional extends TestCase
assertEquals((short)100,sheet.getColumnWidth((short)10)); assertEquals((short)100,sheet.getColumnWidth((short)10));
} }
/**
* Makes sure all rows registered for this sheet are aggregated, they were being skipped
*
*/
public void testRowAggregation() {
List records = new ArrayList();
RowRecord row = new RowRecord();
row.setRowNumber(0);
records.add(row);
row = new RowRecord();
row.setRowNumber(1);
records.add(row);
records.add(new StringRecord());
row = new RowRecord();
row.setRowNumber(2);
records.add(row);
Sheet sheet = Sheet.createSheet(records, 0);
assertNotNull("Row [2] was skipped", sheet.getRow(2));
}
/**
* Make sure page break functionality works (in memory)
*
*/
public void testRowPageBreaks(){
short colFrom = 0;
short colTo = 255;
Sheet sheet = Sheet.createSheet();
sheet.setRowBreak(0, colFrom, colTo);
assertTrue("no row break at 0", sheet.isRowBroken(0));
assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
sheet.setRowBreak(0, colFrom, colTo);
sheet.setRowBreak(0, colFrom, colTo);
assertTrue("no row break at 0", sheet.isRowBroken(0));
assertEquals("1 row break available", 1, sheet.getNumRowBreaks());
sheet.setRowBreak(10, colFrom, colTo);
sheet.setRowBreak(11, colFrom, colTo);
assertTrue("no row break at 10", sheet.isRowBroken(10));
assertTrue("no row break at 11", sheet.isRowBroken(11));
assertEquals("3 row break available", 3, sheet.getNumRowBreaks());
boolean is10 = false;
boolean is0 = false;
boolean is11 = false;
Iterator iterator = sheet.getRowBreaks();
while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
int main = (int)breakItem.main;
if (main != 0 && main != 10 && main != 11) fail("Invalid page break");
if (main == 0) is0 = true;
if (main == 10) is10= true;
if (main == 11) is11 = true;
}
assertTrue("one of the breaks didnt make it", is0 && is10 && is11);
sheet.removeRowBreak(11);
assertFalse("row should be removed", sheet.isRowBroken(11));
sheet.removeRowBreak(0);
assertFalse("row should be removed", sheet.isRowBroken(0));
sheet.removeRowBreak(10);
assertFalse("row should be removed", sheet.isRowBroken(10));
assertEquals("no more breaks", 0, sheet.getNumRowBreaks());
}
/**
* Make sure column pag breaks works properly (in-memory)
*
*/
public void testColPageBreaks(){
short rowFrom = 0;
short rowTo = (short)65535;
Sheet sheet = Sheet.createSheet();
sheet.setColumnBreak((short)0, rowFrom, rowTo);
assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());
sheet.setColumnBreak((short)0, rowFrom, rowTo);
assertTrue("no col break at 0", sheet.isColumnBroken((short)0));
assertEquals("1 col break available", 1, sheet.getNumColumnBreaks());
sheet.setColumnBreak((short)1, rowFrom, rowTo);
sheet.setColumnBreak((short)10, rowFrom, rowTo);
sheet.setColumnBreak((short)15, rowFrom, rowTo);
assertTrue("no col break at 1", sheet.isColumnBroken((short)1));
assertTrue("no col break at 10", sheet.isColumnBroken((short)10));
assertTrue("no col break at 15", sheet.isColumnBroken((short)15));
assertEquals("4 col break available", 4, sheet.getNumColumnBreaks());
boolean is10 = false;
boolean is0 = false;
boolean is1 = false;
boolean is15 = false;
Iterator iterator = sheet.getColumnBreaks();
while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
int main = (int)breakItem.main;
if (main != 0 && main != 1 && main != 10 && main != 15) fail("Invalid page break");
if (main == 0) is0 = true;
if (main == 1) is1 = true;
if (main == 10) is10= true;
if (main == 15) is15 = true;
}
assertTrue("one of the breaks didnt make it", is0 && is1 && is10 && is15);
sheet.removeColumnBreak((short)15);
assertFalse("column break should not be there", sheet.isColumnBroken((short)15));
sheet.removeColumnBreak((short)0);
assertFalse("column break should not be there", sheet.isColumnBroken((short)0));
sheet.removeColumnBreak((short)1);
assertFalse("column break should not be there", sheet.isColumnBroken((short)1));
sheet.removeColumnBreak((short)10);
assertFalse("column break should not be there", sheet.isColumnBroken((short)10));
assertEquals("no more breaks", 0, sheet.getNumColumnBreaks());
}
} }

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -15,34 +14,28 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.record.aggregates; package org.apache.poi.hssf.record.aggregates;
import org.apache.poi.hssf.record.*; import junit.framework.TestCase;
public class TestRowRecordsAggregate extends junit.framework.TestCase { import org.apache.poi.hssf.record.RowRecord;
public TestRowRecordsAggregate(String name) {
super (name); /**
} *
*/
public final class TestRowRecordsAggregate extends TestCase {
public void testRowGet() { public void testRowGet() {
RowRecordsAggregate rra = new RowRecordsAggregate(); RowRecordsAggregate rra = new RowRecordsAggregate();
RowRecord rr = new RowRecord(); RowRecord rr = new RowRecord(4);
rr.setRowNumber(( short ) 4);
rra.insertRow(rr); rra.insertRow(rr);
RowRecord rr2 = new RowRecord(); rr2.setRowNumber((short) 1); rra.insertRow(new RowRecord(1));
rra.insertRow(rr2);
RowRecord rr1 = rra.getRow(4); RowRecord rr1 = rra.getRow(4);
assertTrue("Row Record should not be null", rr1!=null); assertNotNull(rr1);
assertTrue("Row number is 1",rr1.getRowNumber() == 4); assertEquals("Row number is 1", 4, rr1.getRowNumber());
assertTrue("Row record retrieved is identical ", rr1 == rr); assertTrue("Row record retrieved is identical ", rr1 == rr);
} }
public static void main(String [] args) {
System.out
.println("Testing org.apache.poi.hssf.record.aggregates.RowRecordAggregate");
junit.textui.TestRunner.run(TestRowRecordsAggregate.class);
}
} }

View File

@ -143,8 +143,9 @@ public final class TestHSSFRow extends TestCase {
try { try {
sheet.createRow(-1); sheet.createRow(-1);
fail("IndexOutOfBoundsException should have been thrown"); fail("IndexOutOfBoundsException should have been thrown");
} catch (IndexOutOfBoundsException ex) { } catch (IllegalArgumentException e) {
// expected during successful test // expected during successful test
assertEquals("Invalid row number (-1) outside allowable range (0..65535)", e.getMessage());
} }
//Test high row bound //Test high row bound
@ -153,8 +154,9 @@ public final class TestHSSFRow extends TestCase {
try { try {
sheet.createRow(65536); sheet.createRow(65536);
fail("IndexOutOfBoundsException should have been thrown"); fail("IndexOutOfBoundsException should have been thrown");
} catch (IndexOutOfBoundsException ex) { } catch (IllegalArgumentException e) {
// expected during successful test // expected during successful test
assertEquals("Invalid row number (65536) outside allowable range (0..65535)", e.getMessage());
} }
} }

View File

@ -23,6 +23,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
@ -193,17 +194,29 @@ public final class TestHSSFSheet extends TestCase {
public void testCloneSheet() { public void testCloneSheet() {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Test Clone"); HSSFSheet sheet = workbook.createSheet("Test Clone");
HSSFRow row = sheet.createRow((short) 0); HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0); HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("clone_test"); HSSFCell cell2 = row.createCell((short) 1);
HSSFSheet cloned = workbook.cloneSheet(0); cell.setCellValue(new HSSFRichTextString("clone_test"));
cell2.setCellFormula("sin(1)");
HSSFSheet clonedSheet = workbook.cloneSheet(0);
HSSFRow clonedRow = clonedSheet.getRow(0);
//Check for a good clone //Check for a good clone
assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test");
//Check that the cells are not somehow linked //Check that the cells are not somehow linked
cell.setCellValue("Difference Check"); cell.setCellValue(new HSSFRichTextString("Difference Check"));
assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); cell2.setCellFormula("cos(2)");
if ("Difference Check".equals(clonedRow.getCell(0).getRichStringCellValue().getString())) {
fail("string cell not properly cloned");
}
if ("COS(2)".equals(clonedRow.getCell(1).getCellFormula())) {
fail("formula cell not properly cloned");
}
assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test");
assertEquals(clonedRow.getCell(1).getCellFormula(), "SIN(1)");
} }
/** tests that the sheet name for multiple clones of the same sheet is unique /** tests that the sheet name for multiple clones of the same sheet is unique
@ -214,7 +227,7 @@ public final class TestHSSFSheet extends TestCase {
HSSFSheet sheet = workbook.createSheet("Test Clone"); HSSFSheet sheet = workbook.createSheet("Test Clone");
HSSFRow row = sheet.createRow((short) 0); HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0); HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("clone_test"); cell.setCellValue(new HSSFRichTextString("clone_test"));
//Clone the sheet multiple times //Clone the sheet multiple times
workbook.cloneSheet(0); workbook.cloneSheet(0);
workbook.cloneSheet(0); workbook.cloneSheet(0);
@ -517,11 +530,11 @@ public final class TestHSSFSheet extends TestCase {
HSSFSheet sheet = wb.createSheet(); HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0); HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)0); HSSFCell cell = row.createCell((short)0);
cell.setCellValue("first row, first cell"); cell.setCellValue(new HSSFRichTextString("first row, first cell"));
row = sheet.createRow(1); row = sheet.createRow(1);
cell = row.createCell((short)1); cell = row.createCell((short)1);
cell.setCellValue("second row, second cell"); cell.setCellValue(new HSSFRichTextString("second row, second cell"));
Region region = new Region(1, (short)0, 1, (short)1); Region region = new Region(1, (short)0, 1, (short)1);
sheet.addMergedRegion(region); sheet.addMergedRegion(region);
@ -643,28 +656,28 @@ public final class TestHSSFSheet extends TestCase {
/** cell with formula becomes null on cloning a sheet*/ /** cell with formula becomes null on cloning a sheet*/
public void test35084() { public void test35084() {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s =wb.createSheet("Sheet1"); HSSFSheet s = wb.createSheet("Sheet1");
HSSFRow r = s.createRow(0); HSSFRow r = s.createRow(0);
r.createCell((short)0).setCellValue(1); r.createCell((short) 0).setCellValue(1);
r.createCell((short)1).setCellFormula("A1*2"); r.createCell((short) 1).setCellFormula("A1*2");
HSSFSheet s1 = wb.cloneSheet(0); HSSFSheet s1 = wb.cloneSheet(0);
r=s1.getRow(0); r = s1.getRow(0);
assertEquals("double" ,r.getCell((short)0).getNumericCellValue(),(double)1,0); //sanity check assertEquals("double", r.getCell((short) 0).getNumericCellValue(), 1, 0); // sanity check
assertNotNull(r.getCell((short)1)); assertNotNull(r.getCell((short) 1));
assertEquals("formula", r.getCell((short)1).getCellFormula(), "A1*2"); assertEquals("formula", r.getCell((short) 1).getCellFormula(), "A1*2");
} }
/** test that new default column styles get applied */ /** test that new default column styles get applied */
public void testDefaultColumnStyle() { public void testDefaultColumnStyle() {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle(); HSSFCellStyle style = wb.createCellStyle();
HSSFSheet s = wb.createSheet(); HSSFSheet s = wb.createSheet();
s.setDefaultColumnStyle((short)0, style); s.setDefaultColumnStyle((short) 0, style);
HSSFRow r = s.createRow(0); HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell((short)0); HSSFCell c = r.createCell((short) 0);
assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex()); assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex());
} }
@ -814,11 +827,6 @@ public final class TestHSSFSheet extends TestCase {
assertTrue(wb3.getSheetAt(3).getForceFormulaRecalculation()); assertTrue(wb3.getSheetAt(3).getForceFormulaRecalculation());
} }
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(TestHSSFSheet.class);
}
public void testColumnWidth() throws Exception { public void testColumnWidth() throws Exception {
//check we can correctly read column widths from a reference workbook //check we can correctly read column widths from a reference workbook
HSSFWorkbook wb = openSample("colwidth.xls"); HSSFWorkbook wb = openSample("colwidth.xls");
@ -870,11 +878,33 @@ public final class TestHSSFSheet extends TestCase {
assertEquals(256*10, sh.getColumnWidth((short)0)); assertEquals(256*10, sh.getColumnWidth((short)0));
assertEquals(256*10, sh.getColumnWidth((short)1)); assertEquals(256*10, sh.getColumnWidth((short)1));
assertEquals(256*10, sh.getColumnWidth((short)2)); assertEquals(256*10, sh.getColumnWidth((short)2));
//columns D-F have custom wodth //columns D-F have custom width
for (char i = 'D'; i <= 'F'; i++) { for (char i = 'D'; i <= 'F'; i++) {
short w = (short)(256*12); short w = (short)(256*12);
assertEquals(w, sh.getColumnWidth((short)i)); assertEquals(w, sh.getColumnWidth((short)i));
} }
}
/**
* Some utilities write Excel files without the ROW records.
* Excel, ooo, and google docs are OK with this.
* Now POI is too.
*/
public void testMissingRowRecords_bug41187() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex41187-19267.xls");
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
if(row == null) {
throw new AssertionFailedError("Identified bug 41187 a");
}
if (row.getHeight() == 0) {
throw new AssertionFailedError("Identified bug 41187 b");
}
assertEquals("Hi Excel!", row.getCell(0).getRichStringCellValue().getString());
// check row height for 'default' flag
assertEquals((short)0x8000, row.getHeight());
HSSFTestDataSamples.writeOutAndReadBack(wb);
} }
} }

View File

@ -147,9 +147,10 @@ public final class TestHSSFWorkbook extends TestCase {
// Single chart, two sheets // Single chart, two sheets
b = openSample("44010-SingleChart.xls"); b = openSample("44010-SingleChart.xls");
assertEquals(2, b.getNumberOfSheets()); assertEquals(2, b.getNumberOfSheets());
assertEquals("Graph2", b.getSheetName(1));
s = b.getSheetAt(1); s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
// Has chart on 1st sheet?? // Has chart on 1st sheet??
// FIXME // FIXME
@ -166,7 +167,7 @@ public final class TestHSSFWorkbook extends TestCase {
assertEquals(2, b.getNumberOfSheets()); assertEquals(2, b.getNumberOfSheets());
s = b.getSheetAt(1); s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
// Two charts, three sheets // Two charts, three sheets
@ -175,10 +176,10 @@ public final class TestHSSFWorkbook extends TestCase {
s = b.getSheetAt(1); s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
s = b.getSheetAt(2); s = b.getSheetAt(2);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
// Has chart on 1st sheet?? // Has chart on 1st sheet??
// FIXME // FIXME
@ -197,13 +198,13 @@ public final class TestHSSFWorkbook extends TestCase {
s = b.getSheetAt(1); s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
s = b.getSheetAt(2); s = b.getSheetAt(2);
assertEquals(0, s.getFirstRowNum()); assertEquals(0, s.getFirstRowNum());
assertEquals(0, s.getLastRowNum()); assertEquals(8, s.getLastRowNum());
} }
private static HSSFWorkbook writeRead(HSSFWorkbook b) { private static HSSFWorkbook writeRead(HSSFWorkbook b) {
return HSSFTestDataSamples.writeOutAndReadBack(b); return HSSFTestDataSamples.writeOutAndReadBack(b);
} }
} }