Home | History | Annotate | Download | only in dec
      1 /* Copyright 2017 Google Inc. All Rights Reserved.
      2 
      3    Distributed under MIT license.
      4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
      5 */
      6 
      7 package org.brotli.wrapper.dec;
      8 
      9 import static org.junit.Assert.assertEquals;
     10 
     11 import org.brotli.integration.BundleHelper;
     12 import java.io.ByteArrayInputStream;
     13 import java.io.FileInputStream;
     14 import java.io.IOException;
     15 import java.io.InputStream;
     16 import java.nio.channels.Channels;
     17 import java.nio.channels.ReadableByteChannel;
     18 import java.util.List;
     19 import junit.framework.TestCase;
     20 import junit.framework.TestSuite;
     21 import org.junit.runner.RunWith;
     22 import org.junit.runners.AllTests;
     23 
     24 /** Tests for {@link org.brotli.wrapper.dec.BrotliDecoderChannel}. */
     25 @RunWith(AllTests.class)
     26 public class BrotliDecoderChannelTest {
     27 
     28   // TODO: remove when Bazel get JNI support.
     29   static {
     30     System.load(new java.io.File(new java.io.File(System.getProperty("java.library.path")),
     31         "liblibjni.so").getAbsolutePath());
     32   }
     33 
     34   static InputStream getBundle() throws IOException {
     35     return new FileInputStream(System.getProperty("TEST_BUNDLE"));
     36   }
     37 
     38   /** Creates a test suite. */
     39   public static TestSuite suite() throws IOException {
     40     TestSuite suite = new TestSuite();
     41     InputStream bundle = getBundle();
     42     try {
     43       List<String> entries = BundleHelper.listEntries(bundle);
     44       for (String entry : entries) {
     45         suite.addTest(new ChannelTestCase(entry));
     46       }
     47     } finally {
     48       bundle.close();
     49     }
     50     return suite;
     51   }
     52 
     53   /** Test case with a unique name. */
     54   static class ChannelTestCase extends TestCase {
     55     final String entryName;
     56     ChannelTestCase(String entryName) {
     57       super("BrotliDecoderChannelTest." + entryName);
     58       this.entryName = entryName;
     59     }
     60 
     61     @Override
     62     protected void runTest() throws Throwable {
     63       BrotliDecoderChannelTest.run(entryName);
     64     }
     65   }
     66 
     67   private static void run(String entryName) throws Throwable {
     68     InputStream bundle = getBundle();
     69     byte[] compressed;
     70     try {
     71       compressed = BundleHelper.readEntry(bundle, entryName);
     72     } finally {
     73       bundle.close();
     74     }
     75     if (compressed == null) {
     76       throw new RuntimeException("Can't read bundle entry: " + entryName);
     77     }
     78 
     79     ReadableByteChannel src = Channels.newChannel(new ByteArrayInputStream(compressed));
     80     ReadableByteChannel decoder = new BrotliDecoderChannel(src);
     81     long crc;
     82     try {
     83       crc = BundleHelper.fingerprintStream(Channels.newInputStream(decoder));
     84     } finally {
     85       decoder.close();
     86     }
     87     assertEquals(BundleHelper.getExpectedFingerprint(entryName), crc);
     88   }
     89 }
     90