Improve cpio tests, submitted by Christian Grobmeier, COMPRESS-28
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@758588 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java b/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
index 1f724e1..aa0f68c 100644
--- a/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
@@ -23,6 +23,8 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
@@ -30,56 +32,73 @@
public final class CpioTestCase extends AbstractTestCase {
- public void testCpioArchiveCreation() throws Exception {
- final File output = new File(dir, "bla.cpio");
-
- final File file1 = getFile("test1.xml");
- final File file2 = getFile("test2.xml");
-
- final OutputStream out = new FileOutputStream(output);
- final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
- os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length()));
- IOUtils.copy(new FileInputStream(file1), os);
- os.closeArchiveEntry();
-
- os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
- IOUtils.copy(new FileInputStream(file2), os);
- os.closeArchiveEntry();
-
- os.close();
- }
+ public void testCpioArchiveCreation() throws Exception {
+ final File output = new File(dir, "bla.cpio");
- public void testCpioUnarchive() throws Exception {
- final File output = new File(dir, "bla.cpio");
- {
- final File file1 = getFile("test1.xml");
- final File file2 = getFile("test2.xml");
-
- final OutputStream out = new FileOutputStream(output);
- final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
- os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length()));
- IOUtils.copy(new FileInputStream(file1), os);
- os.closeArchiveEntry();
-
- os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
- IOUtils.copy(new FileInputStream(file2), os);
- os.closeArchiveEntry();
- os.close();
- }
-
- // Unarchive Operation
- final File input = output;
- final InputStream is = new FileInputStream(input);
- final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("cpio", is);
- final CpioArchiveEntry entry = (CpioArchiveEntry)in.getNextEntry();
-
- File target = new File(dir, entry.getName());
- final OutputStream out = new FileOutputStream(target);
-
- IOUtils.copy(in, out);
-
- out.close();
+ final File file1 = getFile("test1.xml");
+ final File file2 = getFile("test2.xml");
+
+ final OutputStream out = new FileOutputStream(output);
+ final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
+ os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length()));
+ IOUtils.copy(new FileInputStream(file1), os);
+ os.closeArchiveEntry();
+
+ os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
+ IOUtils.copy(new FileInputStream(file2), os);
+ os.closeArchiveEntry();
+
+ os.close();
+ }
+
+ public void testCpioUnarchive() throws Exception {
+ final File output = new File(dir, "bla.cpio");
+ {
+ final File file1 = getFile("test1.xml");
+ final File file2 = getFile("test2.xml");
+
+ final OutputStream out = new FileOutputStream(output);
+ final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
+ os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length()));
+ IOUtils.copy(new FileInputStream(file1), os);
+ os.closeArchiveEntry();
+
+ os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
+ IOUtils.copy(new FileInputStream(file2), os);
+ os.closeArchiveEntry();
+
+ os.close();
+ out.close();
+ }
+
+ // Unarchive Operation
+ final File input = output;
+ final InputStream is = new FileInputStream(input);
+ final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("cpio", is);
+
+
+ Map result = new HashMap();
+ ArchiveEntry entry = null;
+ while ((entry = in.getNextEntry()) != null) {
+ File target = new File(dir, entry.getName());
+ final OutputStream out = new FileOutputStream(target);
+ IOUtils.copy(in, out);
+ out.close();
+ result.put(entry.getName(), target);
+ }
in.close();
- }
+
+ int lineSepLength = System.getProperty("line.separator").length();
+
+ File t = (File)result.get("test1.xml");
+ assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists());
+ assertEquals("length of " + t.getAbsolutePath(),
+ 72 + 4 * lineSepLength, t.length());
+
+ t = (File)result.get("test2.xml");
+ assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists());
+ assertEquals("length of " + t.getAbsolutePath(),
+ 73 + 5 * lineSepLength, t.length());
+ }
}