OSDN Git Service

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