diff options
author | Josh Micich <josh@apache.org> | 2008-05-11 08:15:39 +0000 |
---|---|---|
committer | Josh Micich <josh@apache.org> | 2008-05-11 08:15:39 +0000 |
commit | 96c489deebd92d38a4780ec3ac49d9a0d19e2dd4 (patch) | |
tree | 5583ac9bb77236cacc32373f71345c37d8a7a10c /src/testcases | |
parent | e2cec361f37af84cae51d8987a3a5590adf88b6b (diff) | |
download | poi-96c489deebd92d38a4780ec3ac49d9a0d19e2dd4.tar.gz poi-96c489deebd92d38a4780ec3ac49d9a0d19e2dd4.zip |
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
Diffstat (limited to 'src/testcases')
9 files changed, 226 insertions, 424 deletions
diff --git a/src/testcases/org/apache/poi/hssf/HSSFTests.java b/src/testcases/org/apache/poi/hssf/HSSFTests.java index 5b597a67c8..0a4fa66af7 100644 --- a/src/testcases/org/apache/poi/hssf/HSSFTests.java +++ b/src/testcases/org/apache/poi/hssf/HSSFTests.java @@ -22,9 +22,7 @@ import junit.framework.TestSuite; import org.apache.poi.hssf.eventmodel.TestEventRecordFactory; import org.apache.poi.hssf.eventmodel.TestModelFactory; -import org.apache.poi.hssf.model.TestDrawingManager; -import org.apache.poi.hssf.model.TestFormulaParser; -import org.apache.poi.hssf.model.TestSheet; +import org.apache.poi.hssf.model.AllModelTests; import org.apache.poi.hssf.record.AllRecordTests; import org.apache.poi.hssf.usermodel.AllUserModelTests; 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"); // $JUnit-BEGIN$ + suite.addTest(AllModelTests.suite()); suite.addTest(AllUserModelTests.suite()); suite.addTest(AllRecordTests.suite()); - suite.addTest(new TestSuite(TestFormulaParser.class)); suite.addTest(new TestSuite(TestAreaReference.class)); suite.addTest(new TestSuite(TestCellReference.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(TestEventRecordFactory.class)); suite.addTest(new TestSuite(TestModelFactory.class)); - suite.addTest(new TestSuite(TestDrawingManager.class)); - suite.addTest(new TestSuite(TestSheet.class)); // $JUnit-END$ return suite; } diff --git a/src/testcases/org/apache/poi/hssf/data/ex41187-19267.xls b/src/testcases/org/apache/poi/hssf/data/ex41187-19267.xls Binary files differnew file mode 100644 index 0000000000..c07bfca594 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/data/ex41187-19267.xls diff --git a/src/testcases/org/apache/poi/hssf/model/AllModelTests.java b/src/testcases/org/apache/poi/hssf/model/AllModelTests.java new file mode 100755 index 0000000000..19ef437063 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/model/AllModelTests.java @@ -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; + } +} diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheet.java b/src/testcases/org/apache/poi/hssf/model/TestSheet.java index 964caedc2f..9281eb80d7 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestSheet.java +++ b/src/testcases/org/apache/poi/hssf/model/TestSheet.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.model; @@ -34,8 +32,7 @@ import java.util.List; * * @author Glen Stampoultzis (glens at apache.org) */ -public class TestSheet extends TestCase -{ +public final class TestSheet extends TestCase { public void testCreateSheet() throws Exception { // Check we're adding row and cell aggregates @@ -76,6 +73,21 @@ public class TestSheet extends TestCase 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 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() @@ -113,9 +125,9 @@ public class TestSheet extends TestCase 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(new RowRecord(0)); + records.add(new RowRecord(1)); + records.add(new RowRecord(2)); records.add(merged); Sheet sheet = Sheet.createSheet(records, 0); @@ -142,20 +154,11 @@ public class TestSheet extends TestCase */ 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 RowRecord(0)); + records.add(new RowRecord(1)); records.add(new StringRecord()); - - row = new RowRecord(); - row.setRowNumber(2); - records.add(row); - + records.add(new RowRecord(2)); Sheet sheet = Sheet.createSheet(records, 0); assertNotNull("Row [2] was skipped", sheet.getRow(2)); @@ -197,9 +200,9 @@ public class TestSheet extends TestCase Iterator iterator = sheet.getRowBreaks(); while (iterator.hasNext()) { 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) is0 = true; + if (main == 0) is0 = true; if (main == 10) is10= true; if (main == 11) is11 = true; } @@ -216,8 +219,6 @@ public class TestSheet extends TestCase assertFalse("row should be removed", sheet.isRowBroken(10)); assertEquals("no more breaks", 0, sheet.getNumRowBreaks()); - - } /** @@ -256,10 +257,10 @@ public class TestSheet extends TestCase Iterator iterator = sheet.getColumnBreaks(); while (iterator.hasNext()) { 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) is0 = true; - if (main == 1) is1 = true; + if (main == 0) is0 = true; + if (main == 1) is1 = true; if (main == 10) is10= true; if (main == 15) is15 = true; } @@ -286,72 +287,69 @@ public class TestSheet extends TestCase * works as designed. */ public void testXFIndexForColumn() { - try{ - final short TEST_IDX = 10; - final short DEFAULT_IDX = 0xF; // 15 - short xfindex = Short.MIN_VALUE; - Sheet sheet = Sheet.createSheet(); - - // without ColumnInfoRecord - xfindex = sheet.getXFIndexForColAt((short) 0); - assertEquals(DEFAULT_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 1); - assertEquals(DEFAULT_IDX, xfindex); - - ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo(); - sheet.columns.insertColumn(nci); - - // single column ColumnInfoRecord - nci.setFirstColumn((short) 2); - nci.setLastColumn((short) 2); - nci.setXFIndex(TEST_IDX); - xfindex = sheet.getXFIndexForColAt((short) 0); - assertEquals(DEFAULT_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 1); - assertEquals(DEFAULT_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 2); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 3); - assertEquals(DEFAULT_IDX, xfindex); - - // ten column ColumnInfoRecord - nci.setFirstColumn((short) 2); - nci.setLastColumn((short) 11); - nci.setXFIndex(TEST_IDX); - xfindex = sheet.getXFIndexForColAt((short) 1); - assertEquals(DEFAULT_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 2); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 6); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 11); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 12); - assertEquals(DEFAULT_IDX, xfindex); - - // single column ColumnInfoRecord starting at index 0 - nci.setFirstColumn((short) 0); - nci.setLastColumn((short) 0); - nci.setXFIndex(TEST_IDX); - xfindex = sheet.getXFIndexForColAt((short) 0); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 1); - assertEquals(DEFAULT_IDX, xfindex); - - // ten column ColumnInfoRecord starting at index 0 - nci.setFirstColumn((short) 0); - nci.setLastColumn((short) 9); - nci.setXFIndex(TEST_IDX); - xfindex = sheet.getXFIndexForColAt((short) 0); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 7); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 9); - assertEquals(TEST_IDX, xfindex); - xfindex = sheet.getXFIndexForColAt((short) 10); - assertEquals(DEFAULT_IDX, xfindex); - } - catch(Exception e){e.printStackTrace();fail(e.getMessage());} + final short TEST_IDX = 10; + final short DEFAULT_IDX = 0xF; // 15 + short xfindex = Short.MIN_VALUE; + Sheet sheet = Sheet.createSheet(); + + // without ColumnInfoRecord + xfindex = sheet.getXFIndexForColAt((short) 0); + assertEquals(DEFAULT_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 1); + assertEquals(DEFAULT_IDX, xfindex); + + ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo(); + sheet.columns.insertColumn(nci); + + // single column ColumnInfoRecord + nci.setFirstColumn((short) 2); + nci.setLastColumn((short) 2); + nci.setXFIndex(TEST_IDX); + xfindex = sheet.getXFIndexForColAt((short) 0); + assertEquals(DEFAULT_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 1); + assertEquals(DEFAULT_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 2); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 3); + assertEquals(DEFAULT_IDX, xfindex); + + // ten column ColumnInfoRecord + nci.setFirstColumn((short) 2); + nci.setLastColumn((short) 11); + nci.setXFIndex(TEST_IDX); + xfindex = sheet.getXFIndexForColAt((short) 1); + assertEquals(DEFAULT_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 2); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 6); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 11); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 12); + assertEquals(DEFAULT_IDX, xfindex); + + // single column ColumnInfoRecord starting at index 0 + nci.setFirstColumn((short) 0); + nci.setLastColumn((short) 0); + nci.setXFIndex(TEST_IDX); + xfindex = sheet.getXFIndexForColAt((short) 0); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 1); + assertEquals(DEFAULT_IDX, xfindex); + + // ten column ColumnInfoRecord starting at index 0 + nci.setFirstColumn((short) 0); + nci.setLastColumn((short) 9); + nci.setXFIndex(TEST_IDX); + xfindex = sheet.getXFIndexForColAt((short) 0); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 7); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 9); + assertEquals(TEST_IDX, xfindex); + xfindex = sheet.getXFIndexForColAt((short) 10); + assertEquals(DEFAULT_IDX, xfindex); } - } + diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java index 69ab3f2986..f1c3b7c9ca 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java +++ b/src/testcases/org/apache/poi/hssf/model/TestSheetAdditional.java @@ -19,125 +19,18 @@ package org.apache.poi.hssf.model; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import junit.framework.TestCase; 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 */ -public class TestSheetAdditional extends TestCase -{ - /** - * Constructor for SheetTest. - * @param arg0 - */ - public TestSheetAdditional(String arg0) - { - super(arg0); - } +public final class TestSheetAdditional extends TestCase { - public void testAddMergedRegion() - { - 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 - { + public void testGetCellWidth() { Sheet sheet = Sheet.createSheet(); ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo(); @@ -146,14 +39,8 @@ public class TestSheetAdditional extends TestCase nci.setLastColumn((short)10); nci.setColumnWidth((short)100); - Field f = null; - f = Sheet.class.getDeclaredField("columnSizes"); - f.setAccessible(true); - List columnSizes = new ArrayList(); - f.set(sheet,columnSizes); - columnSizes.add(nci); - sheet.records.add(1 + sheet.dimsloc, nci); - sheet.dimsloc++; + + sheet.columns.insertColumn(nci); assertEquals((short)100,sheet.getColumnWidth((short)5)); assertEquals((short)100,sheet.getColumnWidth((short)6)); @@ -172,151 +59,6 @@ public class TestSheetAdditional extends TestCase 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()); - } - } diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java index 50f67129ce..239fc2b887 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,34 +14,28 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.hssf.record.aggregates; -import org.apache.poi.hssf.record.*; +import junit.framework.TestCase; -public class TestRowRecordsAggregate extends junit.framework.TestCase { - public TestRowRecordsAggregate(String name) { - super (name); - } +import org.apache.poi.hssf.record.RowRecord; + +/** + * + */ +public final class TestRowRecordsAggregate extends TestCase { public void testRowGet() { RowRecordsAggregate rra = new RowRecordsAggregate(); - RowRecord rr = new RowRecord(); - rr.setRowNumber(( short ) 4); + RowRecord rr = new RowRecord(4); rra.insertRow(rr); - RowRecord rr2 = new RowRecord(); rr2.setRowNumber((short) 1); - rra.insertRow(rr2); + rra.insertRow(new RowRecord(1)); RowRecord rr1 = rra.getRow(4); - assertTrue("Row Record should not be null", rr1!=null); - assertTrue("Row number is 1",rr1.getRowNumber() == 4); + assertNotNull(rr1); + assertEquals("Row number is 1", 4, rr1.getRowNumber()); 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); - } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java index b6f22022c6..f0c4139d2e 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java @@ -143,8 +143,9 @@ public final class TestHSSFRow extends TestCase { try { sheet.createRow(-1); fail("IndexOutOfBoundsException should have been thrown"); - } catch (IndexOutOfBoundsException ex) { + } catch (IllegalArgumentException e) { // expected during successful test + assertEquals("Invalid row number (-1) outside allowable range (0..65535)", e.getMessage()); } //Test high row bound @@ -153,8 +154,9 @@ public final class TestHSSFRow extends TestCase { try { sheet.createRow(65536); fail("IndexOutOfBoundsException should have been thrown"); - } catch (IndexOutOfBoundsException ex) { + } catch (IllegalArgumentException e) { // expected during successful test + assertEquals("Invalid row number (65536) outside allowable range (0..65535)", e.getMessage()); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 246d6b5175..71c8a3a4cc 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -193,17 +194,29 @@ public final class TestHSSFSheet extends TestCase { public void testCloneSheet() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Test Clone"); - HSSFRow row = sheet.createRow((short) 0); + HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell((short) 0); - cell.setCellValue("clone_test"); - HSSFSheet cloned = workbook.cloneSheet(0); - + HSSFCell cell2 = row.createCell((short) 1); + 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 - 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 - cell.setCellValue("Difference Check"); - assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); + cell.setCellValue(new HSSFRichTextString("Difference Check")); + 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 @@ -214,7 +227,7 @@ public final class TestHSSFSheet extends TestCase { HSSFSheet sheet = workbook.createSheet("Test Clone"); HSSFRow row = sheet.createRow((short) 0); HSSFCell cell = row.createCell((short) 0); - cell.setCellValue("clone_test"); + cell.setCellValue(new HSSFRichTextString("clone_test")); //Clone the sheet multiple times workbook.cloneSheet(0); workbook.cloneSheet(0); @@ -517,11 +530,11 @@ public final class TestHSSFSheet extends TestCase { HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow(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); 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); sheet.addMergedRegion(region); @@ -643,28 +656,28 @@ public final class TestHSSFSheet extends TestCase { /** cell with formula becomes null on cloning a sheet*/ public void test35084() { - - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s =wb.createSheet("Sheet1"); - HSSFRow r = s.createRow(0); - r.createCell((short)0).setCellValue(1); - r.createCell((short)1).setCellFormula("A1*2"); - HSSFSheet s1 = wb.cloneSheet(0); - r=s1.getRow(0); - assertEquals("double" ,r.getCell((short)0).getNumericCellValue(),(double)1,0); //sanity check - assertNotNull(r.getCell((short)1)); - assertEquals("formula", r.getCell((short)1).getCellFormula(), "A1*2"); - } + + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet("Sheet1"); + HSSFRow r = s.createRow(0); + r.createCell((short) 0).setCellValue(1); + r.createCell((short) 1).setCellFormula("A1*2"); + HSSFSheet s1 = wb.cloneSheet(0); + r = s1.getRow(0); + assertEquals("double", r.getCell((short) 0).getNumericCellValue(), 1, 0); // sanity check + assertNotNull(r.getCell((short) 1)); + assertEquals("formula", r.getCell((short) 1).getCellFormula(), "A1*2"); + } /** test that new default column styles get applied */ public void testDefaultColumnStyle() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFCellStyle style = wb.createCellStyle(); - HSSFSheet s = wb.createSheet(); - s.setDefaultColumnStyle((short)0, style); - HSSFRow r = s.createRow(0); - HSSFCell c = r.createCell((short)0); - assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex()); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFCellStyle style = wb.createCellStyle(); + HSSFSheet s = wb.createSheet(); + s.setDefaultColumnStyle((short) 0, style); + HSSFRow r = s.createRow(0); + HSSFCell c = r.createCell((short) 0); + assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex()); } @@ -814,11 +827,6 @@ public final class TestHSSFSheet extends TestCase { assertTrue(wb3.getSheetAt(3).getForceFormulaRecalculation()); } - - public static void main(java.lang.String[] args) { - junit.textui.TestRunner.run(TestHSSFSheet.class); - } - public void testColumnWidth() throws Exception { //check we can correctly read column widths from a reference workbook 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)1)); 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++) { short w = (short)(256*12); 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); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 4c156e7b6f..75ac6923a9 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -147,9 +147,10 @@ public final class TestHSSFWorkbook extends TestCase { // Single chart, two sheets b = openSample("44010-SingleChart.xls"); assertEquals(2, b.getNumberOfSheets()); + assertEquals("Graph2", b.getSheetName(1)); s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); // Has chart on 1st sheet?? // FIXME @@ -166,7 +167,7 @@ public final class TestHSSFWorkbook extends TestCase { assertEquals(2, b.getNumberOfSheets()); s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); // Two charts, three sheets @@ -175,10 +176,10 @@ public final class TestHSSFWorkbook extends TestCase { s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); s = b.getSheetAt(2); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); // Has chart on 1st sheet?? // FIXME @@ -197,13 +198,13 @@ public final class TestHSSFWorkbook extends TestCase { s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); s = b.getSheetAt(2); assertEquals(0, s.getFirstRowNum()); - assertEquals(0, s.getLastRowNum()); + assertEquals(8, s.getLastRowNum()); } private static HSSFWorkbook writeRead(HSSFWorkbook b) { - return HSSFTestDataSamples.writeOutAndReadBack(b); + return HSSFTestDataSamples.writeOutAndReadBack(b); } }
\ No newline at end of file |