2 * DictZip library test.
4 * Copyright (C) 2016 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.
23 import static org.testng.Assert.*;
25 import org.testng.SkipException;
26 import org.testng.annotations.*;
28 import java.io.EOFException;
29 import java.io.IOException;
30 import java.util.Arrays;
34 * Test of DictZipInputStream.
35 * @author Hiroshi Miura
37 public class DictZipInputStreamTest {
39 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
40 private DictZipInputStream din;
43 public void rewind() throws Exception {
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);
63 @Test (groups = "init")
64 public void testConstructor_defaultBufSize() throws Exception {
65 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"));
69 * Test constructor from filename.
72 @Test (groups = "init")
73 public void testConstructor_fromFilename() throws Exception {
74 din = new DictZipInputStream(dataFile);
78 * Test close of stream.
79 * @throws Exception if I/O error occurred.
81 @Test (dependsOnGroups = { "test" })
82 public void testClose() throws Exception {
87 * Test of read method, of class DictZipInputStream.
89 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
90 public void testRead() throws Exception {
91 System.out.println("read");
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));
100 * Test of read method, of class DictZipInputStream.
102 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
103 public void testRead_with_seek() throws Exception {
104 System.out.println("read with seek");
108 byte[] buf = new byte[len];
110 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
112 din.read(buf, 0, len);
113 assertTrue(Arrays.equals(buf, expResult));
117 * Test of read method, of class DictZipInputStream.
119 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
120 public void testRead_null() throws Exception {
121 System.out.println("read null buffer");
126 din.read(buf, 0, len);
127 fail("Should be throw exception.");
128 } catch (NullPointerException e) {
132 assertTrue(r, "Got NullPointerException when buffer is null");
136 * Test of read method, of class DictZipInputStream.
138 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
139 public void testRead_outOfBound() throws Exception {
140 System.out.println("read out of buffer size");
142 byte[] buf = new byte[len];
145 din.read(buf, 0, len + 10);
146 fail("Should be throw exception.");
147 } catch (IndexOutOfBoundsException e) {
151 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
155 * Test of read method, of class DictZipInputStream.
157 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
158 public void testRead_zeroSize() throws Exception {
159 System.out.println("read zero size");
161 byte[] buf = new byte[len];
162 int size = din.read(buf, 0, 0);
163 throw new SkipException("Unknown bug.");
164 //assertEquals(size, 0);
168 * Test of read method, of class DictZipInputStream.
170 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
171 public void testRead_with_seekLast() throws Exception {
172 System.out.println("read with seek to last");
176 byte[] buf = new byte[len];
177 din.read(buf, 0, len);
178 int result = din.read(buf, 0, len);
179 assertEquals(result, -1);
183 * Test of readFully method, of class DictZipInputStream.
184 * @throws java.lang.Exception
186 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
187 public void testReadFully_byteArr() throws Exception {
188 System.out.println("readFully");
192 byte[] buf = new byte[len];
197 * Test of readFully method, of class DictZipInputStream.
198 * @throws java.lang.Exception
200 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
201 public void testReadFully_3args() throws Exception {
202 System.out.println("readFully");
206 byte[] buf = new byte[len];
207 din.readFully(buf, 0, len);
211 * Test of readFully method, of class DictZipInputStream.
212 * @throws java.lang.Exception
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];
220 } catch (EOFException e) {
225 assertEquals(din.getCrc(), 0x024d1f37);
226 assertEquals(din.getLength(), 383783);
230 * Test getComplength method.
231 * @throws IOException
233 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
234 public void testGetCompLength() throws IOException {
235 System.out.println("getCompLength");
236 assertEquals(din.getCompLength(), 136856);
240 * Test getMtime method.
242 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
243 public void testGetMtime() throws IOException {
244 System.out.println("getMtime");
245 assertEquals(din.getMtime(), 1193780332);
249 * Test getChunkLength method.
251 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
252 public void testGetChunkLength() throws IOException {
253 System.out.println("getChunkLength");
254 assertEquals(din.getChunkLength(), 58315);
258 * Test getChunkCount method.
260 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
261 public void testGetChunkCount() throws IOException {
262 System.out.println("getChunkCount");
263 assertEquals(din.getChunkCount(), 7);
267 * Test getFilename method.
269 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
270 public void testGetFilename() throws IOException {
271 System.out.println("getFilename");
272 assertEquals(din.getFilename(), "results.dict");