OSDN Git Service

Merge pull request #2 from miurahr/on_gradle
[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 package org.dict.zip;
21
22 import java.io.IOException;
23 import junit.framework.TestCase;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27
28 /**
29  * Test of DictZipInputStream.
30  * @author Hiroshi Miura
31  */
32 public class DictZipInputStreamTest extends TestCase {
33
34     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
35     private RandomAccessInputStream in;
36     private DictZipInputStream din;
37     private DictZipHeader header;
38
39     /**
40      * Open output stream.
41      * @throws Exception
42      */
43     @Before
44     @Override
45     public void setUp() throws Exception {
46         in = new RandomAccessInputStream(dataFile, "r");
47         din = new DictZipInputStream(in);
48     }
49
50     /**
51      * Close output stream.
52      * @throws Exception if I/O error occured.
53      */
54     @After
55     @Override
56     public void tearDown() throws Exception {
57         din.close();
58         in.close();
59     }
60
61     private synchronized void getDZHeader(DictZipInputStream din) throws IOException {
62         if (header == null) {
63             header = din.readHeader();
64         }
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 start = 1;
74         int len = 10;
75         getDZHeader(din);
76         int off = header.getOffset(start);
77         long pos = header.getPosition(start);
78         in.seek(pos);
79         byte[] buf = new byte[off + len];
80         int expResult = len;
81         int result = din.read(buf, off, len);
82         assertEquals(expResult, result);
83     }
84
85     /**
86      * Test of readFully method, of class DictZipInputStream.
87      * @throws java.lang.Exception
88      */
89     @Test
90     public void testReadFully_byteArr() throws Exception {
91         System.out.println("readFully");
92         int start = 1;
93         int len = 10;
94         getDZHeader(din);
95         int off = header.getOffset(start);
96         long pos = header.getPosition(start);
97         in.seek(pos);
98         byte[] buf = new byte[off + len];
99         din.readFully(buf);
100     }
101
102     /**
103      * Test of readFully method, of class DictZipInputStream.
104      * @throws java.lang.Exception
105      */
106     @Test
107     public void testReadFully_3args() throws Exception {
108         System.out.println("readFully");
109         int start = 1;
110         int len = 10;
111         getDZHeader(din);
112         int off = header.getOffset(start);
113         long pos = header.getPosition(start);
114         in.seek(pos);
115         byte[] buf = new byte[off + len];
116         din.readFully(buf, off, len);
117     }
118
119     /**
120      * Test of readHeader method, of class DictZipInputStream.
121      * @throws java.lang.Exception
122      */
123     @Test
124     public void testReadHeader() throws Exception {
125         System.out.println("readHeader");
126         header = din.readHeader();
127         StringBuilder sb = new StringBuilder();
128         sb.append("\nHeader length = 49");
129         sb.append("\nSubfield ID = RA");
130         sb.append("\nSubfield length = 20");
131         sb.append("\nSubfield version = 1");
132         sb.append("\nChunk length = 58315");
133         sb.append("\nNumber of chunks = 7");
134         String expResult = sb.toString();
135         assertEquals(expResult, header.toString());
136     }
137
138 }