OSDN Git Service

fefb80337c83fd74afe3c89cd9a1b4d23235867a
[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
43 import java.io.EOFException;
44 import java.io.FileInputStream;
45 import java.io.IOException;
46 import java.util.Arrays;
47
48 import static org.junit.jupiter.api.Assertions.assertEquals;
49 import static org.junit.jupiter.api.Assertions.assertTrue;
50 import static tokyo.northside.io.IOUtils2.contentEquals;
51
52 /**
53  * Test of DictZipInputStream.
54  * @author Hiroshi Miura
55  */
56 public class DictZipInputStreamTest {
57
58     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
59
60     /**
61      * Test constructor @TestFactory.
62      * @throws Exception when i/o error.
63      */
64     @Test
65     public void testConstructorDefaultBufSize() throws Exception {
66         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
67             // pass
68         }
69     }
70
71     /**
72      * Test constructor from filename.
73      * @throws Exception when i/o error.
74      */
75     @Test
76     public void testConstructorFromFilename() throws Exception {
77         try (DictZipInputStream din = new DictZipInputStream(dataFile)) {
78             // pass
79         }
80     }
81
82     /**
83      * Test of read method, of class DictZipInputStream.
84      * @throws Exception when i/o error.
85      */
86     @Test
87     public void testRead() throws Exception {
88         int len = 10;
89         byte[] buf = new byte[len];
90         byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
91         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
92             din.seek(0);
93             din.read(buf, 0, len);
94             assertTrue(Arrays.equals(expResult, buf));
95         }
96     }
97
98     /**
99      * Test of read method, of class DictZipInputStream.
100      * @throws Exception when i/o error.
101      */
102     @Test
103     public void testReadWithSeek() throws Exception {
104         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
105             int start = 0x20;
106             int len = 10;
107             din.seek(start);
108             byte[] buf = new byte[len];
109             byte[] expResult = {
110                     0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
111             };
112             din.read(buf, 0, len);
113             assertTrue(Arrays.equals(buf, expResult));
114         }
115     }
116
117     /**
118      * Test of read method, of class DictZipInputStream.
119      * @throws Exception when i/o error.
120      */
121     @Test
122     public void testReadNull() throws Exception {
123         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
124             int len = 10;
125             byte[] buf = null;
126             boolean r = false;
127             try {
128                 din.read(buf, 0, len);
129                 Assertions.fail("Should be throw exception.");
130             } catch (NullPointerException e) {
131                 // expected.
132                 r = true;
133             }
134             assertTrue(r, "Got NullPointerException when buffer is null");
135         }
136     }
137
138     /**
139      * Test of read method, of class DictZipInputStream.
140      * @throws Exception when i/o error.
141      */
142     @Test
143     public void testReadOutOfBound() throws Exception {
144         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
145             int len = 10;
146             byte[] buf = new byte[len];
147             boolean r = false;
148             try {
149                 din.read(buf, 0, len + 10);
150                 Assertions.fail("Should be throw exception.");
151             } catch (IndexOutOfBoundsException e) {
152                 // expected.
153                 r = true;
154             }
155             assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
156         }
157     }
158
159     /**
160      * Test of read method, of class DictZipInputStream.
161      * @throws Exception when i/o error.
162      */
163     @Test
164     public void testReadZeroSize() throws Exception {
165         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
166             int len = 512;
167             byte[] buf = new byte[len];
168             int size = din.read(buf, 0, 0);
169             assertEquals(size, 0);
170         }
171     }
172
173     /**
174      * Test of read method, of class DictZipInputStream.
175      * @throws Exception when i/o error.
176      */
177     @Test
178     public void testReadWithSeekLast() throws Exception {
179         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
180             int start = 383273;
181             int len = 512;
182             din.seek(start);
183             byte[] buf = new byte[len];
184             din.read(buf, 0, len);
185             int result = din.read(buf, 0, len);
186             assertEquals(result, -1);
187         }
188     }
189
190     /**
191      * Test of readFully method, of class DictZipInputStream.
192      * @throws Exception when i/o error.
193      */
194     @Test
195     public void testReadFullyByteArr() throws Exception {
196         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
197             int start = 100;
198             int len = 10;
199             din.seek(start);
200             byte[] buf = new byte[len];
201             din.readFully(buf);
202         }
203     }
204
205     /**
206      * Test of readFully method, of class DictZipInputStream.
207      * @throws Exception when i/o error.
208      */
209     @Test
210     public void testReadFully3args() throws Exception {
211         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
212             int start = 200;
213             int len = 10;
214             din.seek(start);
215             byte[] buf = new byte[len];
216             din.readFully(buf, 0, len);
217         }
218     }
219
220     /**
221      * Test of readFully method, of class DictZipInputStream.
222      * @throws IOException when i/o error.
223      */
224     @Test
225     public void testReadFullyReadTrailer() throws IOException {
226         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
227             byte[] buf = new byte[512];
228             try {
229                 din.readFully(buf);
230             } catch (EOFException e) {
231                 // Normal, continue
232             }
233             // read trailer
234             din.readTrailer();
235             assertEquals(din.getCrc(), 0x024d1f37);
236             assertEquals(din.getLength(), 383783);
237         }
238     }
239
240     /**
241      * Test getComplength method.
242      * @throws IOException when i/o error.
243      */
244     @Test
245     public void testGetCompLength() throws IOException {
246         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
247             System.out.println("getCompLength");
248             assertEquals(din.getCompLength(), 136856);
249         }
250     }
251
252     /**
253      * Test getMtime method.
254      * @throws IOException when i/o error.
255      */
256     @Test
257     public void testGetMtime() throws IOException {
258         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
259             assertEquals(din.getMtime(), 1193780332);
260         }
261     }
262
263     /**
264      * Test getChunkLength method.
265      * @throws IOException when i/o error.
266      */
267     @Test
268     public void testGetChunkLength() throws IOException {
269         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
270             System.out.println("getChunkLength");
271             assertEquals(din.getChunkLength(), 58315);
272         }
273     }
274
275     /**
276      * Test getChunkCount method.
277      * @throws IOException when i/o error.
278      */
279     @Test
280     public void testGetChunkCount() throws IOException {
281         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
282             System.out.println("getChunkCount");
283             assertEquals(din.getChunkCount(), 7);
284         }
285     }
286
287     /**
288      * Test getFilename method.
289      * @throws IOException when i/o error.
290      */
291     @Test
292     public void testGetFilename() throws IOException {
293         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
294             assertEquals(din.getFilename(), "results.dict");
295         }
296     }
297
298     /**
299      * Test readFully with large seek.
300      * @throws Exception when i/o error.
301      */
302     @Test
303     public void testReadFullySeek2() throws Exception {
304         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
305             int start = 56003;
306             int len = 195;
307             try {
308                 din.seek(start);
309             } catch (IOException ioe) {
310                 Assertions.fail("Unexpected IOException");
311             }
312             byte[] buf = new byte[len];
313             try {
314                 din.readFully(buf);
315             } catch (EOFException eofe) {
316                 Assertions.fail("Unexpected EOF");
317             }
318         }
319     }
320
321     /**
322      * Test with large  seek.
323      * @throws Exception when i/o error.
324      */
325     @Test
326     public void testReadSeek2() throws Exception {
327         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
328             System.out.println("read_seek2");
329             int start = 56003;
330             int len = 195;
331             try {
332                 din.seek(start);
333             } catch (IOException ioe) {
334                 Assertions.fail("Unexpected IOException");
335             }
336             byte[] buf = new byte[len];
337             int result = 0;
338             try {
339                 result = din.read(buf, 0, len);
340             } catch (EOFException eofe) {
341                 Assertions.fail("Unexpected EOF");
342             }
343             assertEquals(result, len);
344         }
345     }
346
347     /**
348      * Test with large seek comparison content.
349      * @throws Exception when i/o error.
350      */
351     @Test
352     public void testReadSeek3() throws Exception {
353         try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
354             int start = 56003;
355             int len = 195;
356             try {
357                 din.seek(start);
358             } catch (IOException ioe) {
359                 Assertions.fail("Unexpected IOException");
360             }
361             FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
362             in2.skip(start);
363             assertTrue(contentEquals(din, in2, 0, len));
364         }
365     }
366 }