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