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.
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 static org.testng.Assert.*;
42 import org.testng.SkipException;
43 import org.testng.annotations.*;
45 import java.io.EOFException;
46 import java.io.FileInputStream;
47 import java.io.IOException;
48 import java.util.Arrays;
50 import static tokyo.northside.io.IOUtils2.contentEquals;
53 * Test of DictZipInputStream.
54 * @author Hiroshi Miura
56 public class DictZipInputStreamTest {
58 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
59 private DictZipInputStream din;
62 public void rewind() throws Exception {
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);
82 @Test (groups = "init")
83 public void testConstructor_defaultBufSize() throws Exception {
84 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"));
88 * Test constructor from filename.
91 @Test (groups = "init")
92 public void testConstructor_fromFilename() throws Exception {
93 din = new DictZipInputStream(dataFile);
97 * Test close of stream.
98 * @throws Exception if I/O error occurred.
100 @Test (groups = "exit", dependsOnGroups = { "test" })
101 public void testClose() throws Exception {
107 * Test of read method, of class DictZipInputStream.
109 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
110 public void testRead() throws Exception {
111 System.out.println("read");
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));
120 * Test of read method, of class DictZipInputStream.
122 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
123 public void testRead_with_seek() throws Exception {
124 System.out.println("read with seek");
128 byte[] buf = new byte[len];
130 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
132 din.read(buf, 0, len);
133 assertTrue(Arrays.equals(buf, expResult));
137 * Test of read method, of class DictZipInputStream.
139 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
140 public void testRead_null() throws Exception {
141 System.out.println("read null buffer");
146 din.read(buf, 0, len);
147 fail("Should be throw exception.");
148 } catch (NullPointerException e) {
152 assertTrue(r, "Got NullPointerException when buffer is null");
156 * Test of read method, of class DictZipInputStream.
158 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
159 public void testRead_outOfBound() throws Exception {
160 System.out.println("read out of buffer size");
162 byte[] buf = new byte[len];
165 din.read(buf, 0, len + 10);
166 fail("Should be throw exception.");
167 } catch (IndexOutOfBoundsException e) {
171 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
175 * Test of read method, of class DictZipInputStream.
177 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
178 public void testRead_zeroSize() throws Exception {
179 System.out.println("read zero size");
181 byte[] buf = new byte[len];
182 int size = din.read(buf, 0, 0);
183 assertEquals(size, 0);
187 * Test of read method, of class DictZipInputStream.
189 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
190 public void testRead_with_seekLast() throws Exception {
191 System.out.println("read with seek to last");
195 byte[] buf = new byte[len];
196 din.read(buf, 0, len);
197 int result = din.read(buf, 0, len);
198 assertEquals(result, -1);
202 * Test of readFully method, of class DictZipInputStream.
203 * @throws java.lang.Exception
205 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
206 public void testReadFully_byteArr() throws Exception {
207 System.out.println("readFully");
211 byte[] buf = new byte[len];
216 * Test of readFully method, of class DictZipInputStream.
217 * @throws java.lang.Exception
219 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
220 public void testReadFully_3args() throws Exception {
221 System.out.println("readFully");
225 byte[] buf = new byte[len];
226 din.readFully(buf, 0, len);
230 * Test of readFully method, of class DictZipInputStream.
231 * @throws java.lang.Exception
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];
239 } catch (EOFException e) {
244 assertEquals(din.getCrc(), 0x024d1f37);
245 assertEquals(din.getLength(), 383783);
249 * Test getComplength method.
250 * @throws IOException
252 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
253 public void testGetCompLength() throws IOException {
254 System.out.println("getCompLength");
255 assertEquals(din.getCompLength(), 136856);
259 * Test getMtime method.
261 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
262 public void testGetMtime() throws IOException {
263 System.out.println("getMtime");
264 assertEquals(din.getMtime(), 1193780332);
268 * Test getChunkLength method.
270 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
271 public void testGetChunkLength() throws IOException {
272 System.out.println("getChunkLength");
273 assertEquals(din.getChunkLength(), 58315);
277 * Test getChunkCount method.
279 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
280 public void testGetChunkCount() throws IOException {
281 System.out.println("getChunkCount");
282 assertEquals(din.getChunkCount(), 7);
286 * Test getFilename method.
288 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
289 public void testGetFilename() throws IOException {
290 System.out.println("getFilename");
291 assertEquals(din.getFilename(), "results.dict");
295 * Test readFully with large seek
297 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
298 public void testReadFully_seek2() throws Exception {
299 System.out.println("readFully_seek2");
304 } catch (IOException ioe) {
305 fail("Unexpected IOException");
307 byte[] buf = new byte[len];
310 } catch (EOFException eofe) {
311 fail("Unexpected EOF");
316 * Test with large seek
318 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
319 public void testRead_seek2() throws Exception {
320 System.out.println("read_seek2");
325 } catch (IOException ioe) {
326 fail("Unexpected IOException");
328 byte[] buf = new byte[len];
331 result = din.read(buf, 0, len);
332 } catch (EOFException eofe) {
333 fail("Unexpected EOF");
335 assertEquals(result, len);
338 * Test with large seek comparison content
340 @Test (groups = "test", dependsOnMethods = { "testConstructor"})
341 public void testRead_seek3() throws Exception {
342 System.out.println("read_seek2");
347 } catch (IOException ioe) {
348 fail("Unexpected IOException");
350 FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
352 assertTrue(contentEquals(din, in2, 0, len));