]> source.dussan.org Git - poi.git/commitdiff
added ZipFileAssert, this file was in the original OpenXML4J package but wasn't inclu...
authorYegor Kozlov <yegor@apache.org>
Sat, 4 Sep 2010 06:33:22 +0000 (06:33 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 4 Sep 2010 06:33:22 +0000 (06:33 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@992551 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java [new file with mode: 0644]
test-data/openxml4j/TestCreatePackageOUTPUT.docx [new file with mode: 0644]
test-data/openxml4j/TestOpenPackageOUTPUT.docx [new file with mode: 0644]

index 1c41db0d1bc6285090644a1ac1f3aa652f92ce78..287bcdae67a7b82ed970752a3cfdbc8502e84db1 100644 (file)
@@ -60,7 +60,7 @@ public final class TestPackage extends TestCase {
 
                // Compare the original and newly saved document
                assertTrue(targetFile.exists());
-               //ZipFileAssert.assertEquals(originalFile, targetFile);
+               ZipFileAssert.assertEquals(new File(originalFile), targetFile);
                assertTrue(targetFile.delete());
        }
 
@@ -103,43 +103,43 @@ public final class TestPackage extends TestCase {
        public void testCreatePackageAddPart() throws Exception {
                File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx");
 
-               File expectedFileFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageOUTPUT.docx");
-
-               // Zap the target file, in case of an earlier run
-               if(targetFile.exists()) targetFile.delete();
-
-               // Create a package
-               OPCPackage pkg = OPCPackage.create(targetFile);
-               PackagePartName corePartName = PackagingURIHelper
-                               .createPartName("/word/document.xml");
-
-               pkg.addRelationship(corePartName, TargetMode.INTERNAL,
-                               PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
-
-               PackagePart corePart = pkg
-                               .createPart(
-                                               corePartName,
-                                               "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
-
-               Document doc = DocumentHelper.createDocument();
-               Namespace nsWordprocessinML = new Namespace("w",
-                               "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
-               Element elDocument = doc.addElement(new QName("document",
-                               nsWordprocessinML));
-               Element elBody = elDocument.addElement(new QName("body",
-                               nsWordprocessinML));
-               Element elParagraph = elBody.addElement(new QName("p",
-                               nsWordprocessinML));
-               Element elRun = elParagraph
-                               .addElement(new QName("r", nsWordprocessinML));
-               Element elText = elRun.addElement(new QName("t", nsWordprocessinML));
-               elText.setText("Hello Open XML !");
-
-               StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());
-               pkg.close();
+               File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx");
+
+        // Zap the target file, in case of an earlier run
+        if(targetFile.exists()) targetFile.delete();
+
+        // Create a package
+        OPCPackage pkg = OPCPackage.create(targetFile);
+        PackagePartName corePartName = PackagingURIHelper
+                .createPartName("/word/document.xml");
+
+        pkg.addRelationship(corePartName, TargetMode.INTERNAL,
+                PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
+
+        PackagePart corePart = pkg
+                .createPart(
+                        corePartName,
+                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
+
+        Document doc = DocumentHelper.createDocument();
+        Namespace nsWordprocessinML = new Namespace("w",
+                "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
+        Element elDocument = doc.addElement(new QName("document",
+                nsWordprocessinML));
+        Element elBody = elDocument.addElement(new QName("body",
+                nsWordprocessinML));
+        Element elParagraph = elBody.addElement(new QName("p",
+                nsWordprocessinML));
+        Element elRun = elParagraph
+                .addElement(new QName("r", nsWordprocessinML));
+        Element elText = elRun.addElement(new QName("t", nsWordprocessinML));
+        elText.setText("Hello Open XML !");
+
+        StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());
+        pkg.close();
 
-               //ZipFileAssert.assertEquals(expectedFile, targetFile);
-               assertTrue(targetFile.delete());
+        ZipFileAssert.assertEquals(expectedFile, targetFile);
+        assertTrue(targetFile.delete());
        }
 
        /**
@@ -240,7 +240,7 @@ public final class TestPackage extends TestCase {
 
                File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx");
 
-               File expectedFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageOUTPUT.docx");
+               File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageOUTPUT.docx");
 
                // Copy the input file in the output directory
                FileHelper.copyFile(inputFile, targetFile);
@@ -286,7 +286,7 @@ public final class TestPackage extends TestCase {
                        fail();
                }
 
-               //ZipFileAssert.assertEquals(expectedFile, targetFile);
+               ZipFileAssert.assertEquals(expectedFile, targetFile);
                assertTrue(targetFile.delete());
        }
 
@@ -306,7 +306,7 @@ public final class TestPackage extends TestCase {
 
                // Compare the original and newly saved document
                assertTrue(targetFile.exists());
-               //ZipFileAssert.assertEquals(originalFile, targetFile);
+               ZipFileAssert.assertEquals(new File(originalFile), targetFile);
                assertTrue(targetFile.delete());
        }
 
@@ -346,7 +346,7 @@ public final class TestPackage extends TestCase {
 
                // Compare the original and newly saved document
                assertTrue(targetFile.exists());
-               //ZipFileAssert.assertEquals(targetFile, tempFile);
+               ZipFileAssert.assertEquals(targetFile, tempFile);
                assertTrue(targetFile.delete());
        }
 
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java
new file mode 100644 (file)
index 0000000..c59be45
--- /dev/null
@@ -0,0 +1,161 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.openxml4j.opc;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.util.Iterator;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipInputStream;\r
+\r
+import junit.framework.Assert;\r
+import junit.framework.AssertionFailedError;\r
+\r
+/**\r
+ * Compare the contents of 2 zip files.\r
+ * \r
+ * @author CDubettier\r
+ */\r
+public class ZipFileAssert {\r
+       private ZipFileAssert() {\r
+       }\r
+\r
+       static final int BUFFER_SIZE = 2048;\r
+\r
+       protected static boolean equals(\r
+                       TreeMap<String, ByteArrayOutputStream> file1,\r
+                       TreeMap<String, ByteArrayOutputStream> file2) {\r
+               Set listFile1 = file1.keySet();\r
+               if (listFile1.size() == file2.keySet().size()) {\r
+                       for (Iterator iter = listFile1.iterator(); iter.hasNext();) {\r
+                               String fileName = (String) iter.next();\r
+                               // extract the contents for both\r
+                               ByteArrayOutputStream contain2 = file2.get(fileName);\r
+                               ByteArrayOutputStream contain1 = file1.get(fileName);\r
+\r
+                               if (contain2 == null) {\r
+                                       // file not found in archive 2\r
+                                       Assert.fail(fileName + " not found in 2nd zip");\r
+                                       return false;\r
+                               }\r
+                               // no need to check for contain1. The key come from it\r
+\r
+                               if ((fileName.endsWith(".xml")) || fileName.endsWith(".rels")) {\r
+                                       // we have a xml file\r
+                    // TODO\r
+                    // YK: the original OpenXML4J version attempted to compare xml using xmlunit (http://xmlunit.sourceforge.net),\r
+                    // but POI does not depend on this library\r
+                } else {\r
+                                       // not xml, may be an image or other binary format\r
+                                       if (contain2.size() != contain1.size()) {\r
+                                               // not the same size\r
+                                               Assert.fail(fileName\r
+                                                               + " does not have the same size in both zip:"\r
+                                                               + contain2.size() + "!=" + contain1.size());\r
+                                               return false;\r
+                                       }\r
+                                       byte array1[] = contain1.toByteArray();\r
+                                       byte array2[] = contain2.toByteArray();\r
+                                       for (int i = 0; i < array1.length; i++) {\r
+                                               if (array1[i] != array2[i]) {\r
+                                                       Assert.fail(fileName + " differ at index:" + i);\r
+                                                       return false;\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+               } else {\r
+                       // not the same number of files -> cannot be equals\r
+                       Assert.fail("not the same number of files in zip:"\r
+                                       + listFile1.size() + "!=" + file2.keySet().size());\r
+                       return false;\r
+               }\r
+               return true;\r
+       }\r
+\r
+       protected static TreeMap<String, ByteArrayOutputStream> decompress(\r
+                       File filename) throws IOException {\r
+               // store the zip content in memory\r
+               // let s assume it is not Go ;-)\r
+               TreeMap<String, ByteArrayOutputStream> zipContent = new TreeMap<String, ByteArrayOutputStream>();\r
+\r
+               byte data[] = new byte[BUFFER_SIZE];\r
+               /* Open file to decompress */\r
+               FileInputStream file_decompress = new FileInputStream(filename);\r
+\r
+               /* Create a buffer for the decompressed files */\r
+               BufferedInputStream buffi = new BufferedInputStream(file_decompress);\r
+\r
+               /* Open the file with the buffer */\r
+               ZipInputStream zis = new ZipInputStream(buffi);\r
+\r
+               /* Processing entries of the zip file */\r
+               ZipEntry entree;\r
+               int count;\r
+               while ((entree = zis.getNextEntry()) != null) {\r
+\r
+                       /* Create a array for the current entry */\r
+                       ByteArrayOutputStream byteArray = new ByteArrayOutputStream();\r
+                       zipContent.put(entree.getName(), byteArray);\r
+\r
+                       /* copy in memory */\r
+                       while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {\r
+                               byteArray.write(data, 0, count);\r
+                       }\r
+                       /* Flush the buffer */\r
+                       byteArray.flush();\r
+                       byteArray.close();\r
+               }\r
+\r
+               zis.close();\r
+\r
+               return zipContent;\r
+       }\r
+\r
+       /**\r
+        * Asserts that two files are equal. Throws an <tt>AssertionFailedError</tt>\r
+        * if they are not.\r
+        * <p>\r
+        * \r
+        */\r
+       public static void assertEquals(File expected, File actual) {\r
+               Assert.assertNotNull(expected);\r
+               Assert.assertNotNull(actual);\r
+\r
+               Assert.assertTrue("File does not exist [" + expected.getAbsolutePath()\r
+                               + "]", expected.exists());\r
+               Assert.assertTrue("File does not exist [" + actual.getAbsolutePath()\r
+                               + "]", actual.exists());\r
+\r
+               Assert.assertTrue("Expected file not readable", expected.canRead());\r
+               Assert.assertTrue("Actual file not readable", actual.canRead());\r
+\r
+               try {\r
+                       TreeMap<String, ByteArrayOutputStream> file1 = decompress(expected);\r
+                       TreeMap<String, ByteArrayOutputStream> file2 = decompress(actual);\r
+                       equals(file1, file2);\r
+               } catch (IOException e) {\r
+                       throw new AssertionFailedError(e.toString());\r
+               }\r
+       }\r
+}\r
diff --git a/test-data/openxml4j/TestCreatePackageOUTPUT.docx b/test-data/openxml4j/TestCreatePackageOUTPUT.docx
new file mode 100644 (file)
index 0000000..dfcacc5
Binary files /dev/null and b/test-data/openxml4j/TestCreatePackageOUTPUT.docx differ
diff --git a/test-data/openxml4j/TestOpenPackageOUTPUT.docx b/test-data/openxml4j/TestOpenPackageOUTPUT.docx
new file mode 100644 (file)
index 0000000..1e0996f
Binary files /dev/null and b/test-data/openxml4j/TestOpenPackageOUTPUT.docx differ