From 66f04045144ee621b3a30f4a8c40dc05adf84008 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 4 Sep 2010 06:33:22 +0000 Subject: [PATCH] added ZipFileAssert, this file was in the original OpenXML4J package but wasn't included in POI-3.5 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@992551 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/openxml4j/opc/TestPackage.java | 70 ++++---- .../poi/openxml4j/opc/ZipFileAssert.java | 161 ++++++++++++++++++ .../openxml4j/TestCreatePackageOUTPUT.docx | Bin 0 -> 1412 bytes .../openxml4j/TestOpenPackageOUTPUT.docx | Bin 0 -> 1983 bytes 4 files changed, 196 insertions(+), 35 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java create mode 100644 test-data/openxml4j/TestCreatePackageOUTPUT.docx create mode 100644 test-data/openxml4j/TestOpenPackageOUTPUT.docx diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index 1c41db0d1b..287bcdae67 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -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"); + File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx"); - // Zap the target file, in case of an earlier run - if(targetFile.exists()) targetFile.delete(); + // 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"); + // Create a package + OPCPackage pkg = OPCPackage.create(targetFile); + PackagePartName corePartName = PackagingURIHelper + .createPartName("/word/document.xml"); - pkg.addRelationship(corePartName, TargetMode.INTERNAL, - PackageRelationshipTypes.CORE_DOCUMENT, "rId1"); + pkg.addRelationship(corePartName, TargetMode.INTERNAL, + PackageRelationshipTypes.CORE_DOCUMENT, "rId1"); - PackagePart corePart = pkg - .createPart( - corePartName, - "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"); + 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 !"); + 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(); + 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 index 0000000000..c59be45256 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java @@ -0,0 +1,161 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.openxml4j.opc; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeMap; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import junit.framework.Assert; +import junit.framework.AssertionFailedError; + +/** + * Compare the contents of 2 zip files. + * + * @author CDubettier + */ +public class ZipFileAssert { + private ZipFileAssert() { + } + + static final int BUFFER_SIZE = 2048; + + protected static boolean equals( + TreeMap file1, + TreeMap file2) { + Set listFile1 = file1.keySet(); + if (listFile1.size() == file2.keySet().size()) { + for (Iterator iter = listFile1.iterator(); iter.hasNext();) { + String fileName = (String) iter.next(); + // extract the contents for both + ByteArrayOutputStream contain2 = file2.get(fileName); + ByteArrayOutputStream contain1 = file1.get(fileName); + + if (contain2 == null) { + // file not found in archive 2 + Assert.fail(fileName + " not found in 2nd zip"); + return false; + } + // no need to check for contain1. The key come from it + + if ((fileName.endsWith(".xml")) || fileName.endsWith(".rels")) { + // we have a xml file + // TODO + // YK: the original OpenXML4J version attempted to compare xml using xmlunit (http://xmlunit.sourceforge.net), + // but POI does not depend on this library + } else { + // not xml, may be an image or other binary format + if (contain2.size() != contain1.size()) { + // not the same size + Assert.fail(fileName + + " does not have the same size in both zip:" + + contain2.size() + "!=" + contain1.size()); + return false; + } + byte array1[] = contain1.toByteArray(); + byte array2[] = contain2.toByteArray(); + for (int i = 0; i < array1.length; i++) { + if (array1[i] != array2[i]) { + Assert.fail(fileName + " differ at index:" + i); + return false; + } + } + } + } + } else { + // not the same number of files -> cannot be equals + Assert.fail("not the same number of files in zip:" + + listFile1.size() + "!=" + file2.keySet().size()); + return false; + } + return true; + } + + protected static TreeMap decompress( + File filename) throws IOException { + // store the zip content in memory + // let s assume it is not Go ;-) + TreeMap zipContent = new TreeMap(); + + byte data[] = new byte[BUFFER_SIZE]; + /* Open file to decompress */ + FileInputStream file_decompress = new FileInputStream(filename); + + /* Create a buffer for the decompressed files */ + BufferedInputStream buffi = new BufferedInputStream(file_decompress); + + /* Open the file with the buffer */ + ZipInputStream zis = new ZipInputStream(buffi); + + /* Processing entries of the zip file */ + ZipEntry entree; + int count; + while ((entree = zis.getNextEntry()) != null) { + + /* Create a array for the current entry */ + ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); + zipContent.put(entree.getName(), byteArray); + + /* copy in memory */ + while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { + byteArray.write(data, 0, count); + } + /* Flush the buffer */ + byteArray.flush(); + byteArray.close(); + } + + zis.close(); + + return zipContent; + } + + /** + * Asserts that two files are equal. Throws an AssertionFailedError + * if they are not. + *

+ * + */ + public static void assertEquals(File expected, File actual) { + Assert.assertNotNull(expected); + Assert.assertNotNull(actual); + + Assert.assertTrue("File does not exist [" + expected.getAbsolutePath() + + "]", expected.exists()); + Assert.assertTrue("File does not exist [" + actual.getAbsolutePath() + + "]", actual.exists()); + + Assert.assertTrue("Expected file not readable", expected.canRead()); + Assert.assertTrue("Actual file not readable", actual.canRead()); + + try { + TreeMap file1 = decompress(expected); + TreeMap file2 = decompress(actual); + equals(file1, file2); + } catch (IOException e) { + throw new AssertionFailedError(e.toString()); + } + } +} diff --git a/test-data/openxml4j/TestCreatePackageOUTPUT.docx b/test-data/openxml4j/TestCreatePackageOUTPUT.docx new file mode 100644 index 0000000000000000000000000000000000000000..dfcacc55e2e92de7ccb436018f9362eb12fcd2cb GIT binary patch literal 1412 zcmWIWW@Zs#-~htRSu(Z^NI($CPRUOWD9SG=)=$naO4X~#%~?Cq*ZYWpi0k*iqKd!G zJl&VJZQC5+Cg6IIuWR=VJL{v*npJMitj}L&940bdPyYLz!>XFoqfHm^7wj|Nbx3Ei zMB}t&Z%e};mTvaq-m_-%k}1ay?ll&Rd2w*@jZ6Cu%@)v6KXRH^K2grM2aURWei?8AV<^;5;qyGmY8e z>3x5fQEuQPmuH?|YsJVS^D_6a0 ze|PukwAR;ijAmU@I@{egx2Wt~(Q@fFdq+&OIbcBroSYkArBhe_YPOU0YsH!k{P{M3KZBwl|NDZftH zUF(aYH#V5JhJEM0drzFXv(Sy_){!kIVs=N`OqE~0YV+EspJx6jd-d_v7w2L|P&m9% zczWnO(3hKmVSzgwgn=reo%8cbQu9jULn;eWi(^4aYU)I9zrzLsZQm_hpXzqa-Q>Wb z`m*Ve&@K)34Rbt`9!|H()7+kZ$H}oHEGhGEfBvz{df(4&l2tJ|SYD$jb?(4x0}1=m zKdGN1?te;+4}JU0mdVJd`pu6y#d{_TedY06$RTK>ne5@0TD8p6ZE5^fmQTAf^Oo@0 zHJ>lllijmkB0Jn*)!MUf68%q2oO?hi{kwbq!P1zc8NFdMOLl%ftRL`~Q*eq+MS+>% z`V$A9T6zecH{Q4S{1?@eJO{36Jym~tLZf}bbxoHgwL%@IeZ`mQzO)JwT$1zP`JQ^m z`%Y3^m9INK*Jnt$FMc>hKey#tg9m$A*c_4NJ^!VaPHoTrew7^*dIERVZ2khnMh}>n?@*PqTaVww4bW`{0!5Pbz&Mv&LBjZsN<88GAstsZH z;})#E{qx=Vo!j&5qaJQAsbjf3!8cGyhnL-O);`7UuM_vG>pHKPr^WbZ=~7OMJc)Q; zUP-47qD$xT2bJ$*(Dr>}d+?>+kr!1~jl$O$r{8m57*-NJ&3e`Divn#1eb?AK@(;

-;0a8TNO=!kBXZD#k|zQL0wV)UVnsIq*;A+v Y?`OsmUjg2%Y#=*Wfba*9E@J`l0J{wuyZ`_I literal 0 HcmV?d00001 diff --git a/test-data/openxml4j/TestOpenPackageOUTPUT.docx b/test-data/openxml4j/TestOpenPackageOUTPUT.docx new file mode 100644 index 0000000000000000000000000000000000000000..1e0996fd4172a3e4b36a6da3f5f2ca558884f6a6 GIT binary patch literal 1983 zcmWIWW@Zs#-~d9OIWo2kNPrv2jxS2hDc08mk!vTN_hL2_Xn+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