Browse files

added round-trip tests through unlzma(1)

  • Loading branch information...
1 parent 60fa2f7 commit 741a607b9b8451d1134806d6b25292013524dbe4 @league committed Sep 5, 2009
Showing with 45 additions and 0 deletions.
  1. +45 −0 net/contrapunctus/lzma/RoundTripTest.java
View
45 net/contrapunctus/lzma/RoundTripTest.java
@@ -15,6 +15,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;
+import static org.junit.Assert.*;
@RunWith(Parameterized.class)
public class RoundTripTest
@@ -70,6 +71,7 @@ public RoundTripTest(File file, boolean header) throws IOException
assert len < Integer.MAX_VALUE; // huge files will fail, because
original = new byte[(int)len]; // we read whole thing into mem
f.readFully(original);
+ f.close();
}
else
{
@@ -111,6 +113,49 @@ public String toString()
Assert.assertTrue(Arrays.equals(original, expanded));
}
+ @Test public void withLzmaCommand()
+ throws IOException, InterruptedException
+ {
+ // header is required for compatibility with lzma(1)
+ if(!header) return;
+ LzmaOutputStream.LZMA_HEADER = header;
+ System.out.printf("%s: ", this);
+
+ // write compressed data to temp file
+ File lzfile = File.createTempFile("roundtrip", ".lzma");
+ FileOutputStream fos = new FileOutputStream(lzfile);
+ LzmaOutputStream los = new LzmaOutputStream(fos);
+ los.write(original);
+ los.close();
+
+ // ask lzma(1) to decompress it
+ System.out.printf("unlzma %s\n", lzfile.getName());
+ Runtime rt = Runtime.getRuntime();
+ Process p = rt.exec("unlzma " + lzfile);
+ int r = p.waitFor();
+ assertEquals(r, 0);
+
+ // chop off .lzma extension
+ String path = lzfile.getPath();
+ assertTrue(path.endsWith(".lzma"));
+ int k = path.lastIndexOf('.');
+ File plain = new File(path.substring(0, k));
+ assertTrue(plain.exists());
+
+ // read contents and verify
+ RandomAccessFile raf = new RandomAccessFile(plain, "r");
+ long len = raf.length();
+ assert len < Integer.MAX_VALUE;
+ byte[] copy = new byte[(int)len];
+ raf.readFully(copy);
+ raf.close();
+ assertTrue(Arrays.equals(original, copy));
+
+ // clean up
+ lzfile.delete();
+ plain.delete();
+ }
+
public static void main( String[] args ) throws IOException
{
if(0 == args.length)

0 comments on commit 741a607

Please sign in to comment.