OSDN Git Service

Update tests
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / DictZipInputStreamTest.java
1 /*
2  * DictZip library test.
3  *
4  * Copyright (C) 2016 Hiroshi Miura
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 package org.dict.zip;
22
23 import static org.testng.Assert.*;
24
25 import org.testng.SkipException;
26 import org.testng.annotations.AfterTest;
27 import org.testng.annotations.BeforeTest;
28 import org.testng.annotations.Test;
29
30 import java.io.EOFException;
31 import java.util.Arrays;
32
33
34 /**
35  * Test of DictZipInputStream.
36  * @author Hiroshi Miura
37  */
38 public class DictZipInputStreamTest {
39
40     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
41     private RandomAccessInputStream in;
42     private DictZipInputStream din;
43     private DictZipHeader header;
44
45     /**
46      * Open output stream.
47      * @throws Exception
48      */
49     @BeforeTest
50     public void setUp() throws Exception {
51         in = new RandomAccessInputStream(dataFile, "r");
52         din = new DictZipInputStream(in);
53     }
54
55     /**
56      * Close output stream.
57      * @throws Exception if I/O error occured.
58      */
59     @AfterTest
60     public void tearDown() throws Exception {
61         din.close();
62         in.close();
63     }
64
65     /**
66      * Test of read method, of class DictZipInputStream.
67      */
68     @Test
69     public void testRead() throws Exception {
70         System.out.println("read");
71         int len = 10;
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));
76     }
77
78     /**
79      * Test of read method, of class DictZipInputStream.
80      */
81     @Test
82     public void testRead_with_seek() throws Exception {
83         System.out.println("read with seek");
84         int start = 0x20;
85         int len = 10;
86         din.seek(start);
87         byte[] buf = new byte[len];
88         byte[] expResult = {
89             0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
90         };
91         din.read(buf, 0, len);
92         assertTrue(Arrays.equals(buf, expResult));
93     }
94
95     /**
96      * Test of read method, of class DictZipInputStream.
97      */
98     @Test
99     public void testRead_null() throws Exception {
100         System.out.println("read null buffer");
101         int len = 10;
102         byte[] buf = null;
103         boolean r = false;
104         try {
105             din.read(buf, 0, len);
106             fail("Should be throw exception.");
107         } catch (NullPointerException e) {
108             // expected.
109             r = true;
110         }
111         assertTrue(r, "Got NullPointerException when buffer is null");
112     }
113
114     /**
115      * Test of read method, of class DictZipInputStream.
116      */
117     @Test
118     public void testRead_outOfBound() throws Exception {
119         System.out.println("read out of buffer size");
120         int len = 10;
121         byte[] buf = new byte[len];
122         boolean r = false;
123         try {
124             din.read(buf, 0, len + 10);
125             fail("Should be throw exception.");
126         } catch (IndexOutOfBoundsException e) {
127             // expected.
128             r = true;
129         }
130         assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
131     }
132
133     /**
134      * Test of read method, of class DictZipInputStream.
135      */
136     @Test
137     public void testRead_zeroSize() throws Exception {
138         System.out.println("read zero size");
139         int len = 512;
140         byte[] buf = new byte[len];
141         int size = din.read(buf, 0, 0);
142         throw new SkipException("Unknown bug.");
143         //assertEquals(size, 0);
144     }
145
146     /**
147      * Test of read method, of class DictZipInputStream.
148      */
149     @Test
150     public void testRead_with_seekLast() throws Exception {
151         System.out.println("read with seek to last");
152         int start = 383273;
153         int len = 512;
154         din.seek(start);
155         byte[] buf = new byte[len];
156         din.read(buf, 0, len);
157         int result = din.read(buf, 0, len);
158         assertEquals(result, -1);
159     }
160
161    /**
162      * Test of readFully method, of class DictZipInputStream.
163      * @throws java.lang.Exception
164      */
165     @Test
166     public void testReadFully_byteArr() throws Exception {
167         System.out.println("readFully");
168         int start = 1;
169         int len = 10;
170         in.seek(start);
171         byte[] buf = new byte[len];
172         din.readFully(buf);
173     }
174
175     /**
176      * Test of readFully method, of class DictZipInputStream.
177      * @throws java.lang.Exception
178      */
179     @Test
180     public void testReadFully_3args() throws Exception {
181         System.out.println("readFully");
182         int start = 1;
183         int len = 10;
184         in.seek(start);
185         byte[] buf = new byte[len];
186         din.readFully(buf, 0, len);
187     }
188
189     /**
190      * Test of readFully method, of class DictZipInputStream.
191      * @throws java.lang.Exception
192      */
193     @Test
194     public void testReadFully_checkTrailer() throws Exception {
195         System.out.println("readFully and checkTrailer");
196         byte[] buf = new byte[512];
197         try {
198             din.readFully(buf);
199         } catch (RuntimeException e) {
200             throw e;
201         } catch (EOFException e) {
202             // continue
203         }
204         // read trailer
205         try {
206             din.readTrailer();
207         } catch (RuntimeException e) {
208             throw e;
209         }
210         assertEquals(din.getCrc(), 0x024d1f37);
211         assertEquals(din.getLength(), 383783);
212     }
213 }