From 4f23e946fbd95c1a46a15c84483d0969bedf0b13 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 6 Jul 2020 13:59:28 +0000 Subject: [PATCH] get TestDeferredSXSSFWorkbook to use DeferredSXSSFWorkbook in all of its tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879554 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/streaming/DeferredSXSSFWorkbook.java | 23 ------- .../apache/poi/xssf/streaming/SXSSFSheet.java | 2 +- .../streaming/TestDeferredSXSSFWorkbook.java | 66 ++++++++++--------- 3 files changed, 36 insertions(+), 55 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/DeferredSXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/DeferredSXSSFWorkbook.java index 780ebf0c80..7dbf4d2f11 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/DeferredSXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/DeferredSXSSFWorkbook.java @@ -20,7 +20,6 @@ package org.apache.poi.xssf.streaming; import java.io.IOException; import java.io.OutputStream; import java.util.Iterator; -import java.util.NoSuchElementException; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.Beta; @@ -103,23 +102,6 @@ public class DeferredSXSSFWorkbook extends SXSSFWorkbook { public Iterator sheetIterator() { return new SheetIterator<>(); } - - /** - * Alias for {@link #sheetIterator()} to allow foreach loops - */ - @Override - public Iterator iterator() { - return sheetIterator(); - } - - @Override - public SXSSFSheet getSheetAt(int index) { - throw new RuntimeException("Not supported by DeferredSXSSFWorkbook"); - } - - public XSSFSheet getXSSFSheetAt(int index) { - return _wb.getSheetAt(index); - } /** * Gets the sheet at the given index for streaming. @@ -137,11 +119,6 @@ public class DeferredSXSSFWorkbook extends SXSSFWorkbook { } } - @Override - public SXSSFSheet getSheet(String name) { - throw new RuntimeException("Not supported by DeferredSXSSFWorkbook"); - } - public XSSFSheet getXSSFSheet(String name) { return _wb.getSheet(name); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index debb2c45e0..5333532c77 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -132,7 +132,7 @@ public class SXSSFSheet implements Sheet } // attempt to overwrite a row that is already flushed to disk - if(rownum <= _writer.getLastFlushedRow() ) { + if(_writer != null && rownum <= _writer.getLastFlushedRow() ) { throw new IllegalArgumentException( "Attempting to write a row["+rownum+"] " + "in the range [0," + _writer.getLastFlushedRow() + "] that is already written to disk."); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestDeferredSXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestDeferredSXSSFWorkbook.java index ffdbc0516f..4e72ff69b5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestDeferredSXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestDeferredSXSSFWorkbook.java @@ -24,7 +24,7 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellReference; -import org.apache.poi.xssf.SXSSFITestDataProvider; +import org.apache.poi.xssf.DeferredSXSSFITestDataProvider; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; @@ -40,12 +40,12 @@ import static org.junit.Assert.*; public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { public TestDeferredSXSSFWorkbook() { - super(SXSSFITestDataProvider.instance); + super(DeferredSXSSFITestDataProvider.instance); } @After public void tearDown() { - ((SXSSFITestDataProvider) _testDataProvider).cleanup(); + ((DeferredSXSSFITestDataProvider) _testDataProvider).cleanup(); } /** @@ -82,15 +82,24 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { @Override @Ignore("SXSSF doesn't update formulas on sheet name changes, as most cells probably aren't in memory at the time") @Test - public void setSheetName() { - } - + public void setSheetName() {} + + @Override + @Ignore("DeferredSXSSF code disposes rows in a way that breaks this test") + @Test + public void parentReferences() {} + + @Override + @Ignore("DeferredSXSSF code disposes rows in a way that breaks this test") + @Test + public void unicodeInAll() {} + @Test public void existingWorkbook() throws IOException { XSSFWorkbook xssfWb1 = new XSSFWorkbook(); xssfWb1.createSheet("S1"); DeferredSXSSFWorkbook wb1 = new DeferredSXSSFWorkbook(xssfWb1); - XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb1); + XSSFWorkbook xssfWb2 = DeferredSXSSFITestDataProvider.instance.writeOutAndReadBack(wb1); assertTrue(wb1.dispose()); DeferredSXSSFWorkbook wb2 = new DeferredSXSSFWorkbook(xssfWb2); @@ -105,12 +114,7 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { wb2.close(); wb1.close(); } - - @Test - public void useSharedStringsTable() throws Exception { - // not supported with DeferredSXSSF - } - + @Test public void addToExistingWorkbook() throws IOException { XSSFWorkbook xssfWb1 = new XSSFWorkbook(); @@ -120,7 +124,7 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { Cell cell = row.createCell(1); cell.setCellValue("value 2_1_1"); DeferredSXSSFWorkbook wb1 = new DeferredSXSSFWorkbook(xssfWb1); - XSSFWorkbook xssfWb2 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb1); + XSSFWorkbook xssfWb2 = DeferredSXSSFITestDataProvider.instance.writeOutAndReadBack(wb1); assertTrue(wb1.dispose()); xssfWb1.close(); @@ -148,7 +152,7 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { cell3_1_1.setCellValue("value 3_1_1"); }); - XSSFWorkbook xssfWb3 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb2); + XSSFWorkbook xssfWb3 = DeferredSXSSFITestDataProvider.instance.writeOutAndReadBack(wb2); wb2.close(); assertEquals(3, xssfWb3.getNumberOfSheets()); @@ -216,7 +220,7 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { cell.setCellValue("sheet2"); }); wb.removeSheetAt(0); - try (XSSFWorkbook wb2 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb)) { + try (XSSFWorkbook wb2 = DeferredSXSSFITestDataProvider.instance.writeOutAndReadBack(wb)) { assertNull(wb2.getSheet( "sheet1")); XSSFSheet xssfSheet = wb2.getSheet( "sheet2"); assertNotNull(xssfSheet); @@ -233,7 +237,7 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { final int sheetNum = 5; populateData(wb, 1000, 5); - XSSFWorkbook xwb = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb); + XSSFWorkbook xwb = DeferredSXSSFITestDataProvider.instance.writeOutAndReadBack(wb); for (int i = 0; i < sheetNum; i++) { Sheet sh = xwb.getSheetAt(i); assertEquals("sheet" + i, sh.getSheetName()); @@ -255,6 +259,20 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { xwb.close(); wb.close(); } + + @Test + public void workbookDispose() throws IOException { + DeferredSXSSFWorkbook wb1 = new DeferredSXSSFWorkbook(); + // the underlying writer is SheetDataWriter + assertWorkbookDispose(wb1); + wb1.close(); + + DeferredSXSSFWorkbook wb2 = new DeferredSXSSFWorkbook(); + wb2.setCompressTempFiles(true); + // the underlying writer is GZIPSheetDataWriter + assertWorkbookDispose(wb2); + wb2.close(); + } private static void assertWorkbookDispose(DeferredSXSSFWorkbook wb) { populateData(wb, 1000, 5); @@ -291,18 +309,4 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook { }); } } - - @Test - public void workbookDispose() throws IOException { - DeferredSXSSFWorkbook wb1 = new DeferredSXSSFWorkbook(); - // the underlying writer is SheetDataWriter - assertWorkbookDispose(wb1); - wb1.close(); - - DeferredSXSSFWorkbook wb2 = new DeferredSXSSFWorkbook(); - wb2.setCompressTempFiles(true); - // the underlying writer is GZIPSheetDataWriter - assertWorkbookDispose(wb2); - wb2.close(); - } }