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