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 2014/01/19 14:42:40 UTC

svn commit: r1559497 - /commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java

Author: bodewig
Date: Sun Jan 19 13:42:39 2014
New Revision: 1559497

URL: http://svn.apache.org/r1559497
Log:
some more tests to provide more coverage of the abstract ArchiveFormat - tests are in need of some DRYing up, I know

Modified:
    commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java

Modified: commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java?rev=1559497&r1=1559496&r2=1559497&view=diff
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java (original)
+++ commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java Sun Jan 19 13:42:39 2014
@@ -30,10 +30,13 @@ import java.nio.channels.WritableByteCha
 import java.util.Locale;
 
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
 import org.apache.commons.compress2.archivers.ArchiveEntryParameters;
+import org.apache.commons.compress2.archivers.ArchiveInput;
+import org.apache.commons.compress2.archivers.ArchiveOutput;
 
 public class RoundTripTest {
 
@@ -50,8 +53,8 @@ public class RoundTripTest {
     }
 
     @Test
-    public void testArUnarchive() throws Exception {
-        final File output = new File(dir, "bla.ar");
+    public void testRoundtripUsingConstructors() throws Exception {
+        final File output = new File(dir, "constructors.ar");
         {
             final File file1 = getFile("test1.xml");
             final File file2 = getFile("test2.xml");
@@ -73,18 +76,100 @@ public class RoundTripTest {
         final File input = output;
         final ReadableByteChannel is = new FileInputStream(input).getChannel();
         final ArArchiveInput in = new ArArchiveInput(is);
-        final ArArchiveEntry entry = in.next();
+        ArArchiveEntry entry = in.next();
+        Assert.assertEquals("test1.xml", entry.getName());
 
         File target = new File(dir, entry.getName());
         final WritableByteChannel out = new FileOutputStream(target).getChannel();
 
         IOUtils.copy(in.getChannel(), out);
+        out.close();
+
+        entry = in.next();
+        Assert.assertEquals("test2.xml", entry.getName());
+
+        in.close();
+        is.close();
+    }
+
+    @Test
+    public void testRoundtripUsingFormatInstanceAndChannels() throws Exception {
+        ArArchiveFormat format = new ArArchiveFormat();
+        final File output = new File(dir, "format-channels.ar");
+        {
+            final File file1 = getFile("test1.xml");
+            final File file2 = getFile("test2.xml");
+
+            final WritableByteChannel out = new FileOutputStream(output).getChannel();
+            final ArchiveOutput<ArArchiveEntry> os = format.writeTo(out, null);
+            IOUtils.copy(new FileInputStream(file1).getChannel(),
+                         os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file1))));
+            os.closeEntry();
 
+            IOUtils.copy(new FileInputStream(file2).getChannel(),
+                         os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file2))));
+            os.closeEntry();
+            os.close();
+            out.close();
+        }
+
+        // UnArArchive Operation
+        final File input = output;
+        final ReadableByteChannel is = new FileInputStream(input).getChannel();
+        final ArchiveInput<ArArchiveEntry> in = format.readFrom(is, null);
+        ArArchiveEntry entry = in.next();
+        Assert.assertEquals("test1.xml", entry.getName());
+
+        File target = new File(dir, entry.getName());
+        final WritableByteChannel out = new FileOutputStream(target).getChannel();
+
+        IOUtils.copy(in.getChannel(), out);
         out.close();
+
+        entry = in.next();
+        Assert.assertEquals("test2.xml", entry.getName());
+
         in.close();
         is.close();
     }
 
+    @Test
+    public void testRoundtripUsingFormatInstanceAndFiles() throws Exception {
+        ArArchiveFormat format = new ArArchiveFormat();
+        final File output = new File(dir, "format-files.ar");
+        {
+            final File file1 = getFile("test1.xml");
+            final File file2 = getFile("test2.xml");
+
+            final ArchiveOutput<ArArchiveEntry> os = format.writeTo(output, null);
+            IOUtils.copy(new FileInputStream(file1).getChannel(),
+                         os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file1))));
+            os.closeEntry();
+
+            IOUtils.copy(new FileInputStream(file2).getChannel(),
+                         os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file2))));
+            os.closeEntry();
+            os.close();
+        }
+
+        // UnArArchive Operation
+        final File input = output;
+        final ArchiveInput<ArArchiveEntry> in = format.readFrom(input, null);
+        ArArchiveEntry entry = in.next();
+        Assert.assertEquals("test1.xml", entry.getName());
+
+        File target = new File(dir, entry.getName());
+        final WritableByteChannel out = new FileOutputStream(target).getChannel();
+
+        IOUtils.copy(in.getChannel(), out);
+        out.close();
+
+        entry = in.next();
+        Assert.assertEquals("test2.xml", entry.getName());
+
+        in.close();
+    }
+
     public static File mkdir(String name) throws IOException {
         File f = File.createTempFile(name, "");
         f.delete();