diff options
author | Dominik Stadler <centic@apache.org> | 2020-04-26 08:23:04 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2020-04-26 08:23:04 +0000 |
commit | 979d1b48e7a057dbfd29af6bbe0dabad0adec65a (patch) | |
tree | 555d42111a61c5013d99ac3a6ee8f353b3eef272 /src/testcases/org/apache/poi | |
parent | 6ae31c8a67aeca159f1f0f0eeb5a62a690b1ebba (diff) | |
download | poi-979d1b48e7a057dbfd29af6bbe0dabad0adec65a.tar.gz poi-979d1b48e7a057dbfd29af6bbe0dabad0adec65a.zip |
Github-177: Avoid NullPointerException if RangeCopier encounters empty/missing rows
Also expose one-parameter constructor and verify it in tests.
Closes #177
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1877010 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi')
-rw-r--r-- | src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRangeCopier.java | 4 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java | 55 |
2 files changed, 44 insertions, 15 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRangeCopier.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRangeCopier.java index 8f6b678513..8d685fc68d 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRangeCopier.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRangeCopier.java @@ -27,9 +27,9 @@ import org.junit.Before; public class TestHSSFRangeCopier extends TestRangeCopier { public TestHSSFRangeCopier() { - super(); + super(); workbook = new HSSFWorkbook(); - testDataProvider = HSSFITestDataProvider.instance; + testDataProvider = HSSFITestDataProvider.instance; } @Before diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java b/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java index def64a32c9..d00a820e3c 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java @@ -32,15 +32,15 @@ public abstract class TestRangeCopier { protected Sheet sheet1; protected Sheet sheet2; protected Workbook workbook; - protected RangeCopier rangeCopier; - protected RangeCopier transSheetRangeCopier; + protected RangeCopier rangeCopier; + protected RangeCopier transSheetRangeCopier; protected ITestDataProvider testDataProvider; protected void initSheets() { sheet1 = workbook.getSheet("sheet1"); sheet2 = workbook.getSheet("sheet2"); } - + @Test public void copySheetRangeWithoutFormulas() { CellRangeAddress rangeToCopy = CellRangeAddress.valueOf("B1:C2"); //2x2 @@ -53,21 +53,21 @@ public abstract class TestRangeCopier { @Test public void tileTheRangeAway() { CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5"); - CellRangeAddress destRange = CellRangeAddress.valueOf("F4:K5"); + CellRangeAddress destRange = CellRangeAddress.valueOf("F4:K5"); rangeCopier.copyRange(tileRange, destRange); - assertEquals("1.3", getCellContent(sheet1, "H4")); - assertEquals("1.3", getCellContent(sheet1, "J4")); - assertEquals("$C1+G$2", getCellContent(sheet1, "G5")); - assertEquals("SUM(G3:I3)", getCellContent(sheet1, "H5")); - assertEquals("$C1+I$2", getCellContent(sheet1, "I5")); + assertEquals("1.3", getCellContent(sheet1, "H4")); + assertEquals("1.3", getCellContent(sheet1, "J4")); + assertEquals("$C1+G$2", getCellContent(sheet1, "G5")); + assertEquals("SUM(G3:I3)", getCellContent(sheet1, "H5")); + assertEquals("$C1+I$2", getCellContent(sheet1, "I5")); assertEquals("", getCellContent(sheet1, "L5")); //out of borders assertEquals("", getCellContent(sheet1, "G7")); //out of borders } - + @Test public void tileTheRangeOver() { CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5"); - CellRangeAddress destRange = CellRangeAddress.valueOf("A4:C5"); + CellRangeAddress destRange = CellRangeAddress.valueOf("A4:C5"); rangeCopier.copyRange(tileRange, destRange); assertEquals("1.3", getCellContent(sheet1, "A4")); assertEquals("$C1+B$2", getCellContent(sheet1, "B5")); @@ -78,7 +78,7 @@ public abstract class TestRangeCopier { public void copyRangeToOtherSheet() { Sheet destSheet = sheet2; CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5"); // on sheet1 - CellRangeAddress destRange = CellRangeAddress.valueOf("F4:J6"); // on sheet2 + CellRangeAddress destRange = CellRangeAddress.valueOf("F4:J6"); // on sheet2 transSheetRangeCopier.copyRange(tileRange, destRange); assertEquals("1.3", getCellContent(destSheet, "H4")); assertEquals("1.3", getCellContent(destSheet, "J4")); @@ -86,7 +86,36 @@ public abstract class TestRangeCopier { assertEquals("SUM(G3:I3)", getCellContent(destSheet, "H5")); assertEquals("$C1+I$2", getCellContent(destSheet, "I5")); } - + + @Test + public void testEmptyRow() { + // leave some rows empty in-between + Row row = sheet1.createRow(23); + row.createCell(0).setCellValue(1.2); + + Sheet destSheet = sheet2; + CellRangeAddress tileRange = CellRangeAddress.valueOf("A1:A100"); // on sheet1 + CellRangeAddress destRange = CellRangeAddress.valueOf("G1:G100"); // on sheet2 + transSheetRangeCopier.copyRange(tileRange, destRange); + + assertEquals("1.2", getCellContent(destSheet, "G24")); + } + + @Test + public void testSameSheet() { + // leave some rows empty in-between + Row row = sheet1.createRow(23); + row.createCell(0).setCellValue(1.2); + + CellRangeAddress tileRange = CellRangeAddress.valueOf("A1:A100"); // on sheet1 + CellRangeAddress destRange = CellRangeAddress.valueOf("G1:G100"); // on sheet2 + + // use the a RangeCopier with the same Sheet for source and dest + rangeCopier.copyRange(tileRange, destRange); + + assertEquals("1.2", getCellContent(sheet1, "G24")); + } + protected static String getCellContent(Sheet sheet, String coordinates) { try { CellReference p = new CellReference(coordinates); |