From 6b6b6a668e6f4f46025a755dfcbaa5365f274f26 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sun, 27 Mar 2016 13:04:37 +0000 Subject: [PATCH] add more commented-out tests for bug 58779: closing an XML Workbook that was not opened as read-only will modify the file on disk git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736742 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/streaming/TestSXSSFWorkbook.java | 30 +++++++++++++++++++ .../poi/xssf/usermodel/TestXSSFWorkbook.java | 28 +++++++++++++++++ .../poi/hssf/usermodel/TestHSSFWorkbook.java | 20 +++++++++++++ .../poi/ss/usermodel/BaseTestWorkbook.java | 10 +++++++ 4 files changed, 88 insertions(+) diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java index 6d89aa500..e9611891d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java @@ -26,10 +26,14 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.POIDataSamples; import org.apache.poi.POITestCase; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.BaseTestWorkbook; import org.apache.poi.ss.usermodel.Cell; @@ -444,4 +448,30 @@ public final class TestSXSSFWorkbook extends BaseTestWorkbook { public void getSpreadsheetVersion() throws IOException { verifySpreadsheetVersion(SpreadsheetVersion.EXCEL2007); } + + @Test + public void closeDoesNotModifyWorkbook() throws IOException, InvalidFormatException { + final String filename = "SampleSS.xlsx"; + final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename); + SXSSFWorkbook wb; + + // Some tests commented out because close() modifies the file + // See bug 58779 + + // String + //wb = new SXSSFWorkbook(new XSSFWorkbook(file.getPath())); + //assertCloseDoesNotModifyFile(filename, wb); + + // File + //wb = new SXSSFWorkbook(new XSSFWorkbook(file)); + //assertCloseDoesNotModifyFile(filename, wb); + + // InputStream + wb = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream(file))); + assertCloseDoesNotModifyFile(filename, wb); + + // OPCPackage + //wb = new SXSSFWorkbook(new XSSFWorkbook(OPCPackage.open(file))); + //assertCloseDoesNotModifyFile(filename, wb); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index b73593b26..41cb7e5fe 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -29,6 +29,7 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -37,6 +38,7 @@ import java.util.Iterator; import java.util.List; import java.util.zip.CRC32; +import org.apache.poi.POIDataSamples; import org.apache.poi.POIXMLProperties; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -1085,4 +1087,30 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { public void getSpreadsheetVersion() throws IOException { verifySpreadsheetVersion(SpreadsheetVersion.EXCEL2007); } + + @Test + public void closeDoesNotModifyWorkbook() throws IOException, InvalidFormatException { + final String filename = "SampleSS.xlsx"; + final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename); + Workbook wb; + + // Some tests commented out because close() modifies the file + // See bug 58779 + + // String + //wb = new XSSFWorkbook(file.getPath()); + //assertCloseDoesNotModifyFile(filename, wb); + + // File + //wb = new XSSFWorkbook(file); + //assertCloseDoesNotModifyFile(filename, wb); + + // InputStream + wb = new XSSFWorkbook(new FileInputStream(file)); + assertCloseDoesNotModifyFile(filename, wb); + + // OPCPackage + //wb = new XSSFWorkbook(OPCPackage.open(file)); + //assertCloseDoesNotModifyFile(filename, wb); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 6fa22bc4d..a5f48eec2 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -28,6 +28,7 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -1262,4 +1263,23 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { public void getSpreadsheetVersion() throws IOException { verifySpreadsheetVersion(SpreadsheetVersion.EXCEL97); } + + @Test + public void closeDoesNotModifyWorkbook() throws IOException { + final String filename = "SampleSS.xls"; + final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename); + Workbook wb; + + // File via POIFileStream (java.io) + wb = new HSSFWorkbook(new POIFSFileSystem(file)); + assertCloseDoesNotModifyFile(filename, wb); + + // File via NPOIFileStream (java.nio) + wb = new HSSFWorkbook(new NPOIFSFileSystem(file)); + assertCloseDoesNotModifyFile(filename, wb); + + // InputStream + wb = new HSSFWorkbook(new FileInputStream(file)); + assertCloseDoesNotModifyFile(filename, wb); + } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index d153df2ea..ba1e3d084 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.usermodel; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -30,6 +31,7 @@ import java.io.OutputStream; import java.util.ConcurrentModificationException; import java.util.Iterator; +import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.util.CellRangeAddress; @@ -832,4 +834,12 @@ public abstract class BaseTestWorkbook { assertEquals(expected, wb.getSpreadsheetVersion()); wb.close(); } + + protected static void assertCloseDoesNotModifyFile(String filename, Workbook wb) throws IOException { + final byte[] before = HSSFTestDataSamples.getTestDataFileContent(filename); + wb.close(); + final byte[] after = HSSFTestDataSamples.getTestDataFileContent(filename); + assertArrayEquals(filename + " sample file was modified as a result of closing the workbook", + before, after); + } }