OSDN Git Service

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