poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r992551 - in /poi/trunk: src/ooxml/testcases/org/apache/poi/openxml4j/opc/ test-data/openxml4j/
Date Sat, 04 Sep 2010 06:33:23 GMT
Author: yegor
Date: Sat Sep  4 06:33:22 2010
New Revision: 992551

URL: http://svn.apache.org/viewvc?rev=992551&view=rev
Log:
added ZipFileAssert, this file was in the original OpenXML4J package but wasn't included in
POI-3.5

Added:
    poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java
    poi/trunk/test-data/openxml4j/TestCreatePackageOUTPUT.docx   (with props)
    poi/trunk/test-data/openxml4j/TestOpenPackageOUTPUT.docx   (with props)
Modified:
    poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java?rev=992551&r1=992550&r2=992551&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java Sat Sep  4
06:33:22 2010
@@ -60,7 +60,7 @@ public final class TestPackage extends T
 
 		// 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 T
 	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();
-
-		// Create a package
-		OPCPackage pkg = OPCPackage.create(targetFile);
-		PackagePartName corePartName = PackagingURIHelper
-				.createPartName("/word/document.xml");
+        // Zap the target file, in case of an earlier run
+        if(targetFile.exists()) targetFile.delete();
 
-		pkg.addRelationship(corePartName, TargetMode.INTERNAL,
-				PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
+        // 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 !");
 
-		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();
+        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 T
 
 		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 T
 			fail();
 		}
 
-		//ZipFileAssert.assertEquals(expectedFile, targetFile);
+		ZipFileAssert.assertEquals(expectedFile, targetFile);
 		assertTrue(targetFile.delete());
 	}
 
@@ -306,7 +306,7 @@ public final class TestPackage extends T
 
 		// 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 T
 
 		// Compare the original and newly saved document
 		assertTrue(targetFile.exists());
-		//ZipFileAssert.assertEquals(targetFile, tempFile);
+		ZipFileAssert.assertEquals(targetFile, tempFile);
 		assertTrue(targetFile.delete());
 	}
 

Added: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java?rev=992551&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/ZipFileAssert.java Sat Sep
 4 06:33:22 2010
@@ -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<String, ByteArrayOutputStream> file1,
+			TreeMap<String, ByteArrayOutputStream> 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<String, ByteArrayOutputStream> decompress(
+			File filename) throws IOException {
+		// store the zip content in memory
+		// let s assume it is not Go ;-)
+		TreeMap<String, ByteArrayOutputStream> zipContent = new TreeMap<String, ByteArrayOutputStream>();
+
+		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 <tt>AssertionFailedError</tt>
+	 * if they are not.
+	 * <p>
+	 * 
+	 */
+	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<String, ByteArrayOutputStream> file1 = decompress(expected);
+			TreeMap<String, ByteArrayOutputStream> file2 = decompress(actual);
+			equals(file1, file2);
+		} catch (IOException e) {
+			throw new AssertionFailedError(e.toString());
+		}
+	}
+}

Added: poi/trunk/test-data/openxml4j/TestCreatePackageOUTPUT.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/openxml4j/TestCreatePackageOUTPUT.docx?rev=992551&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/openxml4j/TestCreatePackageOUTPUT.docx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: poi/trunk/test-data/openxml4j/TestOpenPackageOUTPUT.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/openxml4j/TestOpenPackageOUTPUT.docx?rev=992551&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/openxml4j/TestOpenPackageOUTPUT.docx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message