]> source.dussan.org Git - poi.git/commitdiff
add more commented-out tests for bug 58779: closing an XML Workbook that was not...
authorJaven O'Neal <onealj@apache.org>
Sun, 27 Mar 2016 13:04:37 +0000 (13:04 +0000)
committerJaven O'Neal <onealj@apache.org>
Sun, 27 Mar 2016 13:04:37 +0000 (13:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736742 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java

index 6d89aa500eecd09ea48f939174570d33d5e966e1..e9611891dbd403c5feed561dfd184d59dee16fab 100644 (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);
+    }
 }
index b73593b261b2305a14664913afd00d37e8b46af6..41cb7e5fec9f3bb347c2ca150682e355c572f6f1 100644 (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);
+    }
 }
index 6fa22bc4d159b106058960997c302199df83f34f..a5f48eec2e7bfb7b79c6159636cb8c7529eee24f 100644 (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);
+    }
 }
index d153df2ea16970f7a8dad2c8c8734d6f5a196ccd..ba1e3d08455f7505cbd29c752c33686aa2602045 100644 (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);
+    }
 }