2 * DictZip library test.
4 * Copyright (C) 2016,2019 Hiroshi Miura
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.
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.
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.
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
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.
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
43 import java.io.EOFException;
44 import java.io.FileInputStream;
45 import java.io.IOException;
46 import java.util.Arrays;
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;
54 * Test of DictZipInputStream.
55 * @author Hiroshi Miura
57 public class DictZipInputStreamTest {
59 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
60 private DictZipInputStream din;
63 * rewind dictionary file.
65 * @throws Exception when i/o error.
68 public void rewind() throws Exception {
76 * @throws Exception when i/o error.
78 @Test (groups = "init", dependsOnMethods = { "testConstructorFromFilename",
79 "testConstructorDefaultBufSize"})
80 public void testConstructor() throws Exception {
81 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534);
86 * @throws Exception when i/o error.
88 @Test (groups = "init")
89 public void testConstructorDefaultBufSize() throws Exception {
90 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"));
94 * Test constructor from filename.
95 * @throws Exception when i/o error.
97 @Test (groups = "init")
98 public void testConstructorFromFilename() throws Exception {
99 din = new DictZipInputStream(dataFile);
103 * Test close of stream.
104 * @throws Exception if I/O error occurred.
106 @Test (groups = "exit", dependsOnGroups = { "test" })
107 public void testClose() throws Exception {
113 * Test of read method, of class DictZipInputStream.
114 * @throws Exception when i/o error.
116 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
117 public void testRead() throws Exception {
118 System.out.println("read");
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));
127 * Test of read method, of class DictZipInputStream.
128 * @throws Exception when i/o error.
130 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
131 public void testReadWithSeek() throws Exception {
132 System.out.println("read with seek");
136 byte[] buf = new byte[len];
138 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
140 din.read(buf, 0, len);
141 assertTrue(Arrays.equals(buf, expResult));
145 * Test of read method, of class DictZipInputStream.
146 * @throws Exception when i/o error.
148 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
149 public void testReadNull() throws Exception {
150 System.out.println("read null buffer");
155 din.read(buf, 0, len);
156 fail("Should be throw exception.");
157 } catch (NullPointerException e) {
161 assertTrue(r, "Got NullPointerException when buffer is null");
165 * Test of read method, of class DictZipInputStream.
166 * @throws Exception when i/o error.
168 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
169 public void testReadOutOfBound() throws Exception {
170 System.out.println("read out of buffer size");
172 byte[] buf = new byte[len];
175 din.read(buf, 0, len + 10);
176 fail("Should be throw exception.");
177 } catch (IndexOutOfBoundsException e) {
181 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
185 * Test of read method, of class DictZipInputStream.
186 * @throws Exception when i/o error.
188 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
189 public void testReadZeroSize() throws Exception {
190 System.out.println("read zero size");
192 byte[] buf = new byte[len];
193 int size = din.read(buf, 0, 0);
194 assertEquals(size, 0);
198 * Test of read method, of class DictZipInputStream.
199 * @throws Exception when i/o error.
201 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
202 public void testReadWithSeekLast() throws Exception {
203 System.out.println("read with seek to last");
207 byte[] buf = new byte[len];
208 din.read(buf, 0, len);
209 int result = din.read(buf, 0, len);
210 assertEquals(result, -1);
214 * Test of readFully method, of class DictZipInputStream.
215 * @throws Exception when i/o error.
217 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
218 public void testReadFullyByteArr() throws Exception {
219 System.out.println("readFully");
223 byte[] buf = new byte[len];
228 * Test of readFully method, of class DictZipInputStream.
229 * @throws Exception when i/o error.
231 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
232 public void testReadFully3args() throws Exception {
233 System.out.println("readFully");
237 byte[] buf = new byte[len];
238 din.readFully(buf, 0, len);
242 * Test of readFully method, of class DictZipInputStream.
243 * @throws IOException when i/o error.
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];
251 } catch (EOFException e) {
256 assertEquals(din.getCrc(), 0x024d1f37);
257 assertEquals(din.getLength(), 383783);
261 * Test getComplength method.
262 * @throws IOException when i/o error.
264 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
265 public void testGetCompLength() throws IOException {
266 System.out.println("getCompLength");
267 assertEquals(din.getCompLength(), 136856);
271 * Test getMtime method.
272 * @throws IOException when i/o error.
274 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
275 public void testGetMtime() throws IOException {
276 System.out.println("getMtime");
277 assertEquals(din.getMtime(), 1193780332);
281 * Test getChunkLength method.
282 * @throws IOException when i/o error.
284 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
285 public void testGetChunkLength() throws IOException {
286 System.out.println("getChunkLength");
287 assertEquals(din.getChunkLength(), 58315);
291 * Test getChunkCount method.
292 * @throws IOException when i/o error.
294 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
295 public void testGetChunkCount() throws IOException {
296 System.out.println("getChunkCount");
297 assertEquals(din.getChunkCount(), 7);
301 * Test getFilename method.
302 * @throws IOException when i/o error.
304 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
305 public void testGetFilename() throws IOException {
306 System.out.println("getFilename");
307 assertEquals(din.getFilename(), "results.dict");
311 * Test readFully with large seek.
312 * @throws Exception when i/o error.
314 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
315 public void testReadFullySeek2() throws Exception {
316 System.out.println("readFully_seek2");
321 } catch (IOException ioe) {
322 fail("Unexpected IOException");
324 byte[] buf = new byte[len];
327 } catch (EOFException eofe) {
328 fail("Unexpected EOF");
333 * Test with large seek.
334 * @throws Exception when i/o error.
336 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
337 public void testReadSeek2() throws Exception {
338 System.out.println("read_seek2");
343 } catch (IOException ioe) {
344 fail("Unexpected IOException");
346 byte[] buf = new byte[len];
349 result = din.read(buf, 0, len);
350 } catch (EOFException eofe) {
351 fail("Unexpected EOF");
353 assertEquals(result, len);
357 * Test with large seek comparison content.
358 * @throws Exception when i/o error.
360 @Test (groups = "test", dependsOnMethods = { "testConstructor"})
361 public void testReadSeek3() throws Exception {
362 System.out.println("read_seek2");
367 } catch (IOException ioe) {
368 fail("Unexpected IOException");
370 FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
372 assertTrue(contentEquals(din, in2, 0, len));