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.nio.channels.Channels; 18 import java.nio.channels.ReadableByteChannel; 19 import java.util.List; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.AllTests; 24 25 /** Tests for {@link org.brotli.wrapper.dec.BrotliDecoderChannel}. */ 26 @RunWith(AllTests.class) 27 public class BrotliDecoderChannelTest extends BrotliJniTestBase { 28 getBundle()29 static InputStream getBundle() throws IOException { 30 return new FileInputStream(System.getProperty("TEST_BUNDLE")); 31 } 32 33 /** Creates a test suite. */ suite()34 public static TestSuite suite() throws IOException { 35 TestSuite suite = new TestSuite(); 36 InputStream bundle = getBundle(); 37 try { 38 List<String> entries = BundleHelper.listEntries(bundle); 39 for (String entry : entries) { 40 suite.addTest(new ChannelTestCase(entry)); 41 } 42 } finally { 43 bundle.close(); 44 } 45 return suite; 46 } 47 48 /** Test case with a unique name. */ 49 static class ChannelTestCase extends TestCase { 50 final String entryName; ChannelTestCase(String entryName)51 ChannelTestCase(String entryName) { 52 super("BrotliDecoderChannelTest." + entryName); 53 this.entryName = entryName; 54 } 55 56 @Override runTest()57 protected void runTest() throws Throwable { 58 BrotliDecoderChannelTest.run(entryName); 59 } 60 } 61 run(String entryName)62 private static void run(String entryName) throws Throwable { 63 InputStream bundle = getBundle(); 64 byte[] compressed; 65 try { 66 compressed = BundleHelper.readEntry(bundle, entryName); 67 } finally { 68 bundle.close(); 69 } 70 if (compressed == null) { 71 throw new RuntimeException("Can't read bundle entry: " + entryName); 72 } 73 74 ReadableByteChannel src = Channels.newChannel(new ByteArrayInputStream(compressed)); 75 ReadableByteChannel decoder = new BrotliDecoderChannel(src); 76 long crc; 77 try { 78 crc = BundleHelper.fingerprintStream(Channels.newInputStream(decoder)); 79 } finally { 80 decoder.close(); 81 } 82 assertEquals(BundleHelper.getExpectedFingerprint(entryName), crc); 83 } 84 } 85