add test case

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879485 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2020-07-03 17:02:59 +00:00
parent 9f8b864c41
commit 3aa573bdd8
3 changed files with 35 additions and 48 deletions

View File

@ -101,41 +101,9 @@ public class DeferredSXSSFWorkbook extends SXSSFWorkbook {
*/
@Override
public Iterator<Sheet> sheetIterator() {
return new SheetIterator<Sheet>();
return new SheetIterator<>();
}
private final class SheetIterator<T extends Sheet> implements Iterator<T> {
final private Iterator<XSSFSheet> it;
@SuppressWarnings("unchecked")
public SheetIterator() {
it = (Iterator<XSSFSheet>) (Iterator<? extends Sheet>) _wb.iterator();
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
@SuppressWarnings("unchecked")
public T next() throws NoSuchElementException {
final XSSFSheet xssfSheet = it.next();
DeferredSXSSFSheet sxSheet = (DeferredSXSSFSheet) getSXSSFSheet(xssfSheet);
return (T) (sxSheet == null ? xssfSheet : sxSheet);
}
/**
* Unexpected behavior may occur if sheets are reordered after iterator has been created. Support for the remove
* method may be added in the future if someone can figure out a reliable implementation.
*/
@Override
public void remove() throws IllegalStateException {
throw new UnsupportedOperationException("remove method not supported on XSSFWorkbook.iterator(). "
+ "Use Sheet.removeSheetAt(int) instead.");
}
}
/**
* Alias for {@link #sheetIterator()} to allow foreach loops
*/

View File

@ -367,18 +367,13 @@ public class SXSSFWorkbook implements Workbook {
void deregisterSheetMapping(XSSFSheet xSheet)
{
SXSSFSheet sxSheet=getSXSSFSheet(xSheet);
// ensure that the writer is closed in all cases to not have lingering writers
try {
sxSheet.getSheetDataWriter().close();
} catch (IOException e) {
// ignore exception here
SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
if (sxSheet != null) {
// ensure that the writer is closed in all cases to not have lingering writers
IOUtils.closeQuietly(sxSheet.getSheetDataWriter());
_sxFromXHash.remove(sxSheet);
_xFromSxHash.remove(xSheet);
}
_sxFromXHash.remove(sxSheet);
_xFromSxHash.remove(xSheet);
}
protected XSSFSheet getSheetFromZipEntryName(String sheetRef)
@ -755,7 +750,7 @@ public class SXSSFWorkbook implements Workbook {
return new SheetIterator<>();
}
private final class SheetIterator<T extends Sheet> implements Iterator<T> {
protected final class SheetIterator<T extends Sheet> implements Iterator<T> {
final private Iterator<XSSFSheet> it;
@SuppressWarnings("unchecked")
public SheetIterator() {

View File

@ -199,12 +199,36 @@ public final class TestDeferredSXSSFWorkbook extends BaseTestXWorkbook {
assertNull(wr);
wb.close();
}
@Test
public void removeSheet() throws IOException {
try (DeferredSXSSFWorkbook wb = new DeferredSXSSFWorkbook()) {
DeferredSXSSFSheet sheet1 = wb.createSheet("sheet1");
sheet1.setRowGenerator((sh) -> {
Row row = sh.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("sheet1");
});
DeferredSXSSFSheet sheet2 = wb.createSheet("sheet2");
sheet2.setRowGenerator((sh) -> {
Row row = sh.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("sheet2");
});
wb.removeSheetAt(0);
try (XSSFWorkbook wb2 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb)) {
assertNull(wb2.getSheet( "sheet1"));
XSSFSheet xssfSheet = wb2.getSheet( "sheet2");
assertNotNull(xssfSheet);
assertEquals("sheet2", xssfSheet.getRow(0).getCell(0).getStringCellValue());
}
}
}
@Test
public void gzipSheetdataWriter() throws IOException {
DeferredSXSSFWorkbook wb = new DeferredSXSSFWorkbook();
wb.setCompressTempFiles(true);
final int rowNum = 1000;
final int sheetNum = 5;
populateData(wb, 1000, 5);