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.IOException;
47 import java.util.Arrays;
51 * Test of DictZipInputStream.
52 * @author Hiroshi Miura
54 public class DictZipInputStreamTest {
56 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
57 private DictZipInputStream din;
60 public void rewind() throws Exception {
70 @Test (groups = "init", dependsOnMethods = { "testConstructor_fromFilename",
71 "testConstructor_defaultBufSize"})
72 public void testConstructor() throws Exception {
73 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534);
80 @Test (groups = "init")
81 public void testConstructor_defaultBufSize() throws Exception {
82 din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"));
86 * Test constructor from filename.
89 @Test (groups = "init")
90 public void testConstructor_fromFilename() throws Exception {
91 din = new DictZipInputStream(dataFile);
95 * Test close of stream.
96 * @throws Exception if I/O error occurred.
98 @Test (dependsOnGroups = { "test" })
99 public void testClose() throws Exception {
104 * Test of read method, of class DictZipInputStream.
106 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
107 public void testRead() throws Exception {
108 System.out.println("read");
110 byte[] buf = new byte[len];
111 byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
112 din.read(buf, 0, len);
113 assertTrue(Arrays.equals(expResult, buf));
117 * Test of read method, of class DictZipInputStream.
119 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
120 public void testRead_with_seek() throws Exception {
121 System.out.println("read with seek");
125 byte[] buf = new byte[len];
127 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
129 din.read(buf, 0, len);
130 assertTrue(Arrays.equals(buf, expResult));
134 * Test of read method, of class DictZipInputStream.
136 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
137 public void testRead_null() throws Exception {
138 System.out.println("read null buffer");
143 din.read(buf, 0, len);
144 fail("Should be throw exception.");
145 } catch (NullPointerException e) {
149 assertTrue(r, "Got NullPointerException when buffer is null");
153 * Test of read method, of class DictZipInputStream.
155 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
156 public void testRead_outOfBound() throws Exception {
157 System.out.println("read out of buffer size");
159 byte[] buf = new byte[len];
162 din.read(buf, 0, len + 10);
163 fail("Should be throw exception.");
164 } catch (IndexOutOfBoundsException e) {
168 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
172 * Test of read method, of class DictZipInputStream.
174 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
175 public void testRead_zeroSize() throws Exception {
176 System.out.println("read zero size");
178 byte[] buf = new byte[len];
179 int size = din.read(buf, 0, 0);
180 throw new SkipException("Unknown bug.");
181 //assertEquals(size, 0);
185 * Test of read method, of class DictZipInputStream.
187 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
188 public void testRead_with_seekLast() throws Exception {
189 System.out.println("read with seek to last");
193 byte[] buf = new byte[len];
194 din.read(buf, 0, len);
195 int result = din.read(buf, 0, len);
196 assertEquals(result, -1);
200 * Test of readFully method, of class DictZipInputStream.
201 * @throws java.lang.Exception
203 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
204 public void testReadFully_byteArr() throws Exception {
205 System.out.println("readFully");
209 byte[] buf = new byte[len];
214 * Test of readFully method, of class DictZipInputStream.
215 * @throws java.lang.Exception
217 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
218 public void testReadFully_3args() throws Exception {
219 System.out.println("readFully");
223 byte[] buf = new byte[len];
224 din.readFully(buf, 0, len);
228 * Test of readFully method, of class DictZipInputStream.
229 * @throws java.lang.Exception
231 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
232 public void testReadFully_readTrailer() throws IOException {
233 System.out.println("readFully and readTrailer");
234 byte[] buf = new byte[512];
237 } catch (EOFException e) {
242 assertEquals(din.getCrc(), 0x024d1f37);
243 assertEquals(din.getLength(), 383783);
247 * Test getComplength method.
248 * @throws IOException
250 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
251 public void testGetCompLength() throws IOException {
252 System.out.println("getCompLength");
253 assertEquals(din.getCompLength(), 136856);
257 * Test getMtime method.
259 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
260 public void testGetMtime() throws IOException {
261 System.out.println("getMtime");
262 assertEquals(din.getMtime(), 1193780332);
266 * Test getChunkLength method.
268 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
269 public void testGetChunkLength() throws IOException {
270 System.out.println("getChunkLength");
271 assertEquals(din.getChunkLength(), 58315);
275 * Test getChunkCount method.
277 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
278 public void testGetChunkCount() throws IOException {
279 System.out.println("getChunkCount");
280 assertEquals(din.getChunkCount(), 7);
284 * Test getFilename method.
286 @Test (groups = "test", dependsOnMethods = { "testConstructor" })
287 public void testGetFilename() throws IOException {
288 System.out.println("getFilename");
289 assertEquals(din.getFilename(), "results.dict");