OSDN Git Service

Publish checkTrailer
[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 import org.testng.annotations.AfterTest;
25 import org.testng.annotations.BeforeTest;
26 import org.testng.annotations.Test;
27
28 import java.io.EOFException;
29 import java.io.IOException;
30 import java.util.Arrays;
31
32
33 /**
34  * Test of DictZipInputStream.
35  * @author Hiroshi Miura
36  */
37 public class DictZipInputStreamTest {
38
39     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
40     private RandomAccessInputStream in;
41     private DictZipInputStream din;
42     private DictZipHeader header;
43
44     /**
45      * Open output stream.
46      * @throws Exception
47      */
48     @BeforeTest
49     public void setUp() throws Exception {
50         in = new RandomAccessInputStream(dataFile, "r");
51         din = new DictZipInputStream(in);
52     }
53
54     /**
55      * Close output stream.
56      * @throws Exception if I/O error occured.
57      */
58     @AfterTest
59     public void tearDown() throws Exception {
60         din.close();
61         in.close();
62     }
63
64     private synchronized void getDZHeader(DictZipInputStream din) throws IOException {
65         header = din.readHeader();
66     }
67
68     /**
69      * Test of read method, of class DictZipInputStream.
70      */
71     @Test
72     public void testRead() throws Exception {
73         System.out.println("read");
74         int len = 10;
75         getDZHeader(din);
76         byte[] buf = new byte[len];
77         byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
78         din.read(buf, 0, len);
79         assertTrue(Arrays.equals(expResult, buf));
80     }
81
82     /**
83      * Test of read method, of class DictZipInputStream.
84      */
85     @Test
86     public void testRead_with_seek() throws Exception {
87         System.out.println("read with seek");
88         int start = 0x20;
89         int len = 10;
90         getDZHeader(din);
91         din.seek(start);
92         byte[] buf = new byte[len];
93         byte[] expResult = {
94             0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
95         };
96         din.read(buf, 0, len);
97         assertTrue(Arrays.equals(buf, expResult));
98     }
99
100     /**
101      * Test of readFully method, of class DictZipInputStream.
102      * @throws java.lang.Exception
103      */
104     @Test
105     public void testReadFully_byteArr() throws Exception {
106         System.out.println("readFully");
107         int start = 1;
108         int len = 10;
109         getDZHeader(din);
110         int off = header.getOffset(start);
111         long pos = header.getPosition(start);
112         in.seek(pos);
113         byte[] buf = new byte[off + len];
114         din.readFully(buf);
115     }
116
117     /**
118      * Test of readFully method, of class DictZipInputStream.
119      * @throws java.lang.Exception
120      */
121     @Test
122     public void testReadFully_3args() throws Exception {
123         System.out.println("readFully");
124         int start = 1;
125         int len = 10;
126         getDZHeader(din);
127         int off = header.getOffset(start);
128         long pos = header.getPosition(start);
129         in.seek(pos);
130         byte[] buf = new byte[off + len];
131         din.readFully(buf, off, len);
132     }
133
134
135     /**
136      * Test of readFully method, of class DictZipInputStream.
137      * @throws java.lang.Exception
138      */
139     @Test
140     public void testReadFully_checkTrailer() throws Exception {
141         System.out.println("readFully and checkTrailer");
142         getDZHeader(din);
143         byte[] buf = new byte[512];
144         try {
145             din.readFully(buf);
146         } catch (RuntimeException e) {
147             throw e;
148         } catch (EOFException e) {
149             // continue
150         }
151         // read trailer
152         try {
153             din.readTrailer();
154         } catch (RuntimeException e) {
155             throw e;
156         }
157         assertEquals(din.getCrc(), 0x024d1f37);
158         assertEquals(din.getLength(), 383783);
159     }
160
161    /**
162      * Test of readHeader method, of class DictZipInputStream.
163      * @throws java.lang.Exception
164      */
165     @Test
166     public void testReadHeader() throws Exception {
167         System.out.println("readHeader");
168         header = din.readHeader();
169         StringBuilder sb = new StringBuilder();
170         sb.append("\nHeader length = 49");
171         sb.append("\nSubfield ID = RA");
172         sb.append("\nSubfield length = 20");
173         sb.append("\nSubfield version = 1");
174         sb.append("\nChunk length = 58315");
175         sb.append("\nNumber of chunks = 7");
176         String expResult = sb.toString();
177         assertEquals(header.toString(), expResult);
178     }
179
180 }