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
This commit is contained in:
Javen O'Neal 2016-03-27 13:04:37 +00:00
parent 01b20a6567
commit 6b6b6a668e
4 changed files with 88 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}