OSDN Git Service

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