OSDN Git Service

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