]> source.dussan.org Git - poi.git/commitdiff
Bug 51158: clear out Memory-based part before writing to it,
authorDominik Stadler <centic@apache.org>
Thu, 26 Dec 2013 17:55:36 +0000 (17:55 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 26 Dec 2013 17:55:36 +0000 (17:55 +0000)
except for PictureData items, which keep the original image
data in the Part-object directly.
Add reproducer-unit tests and enhance some related unit tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1553525 13f79535-47bb-0310-9956-ffa450edef68

17 files changed:
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFChart.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java

index fe147b622817271f4ea5161276eb9be8d3a2e6f8..de889d9df1117a8d6d3d9ca7f52d0298e7a8a9b7 100644 (file)
@@ -18,8 +18,14 @@ package org.apache.poi;
 
 import java.io.IOException;
 import java.net.URI;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
@@ -310,6 +316,9 @@ public class POIXMLDocumentPart {
      * @param alreadySaved    context set containing already visited nodes
      */
     protected final void onSave(Set<PackagePart> alreadySaved) throws IOException{
+        // this usually clears out previous content in the part...
+        prepareForCommit();
+
         commit();
         alreadySaved.add(this.getPackagePart());
         for(POIXMLDocumentPart p : relations.values()){
@@ -319,6 +328,20 @@ public class POIXMLDocumentPart {
         }
     }
 
+    /**
+     * Ensure that a memory based package part does not have lingering data from previous 
+     * commit() calls. 
+     * 
+     * Note: This is overwritten for some objects, as *PictureData seem to store the actual content 
+     * in the part directly without keeping a copy like all others therefore we need to handle them differently.
+     */
+    protected void prepareForCommit() {
+        PackagePart part = this.getPackagePart();
+        if(part != null) {
+            part.clear();
+        }
+    }
+
     /**
      * Create a new child POIXMLDocumentPart
      *
index 56f6a66e915cdd731be23c53ea0af567af42a2b4..e97349447f0330f22c6cb3247514f5098fe176e3 100644 (file)
@@ -522,11 +522,11 @@ public abstract class PackagePart implements RelationshipSource {
                        // Create a memory part
                        PackagePart part = _container.createPart(this._partName,
                                        this._contentType.toString(), false);
-                       part._relationships = this._relationships;
                        if (part == null) {
-                               throw new InvalidOperationException(
-                                               "Can't create a temporary part !");
+                           throw new InvalidOperationException(
+                                   "Can't create a temporary part !");
                        }
+                       part._relationships = this._relationships;
                        outStream = part.getOutputStreamImpl();
                } else {
                        outStream = this.getOutputStreamImpl();
@@ -690,4 +690,10 @@ public abstract class PackagePart implements RelationshipSource {
         * respective buffer.
         */
        public abstract void flush();
+
+       /**
+        * Allows sub-classes to clean up before new data is added.
+        */
+       public void clear() {
+       }
 }
index f38d7a26d88e804557ad12dea84ece6796e091f4..f2d8c31151b1cad02e86f61e66f68d03d07f5da0 100644 (file)
@@ -102,7 +102,8 @@ public final class MemoryPackagePart extends PackagePart {
                return data == null ? 0 : data.length;
        }
 
-       public void clear() {
+    @Override
+    public void clear() {
                data = null;
        }
 
index 22f7ea4a32fadb853f1e3ec9aefe09e9a1fa2d14..de31fb2fd633ff76a53ffe1c9ef802d7e8cb8914 100644 (file)
 ==================================================================== */
 package org.apache.poi.xslf.usermodel;
 
+import java.awt.Dimension;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLRelation;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.*;
-import org.apache.poi.util.*;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.TargetMode;
+import org.apache.poi.util.Beta;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.PackageHelper;
+import org.apache.poi.util.Units;
 import org.apache.poi.xslf.XSLFSlideShow;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
@@ -35,17 +55,6 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
 import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideSize;
 import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
 
-import java.awt.Dimension;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Pattern;
-
 /**
  * High level representation of a ooxml slideshow.
  * This is the first object most users will construct whether
@@ -163,6 +172,7 @@ public class XMLSlideShow  extends POIXMLDocument {
     /**
      * Get the document's embedded files.
      */
+    @Override
     public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
         return Collections.unmodifiableList(
                 getPackage().getPartsByName(Pattern.compile("/ppt/embeddings/.*?"))
index 2122e62d2677c762fbd0a1e8dc66fa9aa84ab149..7ade4f948d67b5bdb6c753fb1f6c4f4fd1a4b9c5 100644 (file)
@@ -19,6 +19,8 @@
 
 package org.apache.poi.xslf.usermodel;
 
+import java.io.IOException;
+
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLRelation;
@@ -27,8 +29,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.IOUtils;
 
-import java.io.IOException;
-
 /**
  * Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type
  *
@@ -206,4 +206,13 @@ public final class XSLFPictureData extends POIXMLDocumentPart {
         }
         return checksum;
     }
+
+    /**
+     * *PictureData objects store the actual content in the part directly without keeping a 
+     * copy like all others therefore we need to handle them differently.
+     */
+    @Override
+    protected void prepareForCommit() {
+        // do not clear the part here
+    }
 }
\ No newline at end of file
index 391db1ea4e7f6fe14e3f4bb13beb3d2bd6bb684c..43599feb755904be95e8025befd3b705b9129436 100644 (file)
@@ -129,4 +129,13 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
     public String getMimeType() {
         return getPackagePart().getContentType();
     }
+
+    /**
+     * *PictureData objects store the actual content in the part directly without keeping a 
+     * copy like all others therefore we need to handle them differently.
+     */
+    @Override
+    protected void prepareForCommit() {
+        // do not clear the part here
+    }
 }
index 532d40b00f098d2264e9d511235d51abb73a025d..e819da5dbbb446222546665a02af6630504d93a7 100644 (file)
@@ -234,4 +234,13 @@ public class XWPFPictureData extends POIXMLDocumentPart {
     public int hashCode() {
         return getChecksum().hashCode();
     }
+
+    /**
+     * *PictureData objects store the actual content in the part directly without keeping a 
+     * copy like all others therefore we need to handle them differently.
+     */
+    @Override
+    protected void prepareForCommit() {
+        // do not clear the part here
+    }
 }
index 2cbaeed63e5cf0c8f844aad58ad3d701bb254bff..955b273c1e4610a9bd1fe7dab8a704292441217e 100644 (file)
 ==================================================================== */\r
 package org.apache.poi.xslf.usermodel;\r
 \r
-import junit.framework.TestCase;\r
-import org.apache.poi.xslf.XSLFTestDataSamples;\r
-import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
+import static org.junit.Assert.assertArrayEquals;\r
 \r
-import java.util.Arrays;\r
 import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import junit.framework.TestCase;\r
+\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
+\r
 /**\r
  * @author Yegor Kozlov\r
  */\r
@@ -34,6 +36,7 @@ public class TestXSLFPictureShape extends TestCase {
         XMLSlideShow ppt = new XMLSlideShow();\r
         assertEquals(0, ppt.getAllPictures().size());\r
         byte[] data1 = new byte[100];\r
+        for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }\r
         int idx1 = ppt.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
         assertEquals(0, idx1);\r
         assertEquals(1, ppt.getAllPictures().size());\r
@@ -41,25 +44,26 @@ public class TestXSLFPictureShape extends TestCase {
         XSLFSlide slide = ppt.createSlide();\r
         XSLFPictureShape shape1 = slide.createPicture(idx1);\r
         assertNotNull(shape1.getPictureData());\r
-        assertTrue(Arrays.equals(data1, shape1.getPictureData().getData()));\r
+        assertArrayEquals(data1, shape1.getPictureData().getData());\r
 \r
         byte[] data2 = new byte[200];\r
+        for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }\r
         int idx2 = ppt.addPicture(data2, XSLFPictureData.PICTURE_TYPE_PNG);\r
         XSLFPictureShape shape2 = slide.createPicture(idx2);\r
         assertNotNull(shape2.getPictureData());\r
         assertEquals(1, idx2);\r
         assertEquals(2, ppt.getAllPictures().size());\r
-        assertTrue(Arrays.equals(data2, shape2.getPictureData().getData()));\r
+        assertArrayEquals(data2, shape2.getPictureData().getData());\r
 \r
         ppt = XSLFTestDataSamples.writeOutAndReadBack(ppt);\r
         List<XSLFPictureData> pics = ppt.getAllPictures();\r
         assertEquals(2, pics.size());\r
-        assertTrue(Arrays.equals(data1, pics.get(0).getData()));\r
-        assertTrue(Arrays.equals(data2, pics.get(1).getData()));\r
+        assertArrayEquals(data1, pics.get(0).getData());\r
+        assertArrayEquals(data2, pics.get(1).getData());\r
 \r
         XSLFShape[] shapes = ppt.getSlides()[0].getShapes();\r
-        assertTrue(Arrays.equals(data1, ((XSLFPictureShape) shapes[0]).getPictureData().getData()));\r
-        assertTrue(Arrays.equals(data2, ((XSLFPictureShape) shapes[1]).getPictureData().getData()));\r
+        assertArrayEquals(data1, ((XSLFPictureShape) shapes[0]).getPictureData().getData());\r
+        assertArrayEquals(data2, ((XSLFPictureShape) shapes[1]).getPictureData().getData());\r
     }\r
 \r
     public void testCreateMultiplePictures() {\r
@@ -79,7 +83,7 @@ public class TestXSLFPictureShape extends TestCase {
             // POI saves images as image1.png, image2.png, etc.\r
             String fileName = "image" + (elementIndex + 1) + ".png";\r
             assertEquals(fileName, picture.getPictureData().getFileName());\r
-            assertTrue(Arrays.equals(data, picture.getPictureData().getData()));\r
+            assertArrayEquals(data, picture.getPictureData().getData());\r
         }\r
 \r
         // and then add next 20 images to a group\r
@@ -92,7 +96,7 @@ public class TestXSLFPictureShape extends TestCase {
             assertEquals(pictureIndex, elementIndex);   // added images have indexes 0,1,2....19\r
             String fileName = "image" + (pictureIndex + 1) + ".png";\r
             assertEquals(fileName, picture.getPictureData().getFileName());\r
-            assertTrue(Arrays.equals(data, picture.getPictureData().getData()));\r
+            assertArrayEquals(data, picture.getPictureData().getData());\r
         }\r
 \r
         // serialize, read back and check that all images are there\r
@@ -110,7 +114,7 @@ public class TestXSLFPictureShape extends TestCase {
             XSLFPictureData data = pics.get(fileName);\r
             assertNotNull(data);\r
             assertEquals(fileName, data.getFileName());\r
-            assertTrue(Arrays.equals(data1, data.getData()));\r
+            assertArrayEquals(data1, data.getData());\r
         }\r
     }\r
 \r
@@ -127,7 +131,9 @@ public class TestXSLFPictureShape extends TestCase {
         assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));\r
 \r
         XSLFSlide slide1 = ppt.createSlide();\r
+        assertNotNull(slide1);\r
         XSLFSlide slide2 = ppt.createSlide();\r
+        assertNotNull(slide2);\r
 \r
     }\r
 \r
@@ -146,7 +152,7 @@ public class TestXSLFPictureShape extends TestCase {
         XSLFSlide slide2 = ppt2.createSlide().importContent(slide1);\r
         XSLFPictureShape shape2 = (XSLFPictureShape)slide2.getShapes()[0];\r
 \r
-        assertTrue(Arrays.equals(data1, shape2.getPictureData().getData()));\r
+        assertArrayEquals(data1, shape2.getPictureData().getData());\r
 \r
         CTPicture ctPic2 = (CTPicture)shape2.getXmlObject();\r
         assertFalse(ctPic2.getNvPicPr().getNvPr().isSetCustDataLst());\r
index 0298b051737781014f379c6574483ea88803053c..e06bd213f36ef094e1abae1475586149bd0ba21e 100644 (file)
 
 package org.apache.poi.xssf.model;
 
-import java.util.List;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
-import java.io.*;
+import java.util.List;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.POIXMLException;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.POIDataSamples;
-import org.apache.poi.POIXMLException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
@@ -130,6 +132,8 @@ public final class TestSharedStringsTable extends TestCase {
             CTRst st2 = items2.get(i);
             assertEquals(st1.toString(), st2.toString());
         }
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     /**
@@ -159,6 +163,8 @@ public final class TestSharedStringsTable extends TestCase {
             String val = s.getRow(i++).getCell(0).getStringCellValue();
             assertEquals(str, val);
         }
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(w));
     }
 
     private List<String> readStrings(String filename) throws IOException {
index 73ec1d005ffb7900991b0714f27a41d70967909e..01fa47fe6947266c63a18b938c0cdd1e733917cc 100644 (file)
 
 package org.apache.poi.xssf.model;
 
+import junit.framework.TestCase;
+
+import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.xssf.XSSFTestDataSamples;
-
-import junit.framework.TestCase;
 
 public final class TestStylesTable extends TestCase {
        private String testFile = "Formatting.xlsx";
@@ -51,6 +51,8 @@ public final class TestStylesTable extends TestCase {
                assertEquals(1, st._getXfsSize());
                assertEquals(1, st._getStyleXfsSize());
                assertEquals(0, st._getNumberFormatSize());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
        }
 
        public void testLoadExisting() {
@@ -60,7 +62,10 @@ public final class TestStylesTable extends TestCase {
                StylesTable st = workbook.getStylesSource();
 
                doTestExisting(st);
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
        }
+
        public void testLoadSaveLoad() {
                XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
                assertNotNull(workbook.getStylesSource());
@@ -71,6 +76,7 @@ public final class TestStylesTable extends TestCase {
                st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
                doTestExisting(st);
        }
+
        public void doTestExisting(StylesTable st) {
                // Check contents
                assertNotNull(st.getCTStylesheet());
@@ -123,6 +129,8 @@ public final class TestStylesTable extends TestCase {
                assertEquals("yyyy-mm-dd", st.getNumberFormatAt(nf1));
                assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
                assertEquals(nf2, st.putNumberFormat("yyyy-mm-DD"));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
        }
 
        public void testPopulateExisting() {
@@ -147,5 +155,7 @@ public final class TestStylesTable extends TestCase {
                assertEquals("YYYY-mm-dd", st.getNumberFormatAt(nf1));
                assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
                assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
        }
 }
index 19e9a65a322cefb85ce58f44fbf9dac8824c400e..8d427be552d0a1b94f29ecb021e2dc1d9fa4ee78 100644 (file)
@@ -31,16 +31,7 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
 
 public class TestXSSFCellStyle extends TestCase {
        private StylesTable stylesTable;
@@ -568,6 +559,8 @@ public class TestXSSFCellStyle extends TestCase {
         assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
         assertNull(style1.getFillBackgroundXSSFColor());
 
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb1));
+
         //compatibility with HSSF
         HSSFWorkbook wb2 = new HSSFWorkbook();
         HSSFCellStyle style2 = wb2.createCellStyle();
@@ -588,7 +581,6 @@ public class TestXSSFCellStyle extends TestCase {
 
 
        public void testGetFillForegroundColor() {
-
         XSSFWorkbook wb = new XSSFWorkbook();
         StylesTable styles = wb.getStylesSource();
         assertEquals(1, wb.getNumCellStyles());
@@ -620,6 +612,8 @@ public class TestXSSFCellStyle extends TestCase {
             assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
             assertEquals(4, styles.getFills().size());
         }
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
        }
 
        public void testGetFillPattern() {
@@ -752,7 +746,10 @@ public class TestXSSFCellStyle extends TestCase {
       assertTrue(fnt == clone.getFont());
       assertTrue(18 == clone.getDataFormat());
       assertEquals(2, wb.getNumberOfFonts());
+
+      assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
        }
+
        /**
         * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
         */
@@ -820,6 +817,9 @@ public class TestXSSFCellStyle extends TestCase {
        assertEquals("TestingFont", reload.getFont().getFontName());
        assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
        assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
+
+       assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbOrig));
+       assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbClone));
    }
 
     /**
@@ -831,9 +831,10 @@ public class TestXSSFCellStyle extends TestCase {
         StylesTable st = workbook.getStylesSource();
         assertEquals(0, st._getStyleXfsSize());
         
-        
         XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point
         assertNull(style.getStyleXf());
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
     }
 
     /**
@@ -848,6 +849,8 @@ public class TestXSSFCellStyle extends TestCase {
         // no exception at this point
         XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
         assertNull(style.getStyleXf());
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
     }
 
     public void testShrinkToFit() {
@@ -878,5 +881,8 @@ public class TestXSSFCellStyle extends TestCase {
        r = s.getRow(0);
        assertEquals(false, r.getCell(0).getCellStyle().getShrinkToFit());
        assertEquals(true,  r.getCell(1).getCellStyle().getShrinkToFit());
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wbOrig));
     }
 }
index 00b0e36b2c0edbe035c211f63dbdeee152e631c6..1c8732402e138f4705dc5bc9bdbb4f5df2b3b22d 100644 (file)
@@ -32,6 +32,8 @@ public final class TestXSSFChart extends TestCase {
         assertEquals(0, s1.getRelations().size());
         assertEquals(1, s2.getRelations().size());
         assertEquals(1, s3.getRelations().size());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     public void testGetCharts() throws Exception {
@@ -54,6 +56,8 @@ public final class TestXSSFChart extends TestCase {
        
        chart = s3.createDrawingPatriarch().getCharts().get(0);
        assertEquals("Sheet 3 Chart with Title", chart.getTitle().getString());
+       
+       assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
        public void testAddChartsToNewWorkbook() throws Exception {
@@ -69,6 +73,9 @@ public final class TestXSSFChart extends TestCase {
 
                XSSFClientAnchor a2 = new XSSFClientAnchor(0, 0, 0, 0, 1, 11, 10, 60);
                XSSFChart c2 = d1.createChart(a2);
+               assertNotNull(c2);
                assertEquals(2, d1.getCharts().size());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
        }
 }
index 889c0d8e701166f5a8ab2046836d38b41a62ca48..2ca3dbaa911cc5c099fefe403b61d6d924c7daee 100644 (file)
@@ -22,6 +22,7 @@ import org.apache.poi.ss.usermodel.BaseTestCellComment;
 import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.xmlbeans.XmlObject;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
@@ -146,6 +147,8 @@ public final class TestXSSFComment extends BaseTestCellComment  {
         assertEquals(8.5, rPr.getSzArray(0).getVal());
         assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
         assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     public void testAuthor() {
index 77f48c74aa91564270907dc47e7015cfb3ad8b7e..3eea634e189bb79aaf138e6afb0a755fbd8b7136 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
-import java.awt.*;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.awt.Color;
 import java.util.Arrays;
 import java.util.List;
 
@@ -68,6 +64,7 @@ public class TestXSSFDrawing extends TestCase {
 
         for(XSSFShape sh : shapes) assertNotNull(sh.getAnchor());
 
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     public void testNew() throws Exception {
@@ -94,6 +91,7 @@ public class TestXSSFDrawing extends TestCase {
         c1.setLineStyle(1);
 
         XSSFShapeGroup c2 = drawing.createGroup(new XSSFClientAnchor(0,0,0,0,0,0,5,5));
+        assertNotNull(c2);
 
         XSSFSimpleShape c3 = drawing.createSimpleShape(new XSSFClientAnchor(0,0,0,0,2,2,3,4));
         c3.setText(new XSSFRichTextString("Test String"));
@@ -139,6 +137,8 @@ public class TestXSSFDrawing extends TestCase {
         String xml = ctDrawing.toString();
         assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
         assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     public void testMultipleDrawings(){
@@ -146,9 +146,12 @@ public class TestXSSFDrawing extends TestCase {
         for (int i = 0; i < 3; i++) {
             XSSFSheet sheet = wb.createSheet();
             XSSFDrawing drawing = sheet.createDrawingPatriarch();
+            assertNotNull(drawing);
         }
         OPCPackage pkg = wb.getPackage();
         assertEquals(3, pkg.getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     public void testClone() throws Exception{
@@ -175,12 +178,14 @@ public class TestXSSFDrawing extends TestCase {
         assertEquals(shapes1.size(), shapes2.size());
 
         for(int i = 0; i < shapes1.size(); i++){
-            XSSFShape sh1 = (XSSFShape)shapes1.get(i);
-            XSSFShape sh2 = (XSSFShape)shapes2.get(i);
+            XSSFShape sh1 = shapes1.get(i);
+            XSSFShape sh2 = shapes2.get(i);
 
             assertTrue(sh1.getClass() == sh2.getClass());
             assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
         }
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     /**
@@ -216,7 +221,8 @@ public class TestXSSFDrawing extends TestCase {
         assertTrue(Arrays.equals(
                 new byte[]{0, (byte)128, (byte)128} ,
                 rPr.getSolidFill().getSrgbClr().getVal()));
-
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     /**
@@ -229,13 +235,16 @@ public class TestXSSFDrawing extends TestCase {
 
         XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
         XSSFShape shape1 = drawing.createTextbox(anchor1);
+        assertNotNull(shape1);
 
         XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 5);
         XSSFShape shape2 = drawing.createTextbox(anchor2);
+        assertNotNull(shape2);
 
         int pictureIndex= wb.addPicture(new byte[]{}, XSSFWorkbook.PICTURE_TYPE_PNG);
         XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
         XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex);
+        assertNotNull(shape3);
 
         wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
         sheet = wb.getSheetAt(0);
@@ -244,8 +253,8 @@ public class TestXSSFDrawing extends TestCase {
         assertEquals(shapes.get(0).getAnchor(), anchor1);
         assertEquals(shapes.get(1).getAnchor(), anchor2);
         assertEquals(shapes.get(2).getAnchor(), anchor3);
-
-
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     /**
@@ -277,14 +286,14 @@ public class TestXSSFDrawing extends TestCase {
         assertTrue(Arrays.equals(
                 new byte[]{0, (byte)128, (byte)128} ,
                 rPr.getSolidFill().getSrgbClr().getVal()));
-       
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     /**
      * Test setText single paragraph to ensure backwards compatibility
      */
     public void testSetTextSingleParagraph() {
-    
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet();
         XSSFDrawing drawing = sheet.createDrawingPatriarch();
@@ -311,13 +320,14 @@ public class TestXSSFDrawing extends TestCase {
         assertTrue(Arrays.equals(
                 new int[] { 0, 255, 255 } ,
                 new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     /**
      * Test addNewTextParagraph 
      */
     public void testAddNewTextParagraph() {
-    
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet();
         XSSFDrawing drawing = sheet.createDrawingPatriarch();
@@ -333,13 +343,14 @@ public class TestXSSFDrawing extends TestCase {
         List<XSSFTextRun> runs = para.getTextRuns();
         assertEquals(1, runs.size());
         assertEquals("Line 1", runs.get(0).getText());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     /**
      * Test addNewTextParagraph using RichTextString
      */
     public void testAddNewTextParagraphWithRTS() {
-    
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet();
         XSSFDrawing drawing = sheet.createDrawingPatriarch();
@@ -402,13 +413,14 @@ public class TestXSSFDrawing extends TestCase {
         assertTrue(Arrays.equals(
                 new int[] { 0, 255, 255 } ,
                 new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }    
     
     /**
      * Test add multiple paragraphs and retrieve text
      */
     public void testAddMultipleParagraphs() {
-    
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet();
         XSSFDrawing drawing = sheet.createDrawingPatriarch();
@@ -427,13 +439,14 @@ public class TestXSSFDrawing extends TestCase {
         List<XSSFTextParagraph> paras = shape.getTextParagraphs();
         assertEquals(4, paras.size()); // this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs
         assertEquals("Line 1\nLine 2\nLine 3", shape.getText());           
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     /**
      * Test setting the text, then adding multiple paragraphs and retrieve text
      */
     public void testSetAddMultipleParagraphs() {
-    
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet();
         XSSFDrawing drawing = sheet.createDrawingPatriarch();
@@ -451,6 +464,8 @@ public class TestXSSFDrawing extends TestCase {
         List<XSSFTextParagraph> paras = shape.getTextParagraphs();
         assertEquals(3, paras.size()); // this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs
         assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
     
     /**
@@ -480,6 +495,8 @@ public class TestXSSFDrawing extends TestCase {
 
         XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4); 
         assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 
     
@@ -538,7 +555,10 @@ public class TestXSSFDrawing extends TestCase {
         assertTrue(Arrays.equals(
                 new int[] { 0, 0, 255 } ,
                 new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }));
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
+
     /**
      * Test adding and reading back paragraphs as bullet points
      */
@@ -644,6 +664,8 @@ public class TestXSSFDrawing extends TestCase {
         builder.append(paraString10);
         
         assertEquals(builder.toString(), sshape.getText());
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }  
     
     /**
@@ -668,5 +690,7 @@ public class TestXSSFDrawing extends TestCase {
         sb.append("\t\n\t\n\t\n\t");
 
         assertEquals(sb.toString(), extracted);
+        
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
     }
 }
index 85fe04993648ccedf94d9f7abdb8739c76126b7e..fe06afcd89e7064ab30ed09adf895b62740a987d 100644 (file)
@@ -17,7 +17,8 @@
 
 package org.apache.poi.xssf.usermodel;
 
-import java.util.Arrays;
+import static org.junit.Assert.assertArrayEquals;
+
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -52,7 +53,7 @@ public final class TestXSSFPictureData extends TestCase {
         assertEquals(pictures.size() - 1, idx);
         XSSFPictureData pict = pictures.get(idx);
         assertEquals("jpeg", pict.suggestFileExtension());
-        assertTrue(Arrays.equals(pictureData, pict.getData()));
+        assertArrayEquals(pictureData, pict.getData());
     }
 
     public void testNew(){
@@ -70,22 +71,25 @@ public final class TestXSSFPictureData extends TestCase {
         int jpegIdx = wb.addPicture(jpegData, XSSFWorkbook.PICTURE_TYPE_JPEG);
         assertEquals(1, pictures.size());
         assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData()));
+        assertArrayEquals(jpegData, pictures.get(jpegIdx).getData());
 
         int wmfIdx = wb.addPicture(wmfData, XSSFWorkbook.PICTURE_TYPE_WMF);
         assertEquals(2, pictures.size());
         assertEquals("wmf", pictures.get(wmfIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(wmfData, pictures.get(wmfIdx).getData()));
+        assertArrayEquals(wmfData, pictures.get(wmfIdx).getData());
 
         int pngIdx = wb.addPicture(pngData, XSSFWorkbook.PICTURE_TYPE_PNG);
         assertEquals(3, pictures.size());
         assertEquals("png", pictures.get(pngIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(pngData, pictures.get(pngIdx).getData()));
+        assertArrayEquals(pngData, pictures.get(pngIdx).getData());
 
         //TODO finish usermodel API for XSSFPicture
         XSSFPicture p1 = drawing.createPicture(new XSSFClientAnchor(), jpegIdx);
+        assertNotNull(p1);
         XSSFPicture p2 = drawing.createPicture(new XSSFClientAnchor(), wmfIdx);
+        assertNotNull(p2);
         XSSFPicture p3 = drawing.createPicture(new XSSFClientAnchor(), pngIdx);
+        assertNotNull(p3);
 
         //check that the added pictures are accessible after write
         wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
@@ -93,13 +97,13 @@ public final class TestXSSFPictureData extends TestCase {
         assertEquals(3, pictures2.size());
 
         assertEquals("jpeg", pictures2.get(jpegIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(jpegData, pictures2.get(jpegIdx).getData()));
+        assertArrayEquals(jpegData, pictures2.get(jpegIdx).getData());
 
         assertEquals("wmf", pictures2.get(wmfIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(wmfData, pictures2.get(wmfIdx).getData()));
+        assertArrayEquals(wmfData, pictures2.get(wmfIdx).getData());
 
         assertEquals("png", pictures2.get(pngIdx).suggestFileExtension());
-        assertTrue(Arrays.equals(pngData, pictures2.get(pngIdx).getData()));
+        assertArrayEquals(pngData, pictures2.get(pngIdx).getData());
 
     }
 
@@ -109,10 +113,14 @@ public final class TestXSSFPictureData extends TestCase {
     public void test53568(){
         XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53568.xlsx");
         List<XSSFPictureData> pictures = wb.getAllPictures();
+        assertNotNull(pictures);
+        assertEquals(4, pictures.size());
 
         XSSFSheet sheet1 = wb.getSheetAt(0);
         List<XSSFShape> shapes1 = sheet1.createDrawingPatriarch().getShapes();
-
+        assertNotNull(shapes1);
+        assertEquals(5, shapes1.size());
+        
         for(int i = 0; i < wb.getNumberOfSheets(); i++){
             XSSFSheet sheet = wb.getSheetAt(i);
             XSSFDrawing drawing = sheet.createDrawingPatriarch();
index 0ea3f55811381b99e95d5f91679dc4d214d72762..4f09813b116b33ae46652124f07e84b64c141975 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.List;
 import java.util.zip.CRC32;
@@ -31,6 +32,7 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackagePartName;
 import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
 import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
 import org.apache.poi.ss.usermodel.BaseTestWorkbook;
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -40,6 +42,7 @@ import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -574,4 +577,86 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
            Workbook read = XSSFTestDataSamples.writeOutAndReadBack(workbook);
                assertSheetOrder(read, "Sheet2", "Sheet0", "Sheet1");
        }
+       
+       public void testBug51158() throws IOException {
+        // create a workbook
+        final XSSFWorkbook workbook = new XSSFWorkbook();
+        XSSFSheet sheet = workbook.createSheet("Test Sheet");
+        XSSFRow row = sheet.createRow(2);
+        XSSFCell cell = row.createCell(3);
+        cell.setCellValue("test1");
+
+        //XSSFCreationHelper helper = workbook.getCreationHelper();
+        //cell.setHyperlink(helper.createHyperlink(0));
+
+        XSSFComment comment = sheet.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
+        assertNotNull(comment);
+        comment.setString("some comment");
+
+//        CellStyle cs = workbook.createCellStyle();
+//        cs.setShrinkToFit(false);
+//        row.createCell(0).setCellStyle(cs);
+
+        // write the first excel file
+        XSSFWorkbook readBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+        assertNotNull(readBack);
+        assertEquals("test1", readBack.getSheetAt(0).getRow(2).getCell(3).getStringCellValue());
+        assertNull(readBack.getSheetAt(0).getRow(2).getCell(4));
+
+        // add a new cell to the sheet
+        cell = row.createCell(4);
+        cell.setCellValue("test2");
+
+        // write the second excel file
+        readBack = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+        assertNotNull(readBack);
+        assertEquals("test1", readBack.getSheetAt(0).getRow(2).getCell(3).getStringCellValue());
+        assertEquals("test2", readBack.getSheetAt(0).getRow(2).getCell(4).getStringCellValue());
+       }
+       
+       public void testBug51158a() throws IOException {
+        // create a workbook
+        final XSSFWorkbook workbook = new XSSFWorkbook();
+        workbook.createSheet("Test Sheet");
+
+        XSSFSheet sheetBack = workbook.getSheetAt(0);
+
+        // committing twice did add the XML twice without clearing the part in between
+        sheetBack.commit();
+
+        // ensure that a memory based package part does not have lingering data from previous commit() calls
+        if(sheetBack.getPackagePart() instanceof MemoryPackagePart) {
+            ((MemoryPackagePart)sheetBack.getPackagePart()).clear();
+        }
+
+        sheetBack.commit();
+
+        String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream()));
+        System.out.println(str);
+        
+        assertEquals(1, countMatches(str, "<worksheet"));
+    }  
+       
+    private static final int INDEX_NOT_FOUND = -1;
+    
+    private static boolean isEmpty(CharSequence cs) {
+        return cs == null || cs.length() == 0;
+    }
+
+    private static int countMatches(CharSequence str, CharSequence sub) {
+        if (isEmpty(str) || isEmpty(sub)) {
+            return 0;
+        }
+        int count = 0;
+        int idx = 0;
+        while ((idx = indexOf(str, sub, idx)) != INDEX_NOT_FOUND) {
+            count++;
+            idx += sub.length();
+        }
+        return count;
+    }
+    
+    private static int indexOf(CharSequence cs, CharSequence searchChar, int start) {
+        return cs.toString().indexOf(searchChar.toString(), start);
+    }
 }
index 0cf9db5acdf8a4c258b3f0088ce89c8e9ed85d97..f604683bcfd9d76600f740fceb7d5b8102abddb7 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xwpf.usermodel;
 
+import static org.junit.Assert.assertArrayEquals;
+
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
@@ -58,6 +60,13 @@ public class TestXWPFPictureData extends TestCase {
     public void testPictureInHeader() throws IOException
     {
         XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
+        verifyOneHeaderPicture(sampleDoc);
+        
+        XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
+        verifyOneHeaderPicture(readBack);
+    }
+
+    private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
         XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
 
         XWPFHeader header = policy.getDefaultHeader();
@@ -70,8 +79,11 @@ public class TestXWPFPictureData extends TestCase {
     {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
         byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
+        assertNotNull(jpegData);
         byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
+        assertNotNull(gifData);
         byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
+        assertNotNull(pngData);
 
         List<XWPFPictureData> pictures = doc.getAllPictures();
         assertEquals(0,pictures.size());
@@ -113,20 +125,16 @@ public class TestXWPFPictureData extends TestCase {
         assertEquals("/word/media/image1.jpeg",jpegRel.getTargetURI().getPath());
 
         XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
-        byte[] newJPEGData = pictureDataByID.getData();
-        assertEquals(newJPEGData.length,jpegData.length);
-        for (int i = 0 ; i < newJPEGData.length ; i++)
-        {
-            assertEquals(newJPEGData[i],jpegData[i]);
-        }
+        assertArrayEquals(jpegData, pictureDataByID.getData());
 
         // Save an re-load, check it appears
         doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
         assertEquals(1,doc.getAllPictures().size());
         assertEquals(1,doc.getAllPackagePictures().size());
-    }
-    
-    public void testGetChecksum() {
+
+        // verify the picture that we read back in
+        pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
+        assertArrayEquals(jpegData, pictureDataByID.getData());
         
     }
 
@@ -147,8 +155,4 @@ public class TestXWPFPictureData extends TestCase {
         }
 
     }
-
-    private void process(XWPFParagraph paragraph){
-
-    }
 }