OSDN Git Service

Merge pull request #13 from miurahr/check_stream
[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.IOException;
47 import java.util.Arrays;
48
49
50 /**
51  * Test of DictZipInputStream.
52  * @author Hiroshi Miura
53  */
54 public class DictZipInputStreamTest {
55
56     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
57     private DictZipInputStream din;
58
59     @BeforeMethod
60     public void rewind() throws Exception {
61         if (din != null) {
62             din.seek(0);
63         }
64     }
65
66     /**
67      * Test constructor.
68      * @throws Exception
69      */
70     @Test (groups = "init", dependsOnMethods = { "testConstructor_fromFilename",
71             "testConstructor_defaultBufSize"})
72     public void testConstructor() throws Exception {
73         din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534);
74     }
75
76     /**
77      * Test constructor.
78      * @throws Exception
79      */
80     @Test (groups = "init")
81     public void testConstructor_defaultBufSize() throws Exception {
82         din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"));
83     }
84
85     /**
86      * Test constructor from filename.
87      * @throws Exception
88      */
89     @Test (groups = "init")
90     public void testConstructor_fromFilename() throws Exception {
91         din = new DictZipInputStream(dataFile);
92     }
93
94     /**
95      * Test close of stream.
96      * @throws Exception if I/O error occurred.
97      */
98     @Test (dependsOnGroups = { "test" })
99     public void testClose() throws Exception {
100         din.close();
101     }
102
103     /**
104      * Test of read method, of class DictZipInputStream.
105      */
106     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
107     public void testRead() throws Exception {
108         System.out.println("read");
109         int len = 10;
110         byte[] buf = new byte[len];
111         byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
112         din.read(buf, 0, len);
113         assertTrue(Arrays.equals(expResult, buf));
114     }
115
116     /**
117      * Test of read method, of class DictZipInputStream.
118      */
119     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
120     public void testRead_with_seek() throws Exception {
121         System.out.println("read with seek");
122         int start = 0x20;
123         int len = 10;
124         din.seek(start);
125         byte[] buf = new byte[len];
126         byte[] expResult = {
127             0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
128         };
129         din.read(buf, 0, len);
130         assertTrue(Arrays.equals(buf, expResult));
131     }
132
133     /**
134      * Test of read method, of class DictZipInputStream.
135      */
136     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
137     public void testRead_null() throws Exception {
138         System.out.println("read null buffer");
139         int len = 10;
140         byte[] buf = null;
141         boolean r = false;
142         try {
143             din.read(buf, 0, len);
144             fail("Should be throw exception.");
145         } catch (NullPointerException e) {
146             // expected.
147             r = true;
148         }
149         assertTrue(r, "Got NullPointerException when buffer is null");
150     }
151
152     /**
153      * Test of read method, of class DictZipInputStream.
154      */
155     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
156     public void testRead_outOfBound() throws Exception {
157         System.out.println("read out of buffer size");
158         int len = 10;
159         byte[] buf = new byte[len];
160         boolean r = false;
161         try {
162             din.read(buf, 0, len + 10);
163             fail("Should be throw exception.");
164         } catch (IndexOutOfBoundsException e) {
165             // expected.
166             r = true;
167         }
168         assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
169     }
170
171     /**
172      * Test of read method, of class DictZipInputStream.
173      */
174     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
175     public void testRead_zeroSize() throws Exception {
176         System.out.println("read zero size");
177         int len = 512;
178         byte[] buf = new byte[len];
179         int size = din.read(buf, 0, 0);
180         throw new SkipException("Unknown bug.");
181         //assertEquals(size, 0);
182     }
183
184     /**
185      * Test of read method, of class DictZipInputStream.
186      */
187     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
188     public void testRead_with_seekLast() throws Exception {
189         System.out.println("read with seek to last");
190         int start = 383273;
191         int len = 512;
192         din.seek(start);
193         byte[] buf = new byte[len];
194         din.read(buf, 0, len);
195         int result = din.read(buf, 0, len);
196         assertEquals(result, -1);
197     }
198
199    /**
200      * Test of readFully method, of class DictZipInputStream.
201      * @throws java.lang.Exception
202      */
203     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
204     public void testReadFully_byteArr() throws Exception {
205         System.out.println("readFully");
206         int start = 100;
207         int len = 10;
208         din.seek(start);
209         byte[] buf = new byte[len];
210         din.readFully(buf);
211     }
212
213     /**
214      * Test of readFully method, of class DictZipInputStream.
215      * @throws java.lang.Exception
216      */
217     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
218     public void testReadFully_3args() throws Exception {
219         System.out.println("readFully");
220         int start = 200;
221         int len = 10;
222         din.seek(start);
223         byte[] buf = new byte[len];
224         din.readFully(buf, 0, len);
225     }
226
227     /**
228      * Test of readFully method, of class DictZipInputStream.
229      * @throws java.lang.Exception
230      */
231     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
232     public void testReadFully_readTrailer() throws IOException {
233         System.out.println("readFully and readTrailer");
234         byte[] buf = new byte[512];
235         try {
236             din.readFully(buf);
237         } catch (EOFException e) {
238             // Normal, continue
239         }
240         // read trailer
241         din.readTrailer();
242         assertEquals(din.getCrc(), 0x024d1f37);
243         assertEquals(din.getLength(), 383783);
244     }
245
246     /**
247      * Test getComplength method.
248      * @throws IOException
249      */
250     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
251     public void testGetCompLength() throws IOException {
252         System.out.println("getCompLength");
253         assertEquals(din.getCompLength(), 136856);
254     }
255
256     /**
257      * Test getMtime method.
258      */
259     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
260     public void testGetMtime() throws IOException {
261         System.out.println("getMtime");
262         assertEquals(din.getMtime(), 1193780332);
263     }
264
265     /**
266      * Test getChunkLength method.
267      */
268     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
269     public void testGetChunkLength() throws IOException {
270         System.out.println("getChunkLength");
271         assertEquals(din.getChunkLength(), 58315);
272     }
273
274     /**
275      * Test getChunkCount method.
276      */
277     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
278     public void testGetChunkCount() throws IOException {
279         System.out.println("getChunkCount");
280         assertEquals(din.getChunkCount(), 7);
281     }
282
283     /**
284      * Test getFilename method.
285      */
286     @Test (groups = "test", dependsOnMethods = { "testConstructor" })
287     public void testGetFilename() throws IOException {
288         System.out.println("getFilename");
289         assertEquals(din.getFilename(), "results.dict");
290     }
291
292
293 }