// Compare the original and newly saved document
assertTrue(targetFile.exists());
- //ZipFileAssert.assertEquals(originalFile, targetFile);
+ ZipFileAssert.assertEquals(new File(originalFile), targetFile);
assertTrue(targetFile.delete());
}
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());
}
/**
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);
fail();
}
- //ZipFileAssert.assertEquals(expectedFile, targetFile);
+ ZipFileAssert.assertEquals(expectedFile, targetFile);
assertTrue(targetFile.delete());
}
// Compare the original and newly saved document
assertTrue(targetFile.exists());
- //ZipFileAssert.assertEquals(originalFile, targetFile);
+ ZipFileAssert.assertEquals(new File(originalFile), targetFile);
assertTrue(targetFile.delete());
}
// Compare the original and newly saved document
assertTrue(targetFile.exists());
- //ZipFileAssert.assertEquals(targetFile, tempFile);
+ ZipFileAssert.assertEquals(targetFile, tempFile);
assertTrue(targetFile.delete());
}
--- /dev/null
+/* ====================================================================\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