• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.brotli.wrapper.enc;
2 
3 import static org.junit.Assert.assertEquals;
4 
5 import org.brotli.integration.BrotliJniTestBase;
6 import org.brotli.integration.BundleHelper;
7 import org.brotli.wrapper.dec.BrotliInputStream;
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.util.List;
15 import junit.framework.TestCase;
16 import junit.framework.TestSuite;
17 import org.junit.runner.RunWith;
18 import org.junit.runners.AllTests;
19 
20 /** Tests for {@link org.brotli.wrapper.enc.BrotliOutputStream}. */
21 @RunWith(AllTests.class)
22 public class BrotliOutputStreamTest extends BrotliJniTestBase {
23 
24   private enum TestMode {
25     WRITE_ALL,
26     WRITE_CHUNKS,
27     WRITE_BYTE
28   }
29 
30   private static final int CHUNK_SIZE = 256;
31 
getBundle()32   static InputStream getBundle() throws IOException {
33     return new FileInputStream(System.getProperty("TEST_BUNDLE"));
34   }
35 
36   /** Creates a test suite. */
suite()37   public static TestSuite suite() throws IOException {
38     TestSuite suite = new TestSuite();
39     InputStream bundle = getBundle();
40     try {
41       List<String> entries = BundleHelper.listEntries(bundle);
42       for (String entry : entries) {
43         suite.addTest(new StreamTestCase(entry, TestMode.WRITE_ALL));
44         suite.addTest(new StreamTestCase(entry, TestMode.WRITE_CHUNKS));
45         suite.addTest(new StreamTestCase(entry, TestMode.WRITE_BYTE));
46       }
47     } finally {
48       bundle.close();
49     }
50     return suite;
51   }
52 
53   /** Test case with a unique name. */
54   static class StreamTestCase extends TestCase {
55     final String entryName;
56     final TestMode mode;
StreamTestCase(String entryName, TestMode mode)57     StreamTestCase(String entryName, TestMode mode) {
58       super("BrotliOutputStreamTest." + entryName + "." + mode.name());
59       this.entryName = entryName;
60       this.mode = mode;
61     }
62 
63     @Override
runTest()64     protected void runTest() throws Throwable {
65       BrotliOutputStreamTest.run(entryName, mode);
66     }
67   }
68 
run(String entryName, TestMode mode)69   private static void run(String entryName, TestMode mode) throws Throwable {
70     InputStream bundle = getBundle();
71     byte[] original;
72     try {
73       original = BundleHelper.readEntry(bundle, entryName);
74     } finally {
75       bundle.close();
76     }
77     if (original == null) {
78       throw new RuntimeException("Can't read bundle entry: " + entryName);
79     }
80 
81     if ((mode == TestMode.WRITE_CHUNKS) && (original.length <= CHUNK_SIZE)) {
82       return;
83     }
84 
85     ByteArrayOutputStream dst = new ByteArrayOutputStream();
86     OutputStream encoder = new BrotliOutputStream(dst);
87     try {
88       switch (mode) {
89         case WRITE_ALL:
90           encoder.write(original);
91           break;
92 
93         case WRITE_CHUNKS:
94           for (int offset = 0; offset < original.length; offset += CHUNK_SIZE) {
95             encoder.write(original, offset, Math.min(CHUNK_SIZE, original.length - offset));
96           }
97           break;
98 
99         case WRITE_BYTE:
100           for (byte singleByte : original) {
101             encoder.write(singleByte);
102           }
103           break;
104       }
105     } finally {
106       encoder.close();
107     }
108 
109     InputStream decoder = new BrotliInputStream(new ByteArrayInputStream(dst.toByteArray()));
110     try {
111       long originalCrc = BundleHelper.fingerprintStream(new ByteArrayInputStream(original));
112       long crc = BundleHelper.fingerprintStream(decoder);
113       assertEquals(originalCrc, crc);
114     } finally {
115       decoder.close();
116     }
117   }
118 }
119