You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2009/03/26 11:37:07 UTC

svn commit: r758588 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/cpio/ test/java/org/apache/commons/compress/archivers/ test/java/org/apache/commons/compress/archivers/cpio/

Author: bodewig
Date: Thu Mar 26 10:36:45 2009
New Revision: 758588

URL: http://svn.apache.org/viewvc?rev=758588&view=rev
Log:
Improve cpio tests, submitted by Christian Grobmeier, COMPRESS-28

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java   (with props)
Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java?rev=758588&r1=758587&r2=758588&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java Thu Mar 26 10:36:45 2009
@@ -39,14 +39,13 @@
  * <p/>
  * <code><pre>
  * CpioArchiveOutputStream out = new CpioArchiveOutputStream(
- *         new FileOutputStream(new File(&quot;test.cpio&quot;)));
+ *         new FileOutputStream(new File("test.cpio")));
  * CpioArchiveEntry entry = new CpioArchiveEntry();
  * entry.setName(&quot;testfile&quot;);
  * String contents = &quot;12345&quot;;
  * entry.setFileSize(contents.length());
  * out.putNextEntry(entry);
  * out.write(testContents.getBytes());
- * out.finish();
  * out.close();
  * </pre></code>
  * <p/>
@@ -150,7 +149,7 @@
     public void putNextEntry(final CpioArchiveEntry e) throws IOException {
         ensureOpen();
         if (this.cpioEntry != null) {
-            closeEntry(); // close previous entry
+            closeArchiveEntry(); // close previous entry
         }
         if (e.getTime() == -1) {
             e.setTime(System.currentTimeMillis());
@@ -242,15 +241,13 @@
         pad(entry.getHeaderSize() + entry.getName().length() + 1, 2);
     }
 
-    /**
-     * Closes the current CPIO entry and positions the stream for writing the
-     * next entry.
+    /*(non-Javadoc)
      * 
-     * @throws IOException
-     *             if an I/O error has occurred or if a CPIO file error has
-     *             occurred
+     * @see
+     * org.apache.commons.compress.archivers.ArchiveOutputStream#closeArchiveEntry
+     * ()
      */
-    public void closeEntry() throws IOException {
+    public void closeArchiveEntry() throws IOException {
         ensureOpen();
 
         if (this.cpioEntry.getSize() != this.written) {
@@ -327,14 +324,14 @@
             return;
         }
         if (this.cpioEntry != null) {
-            closeEntry();
+            closeArchiveEntry();
         }
         this.cpioEntry = new CpioArchiveEntry(this.entryFormat);
         this.cpioEntry.setMode(0);
         this.cpioEntry.setName("TRAILER!!!");
         this.cpioEntry.setNumberOfLinks(1);
         writeHeader(this.cpioEntry);
-        closeEntry();
+        closeArchiveEntry();
     }
 
     /**
@@ -346,6 +343,7 @@
      */
     public void close() throws IOException {
         if (!this.closed) {
+            this.finish();
             super.close();
             this.closed = true;
         }
@@ -398,17 +396,6 @@
      * (non-Javadoc)
      * 
      * @see
-     * org.apache.commons.compress.archivers.ArchiveOutputStream#closeArchiveEntry
-     * ()
-     */
-    public void closeArchiveEntry() throws IOException {
-        this.closeEntry();
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see
      * org.apache.commons.compress.archivers.ArchiveOutputStream#putArchiveEntry
      * (org.apache.commons.compress.archivers.ArchiveEntry)
      */

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java?rev=758588&r1=758587&r2=758588&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java Thu Mar 26 10:36:45 2009
@@ -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);
+    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 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();
+        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());
+    }
 
 }

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java?rev=758588&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java (added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java Thu Mar 26 10:36:45 2009
@@ -0,0 +1,31 @@
+package org.apache.commons.compress.archivers.cpio;
+
+import java.io.FileInputStream;
+
+import org.apache.commons.compress.AbstractTestCase;
+
+public class CpioArchiveInputStreamTest extends AbstractTestCase {
+
+    public void testCpioUnarchive() throws Exception {
+        StringBuffer expected = new StringBuffer();
+        expected.append("./test1.xml<?xml version=\"1.0\"?>\n");
+        expected.append("<empty/>./test2.xml<?xml version=\"1.0\"?>\n");
+        expected.append("<empty/>\n");
+        
+
+        CpioArchiveInputStream in = 
+                new CpioArchiveInputStream(new FileInputStream(getFile("bla.cpio")));
+        CpioArchiveEntry entry= null;
+        
+        StringBuffer result = new StringBuffer();
+        while ((entry = (CpioArchiveEntry) in.getNextEntry()) != null) {
+            result.append(entry.getName());
+            int tmp;
+            while ((tmp = in.read()) != -1) {
+                result.append((char) tmp);
+             }
+         }
+         in.close();
+         assertEquals(result.toString(), expected.toString());
+    }    
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native