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.BrotliJniTestBase;
12 import org.brotli.integration.BundleHelper;
13 import java.io.ByteArrayInputStream;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.List;
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.AllTests;
22 
23 /** Tests for {@link org.brotli.wrapper.dec.Decoder}. */
24 @RunWith(AllTests.class)
25 public class DecoderTest extends BrotliJniTestBase {
26 
getBundle()27   static InputStream getBundle() throws IOException {
28     return new FileInputStream(System.getProperty("TEST_BUNDLE"));
29   }
30 
31   /** Creates a test suite. */
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 DecoderTestCase(entry));
39       }
40     } finally {
41       bundle.close();
42     }
43     return suite;
44   }
45 
46   /** Test case with a unique name. */
47   static class DecoderTestCase extends TestCase {
48     final String entryName;
DecoderTestCase(String entryName)49     DecoderTestCase(String entryName) {
50       super("DecoderTest." + entryName);
51       this.entryName = entryName;
52     }
53 
54     @Override
runTest()55     protected void runTest() throws Throwable {
56       DecoderTest.run(entryName);
57     }
58   }
59 
run(String entryName)60   private static void run(String entryName) throws Throwable {
61     InputStream bundle = getBundle();
62     byte[] compressed;
63     try {
64       compressed = BundleHelper.readEntry(bundle, entryName);
65     } finally {
66       bundle.close();
67     }
68     if (compressed == null) {
69       throw new RuntimeException("Can't read bundle entry: " + entryName);
70     }
71 
72     byte[] decompressed = Decoder.decompress(compressed);
73 
74     long crc = BundleHelper.fingerprintStream(new ByteArrayInputStream(decompressed));
75     assertEquals(BundleHelper.getExpectedFingerprint(entryName), crc);
76   }
77 }
78