OSDN Git Service

192f6afd6564186501b43717f4ccf80241887a92
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / DictZipInputStreamTest.java
1 /*
2  * DictZip library test.
3  *
4  * Copyright (C) 2016,2019,2022 Hiroshi Miura
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Linking this library statically or dynamically with other modules is
21  * making a combined work based on this library.  Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * As a special exception, the copyright holders of this library give you
26  * permission to link this library with independent modules to produce an
27  * executable, regardless of the license terms of these independent
28  * modules, and to copy and distribute the resulting executable under
29  * terms of your choice, provided that you also meet, for each linked
30  * independent module, the terms and conditions of the license of that
31  * module.  An independent module is a module which is not derived from
32  * or based on this library.  If you modify this library, you may extend
33  * this exception to your version of the library, but you are not
34  * obligated to do so.  If you do not wish to do so, delete this
35  * exception statement from your version.
36  */
37
38 package org.dict.zip;
39
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.Test;
42 import tokyo.northside.io.IOUtils2;
43
44 import java.io.EOFException;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.util.Arrays;
48
49 import static org.junit.jupiter.api.Assertions.assertEquals;
50 import static org.junit.jupiter.api.Assertions.assertNotNull;
51 import static org.junit.jupiter.api.Assertions.assertTrue;
52
53 /**
54  * Test of DictZipInputStream.
55  * @author Hiroshi Miura
56  */
57 public class DictZipInputStreamTest {
58
59     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
60     private final String dataFile2 = this.getClass().getResource("/test.dsl.dz").getFile();
61
62     /**
63      * Test constructor @TestFactory.
64      * @throws Exception when i/o error.
65      */
66     @Test
67     public void testConstructorDefaultBufSize() throws Exception {
68         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
69             assertNotNull(din);
70         }
71     }
72
73     /**
74      * Test constructor from filename.
75      * @throws Exception when i/o error.
76      */
77     @Test
78     public void testConstructorFromFilename() throws Exception {
79         try (DictZipInputStream din = new DictZipInputStream(dataFile)) {
80             assertNotNull(din);
81         }
82     }
83
84     /**
85      * Test of read method, of class DictZipInputStream.
86      * @throws Exception when i/o error.
87      */
88     @Test
89     public void testRead() throws Exception {
90         int len = 10;
91         byte[] buf = new byte[len];
92         byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
93         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
94             din.read(buf, 0, len);
95             assertTrue(Arrays.equals(expResult, buf));
96         }
97     }
98
99     /**
100      * Test of read method with another file, of class DictZipInputStream.
101      * @throws Exception when i/o error.
102      */
103     @Test
104     public void testRead2() throws Exception {
105         int len = 10;
106         byte[] buf = new byte[len];
107         byte[] expResult = {(byte) 0xFF, (byte) 0xFE, 35, 0, 78, 0, 65, 0, 77, 0};
108         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile2, "r"))) {
109             din.read(buf, 0, len);
110             assertTrue(Arrays.equals(expResult, buf));
111         }
112     }
113
114     /**
115      * Test mark and reset methods.
116      * @throws Exception when i/o error.
117      */
118     @Test
119     public void testMarkReset() throws Exception {
120         int len = 10;
121         byte[] buf1 = new byte[len];
122         byte[] buf2 = new byte[len];
123         byte[] buf3 = new byte[len];
124         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
125             assertTrue(din.markSupported());
126             din.read(buf1, 0, len);
127             din.mark(20);
128             din.read(buf2, 0, len);
129             din.reset();
130             din.read(buf3, 0, len);
131             assertTrue(Arrays.equals(buf2, buf3));
132         }
133     }
134     /**
135      * Test of read method, of class DictZipInputStream.
136      * @throws Exception when i/o error.
137      */
138     @Test
139     public void testReadWithSeek() throws Exception {
140         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
141             int start = 0x20;
142             int len = 10;
143             din.seek(start);
144             byte[] buf = new byte[len];
145             byte[] expResult = {
146                     0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
147             };
148             din.read(buf, 0, len);
149             assertTrue(Arrays.equals(buf, expResult));
150         }
151     }
152
153     /**
154      * Test of read method, of class DictZipInputStream.
155      * @throws Exception when i/o error.
156      */
157     @Test
158     public void testReadNull() throws Exception {
159         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
160             int len = 10;
161             byte[] buf = null;
162             boolean r = false;
163             try {
164                 din.read(buf, 0, len);
165                 Assertions.fail("Should be throw exception.");
166             } catch (NullPointerException e) {
167                 // expected.
168                 r = true;
169             }
170             assertTrue(r, "Got NullPointerException when buffer is null");
171         }
172     }
173
174     /**
175      * Test of read method, of class DictZipInputStream.
176      * @throws Exception when i/o error.
177      */
178     @Test
179     public void testReadOutOfBound() throws Exception {
180         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
181             int len = 10;
182             byte[] buf = new byte[len];
183             boolean r = false;
184             try {
185                 din.read(buf, 0, len + 10);
186                 Assertions.fail("Should be throw exception.");
187             } catch (IndexOutOfBoundsException e) {
188                 // expected.
189                 r = true;
190             }
191             assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
192         }
193     }
194
195     /**
196      * Test of read method, of class DictZipInputStream.
197      * @throws Exception when i/o error.
198      */
199     @Test
200     public void testReadZeroSize() throws Exception {
201         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
202             int len = 512;
203             byte[] buf = new byte[len];
204             int size = din.read(buf, 0, 0);
205             assertEquals(size, 0);
206         }
207     }
208
209     /**
210      * Test of read method, of class DictZipInputStream.
211      * @throws Exception when i/o error.
212      */
213     @Test
214     public void testReadWithSeekLast() throws Exception {
215         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
216             int start = 383273;
217             int len = 512;
218             din.seek(start);
219             byte[] buf = new byte[len];
220             din.read(buf, 0, len);
221             int result = din.read(buf, 0, len);
222             assertEquals(result, -1);
223         }
224     }
225
226     /**
227      * Test of readFully method, of class DictZipInputStream.
228      * @throws Exception when i/o error.
229      */
230     @Test
231     public void testReadFullyByteArr() throws Exception {
232         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
233             int start = 100;
234             int len = 10;
235             din.seek(start);
236             byte[] buf = new byte[len];
237             din.readFully(buf);
238         }
239     }
240
241     /**
242      * Test of readFully method, of class DictZipInputStream.
243      * @throws Exception when i/o error.
244      */
245     @Test
246     public void testReadFully3args() throws Exception {
247         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
248             int start = 200;
249             int len = 10;
250             din.seek(start);
251             byte[] buf = new byte[len];
252             din.readFully(buf, 0, len);
253         }
254     }
255
256     /**
257      * Test of readFully method, of class DictZipInputStream.
258      * @throws IOException when i/o error.
259      */
260     @Test
261     public void testReadFullyReadTrailer() throws IOException {
262         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
263             byte[] buf = new byte[512];
264             try {
265                 din.readFully(buf);
266             } catch (EOFException e) {
267                 // Normal, continue
268             }
269             // read trailer
270             din.readTrailer();
271             assertEquals(din.getCrc(), 0x024d1f37);
272             assertEquals(din.getLength(), 383783);
273         }
274     }
275
276     /**
277      * Test getComplength method.
278      * @throws IOException when i/o error.
279      */
280     @Test
281     public void testGetCompLength() throws IOException {
282         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
283             System.out.println("getCompLength");
284             assertEquals(din.getCompLength(), 136856);
285         }
286     }
287
288     /**
289      * Test getMtime method.
290      * @throws IOException when i/o error.
291      */
292     @Test
293     public void testGetMtime() throws IOException {
294         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
295             assertEquals(din.getMtime(), 1193780332);
296         }
297     }
298
299     /**
300      * Test getChunkLength method.
301      * @throws IOException when i/o error.
302      */
303     @Test
304     public void testGetChunkLength() throws IOException {
305         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
306             System.out.println("getChunkLength");
307             assertEquals(din.getChunkLength(), 58315);
308         }
309     }
310
311     /**
312      * Test getChunkCount method.
313      * @throws IOException when i/o error.
314      */
315     @Test
316     public void testGetChunkCount() throws IOException {
317         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
318             System.out.println("getChunkCount");
319             assertEquals(din.getChunkCount(), 7);
320         }
321     }
322
323     /**
324      * Test getFilename method.
325      * @throws IOException when i/o error.
326      */
327     @Test
328     public void testGetFilename() throws IOException {
329         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
330             assertEquals(din.getFilename(), "results.dict");
331         }
332     }
333
334     /**
335      * Test readFully with large seek.
336      * @throws Exception when i/o error.
337      */
338     @Test
339     public void testReadFullySeek2() throws Exception {
340         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
341             int start = 56003;
342             int len = 195;
343             try {
344                 din.seek(start);
345             } catch (IOException ioe) {
346                 Assertions.fail("Unexpected IOException");
347             }
348             byte[] buf = new byte[len];
349             try {
350                 din.readFully(buf);
351             } catch (EOFException eofe) {
352                 Assertions.fail("Unexpected EOF");
353             }
354         }
355     }
356
357     /**
358      * Test with large  seek.
359      * @throws Exception when i/o error.
360      */
361     @Test
362     public void testReadSeek2() throws Exception {
363         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
364             System.out.println("read_seek2");
365             int start = 56003;
366             int len = 195;
367             try {
368                 din.seek(start);
369             } catch (IOException ioe) {
370                 Assertions.fail("Unexpected IOException");
371             }
372             byte[] buf = new byte[len];
373             int result = 0;
374             try {
375                 result = din.read(buf, 0, len);
376             } catch (EOFException eofe) {
377                 Assertions.fail("Unexpected EOF");
378             }
379             assertEquals(result, len);
380         }
381     }
382
383     /**
384      * Test with large seek comparison content.
385      * @throws Exception when i/o error.
386      */
387     @Test
388     public void testReadSeek3() throws Exception {
389         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
390             int start = 56003;
391             int len = 195;
392             try {
393                 din.seek(start);
394             } catch (IOException ioe) {
395                 Assertions.fail("Unexpected IOException");
396             }
397             FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
398             in2.skip(start);
399             assertTrue(IOUtils2.contentEquals(din, in2, 0, len));
400         }
401     }
402 }