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.AfterTest;
27 import org.testng.annotations.BeforeTest;
28 import org.testng.annotations.Test;
30 import java.io.EOFException;
31 import java.util.Arrays;
35 * Test of DictZipInputStream.
36 * @author Hiroshi Miura
38 public class DictZipInputStreamTest {
40 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
41 private RandomAccessInputStream in;
42 private DictZipInputStream din;
43 private DictZipHeader header;
50 public void setUp() throws Exception {
51 in = new RandomAccessInputStream(dataFile, "r");
52 din = new DictZipInputStream(in);
56 * Close output stream.
57 * @throws Exception if I/O error occured.
60 public void tearDown() throws Exception {
66 * Test of read method, of class DictZipInputStream.
69 public void testRead() throws Exception {
70 System.out.println("read");
72 byte[] buf = new byte[len];
73 byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
74 din.read(buf, 0, len);
75 assertTrue(Arrays.equals(expResult, buf));
79 * Test of read method, of class DictZipInputStream.
82 public void testRead_with_seek() throws Exception {
83 System.out.println("read with seek");
87 byte[] buf = new byte[len];
89 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
91 din.read(buf, 0, len);
92 assertTrue(Arrays.equals(buf, expResult));
96 * Test of read method, of class DictZipInputStream.
99 public void testRead_null() throws Exception {
100 System.out.println("read null buffer");
105 din.read(buf, 0, len);
106 fail("Should be throw exception.");
107 } catch (NullPointerException e) {
111 assertTrue(r, "Got NullPointerException when buffer is null");
115 * Test of read method, of class DictZipInputStream.
118 public void testRead_outOfBound() throws Exception {
119 System.out.println("read out of buffer size");
121 byte[] buf = new byte[len];
124 din.read(buf, 0, len + 10);
125 fail("Should be throw exception.");
126 } catch (IndexOutOfBoundsException e) {
130 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
134 * Test of read method, of class DictZipInputStream.
137 public void testRead_zeroSize() throws Exception {
138 System.out.println("read zero size");
140 byte[] buf = new byte[len];
141 int size = din.read(buf, 0, 0);
142 throw new SkipException("Unknown bug.");
143 //assertEquals(size, 0);
147 * Test of read method, of class DictZipInputStream.
150 public void testRead_with_seekLast() throws Exception {
151 System.out.println("read with seek to last");
155 byte[] buf = new byte[len];
156 din.read(buf, 0, len);
157 int result = din.read(buf, 0, len);
158 assertEquals(result, -1);
162 * Test of readFully method, of class DictZipInputStream.
163 * @throws java.lang.Exception
166 public void testReadFully_byteArr() throws Exception {
167 System.out.println("readFully");
171 byte[] buf = new byte[len];
176 * Test of readFully method, of class DictZipInputStream.
177 * @throws java.lang.Exception
180 public void testReadFully_3args() throws Exception {
181 System.out.println("readFully");
185 byte[] buf = new byte[len];
186 din.readFully(buf, 0, len);
190 * Test of readFully method, of class DictZipInputStream.
191 * @throws java.lang.Exception
194 public void testReadFully_checkTrailer() throws Exception {
195 System.out.println("readFully and checkTrailer");
196 byte[] buf = new byte[512];
199 } catch (RuntimeException e) {
201 } catch (EOFException e) {
207 } catch (RuntimeException e) {
210 assertEquals(din.getCrc(), 0x024d1f37);
211 assertEquals(din.getLength(), 383783);