OSDN Git Service

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