OSDN Git Service

b0776f7dc7e0310d5ffc60bd076bdca92d8ac9e3
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / DictZipHeaderTest.java
1 /*
2  * DictZip Library test.
3  *
4  * Copyright (C) 2016,2019 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  * Linking this library statically or dynamically with other modules is
21  * making a combined work based on this library.  Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * As a special exception, the copyright holders of this library give you
26  * permission to link this library with independent modules to produce an
27  * executable, regardless of the license terms of these independent
28  * modules, and to copy and distribute the resulting executable under
29  * terms of your choice, provided that you also meet, for each linked
30  * independent module, the terms and conditions of the license of that
31  * module.  An independent module is a module which is not derived from
32  * or based on this library.  If you modify this library, you may extend
33  * this exception to your version of the library, but you are not
34  * obligated to do so.  If you do not wish to do so, delete this
35  * exception statement from your version.
36  */
37 package org.dict.zip;
38
39 import java.io.File;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.nio.file.Path;
43
44 import org.dict.zip.DictZipHeader.CompressionLevel;
45 import org.junit.jupiter.api.Test;
46 import org.junit.jupiter.api.io.TempDir;
47 import tokyo.northside.io.FileUtils2;
48
49 import static org.junit.jupiter.api.Assertions.assertEquals;
50 import static org.junit.jupiter.api.Assertions.assertTrue;
51
52
53 /**
54  * Test for DictZip Header.
55  * @author Hiroshi Miura
56  */
57 public class DictZipHeaderTest {
58
59     private String toStringExpResult() {
60         return "\nHeader length = 49" + "\nSubfield ID = RA" + "\nSubfield length = 20"
61                + "\nSubfield version = 1" + "\nChunk length = 58315" + "\nNumber of chunks = 7"
62                + "\nLength of member = 136856";
63     }
64
65     /**
66      * Test of readHeader method, of class DictZipHeader.
67      *
68      * @throws java.lang.Exception if file I/O error occurred.
69      */
70     @Test
71     public void testReadHeaderString() throws Exception {
72         String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
73         DictZipHeader result = DictZipHeader.readHeader(dataFile);
74         assertEquals(result.toString(), toStringExpResult());
75     }
76
77     /**
78      * Test of readHeader method, of class DictZipHeader.
79      *
80      * @throws java.lang.Exception if file I/O error occurred.
81      */
82     @Test
83     public void testReadHeaderType() throws Exception {
84         String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
85         DictZipHeader result = DictZipHeader.readHeader(dataFile);
86         assertEquals(result.getType(), "dzip");
87     }
88
89     /**
90      * Test of readHeader method, of class DictZipHeader.
91      *
92      * @throws java.lang.Exception if file I/O error occurred.
93      */
94     @Test
95     public void testReadHeaderFilename() throws Exception {
96         String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
97         DictZipHeader result = DictZipHeader.readHeader(dataFile);
98         assertEquals(result.getFilename(), "results.dict");
99     }
100
101     /**
102      * Test of readHeader method, of class DictZipHeader.
103      *
104      * @throws java.lang.Exception if file I/O error occurred.
105      */
106     @Test
107     public void testReadHeaderChunkCount() throws Exception {
108         String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
109         DictZipHeader result = DictZipHeader.readHeader(dataFile);
110         assertEquals(result.getChunkCount(), 7);
111     }
112
113    /**
114      * Test of readHeader method, of class DictZipHeader.
115      *
116     * @param tempDir JUnit5 temporary directory.
117     * @throws java.lang.Exception if file I/O error occurred.
118      */
119     @Test
120     public void testReadHeaderNonGZip(@TempDir final Path tempDir) throws Exception {
121         byte[] b = {3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w'};
122         File testFile = tempDir.resolve("DictZipOutCon.txt.dz").toFile();
123         FileOutputStream outFile = new FileOutputStream(testFile);
124         outFile.write(b);
125         outFile.close();
126         boolean r = false;
127         try {
128             DictZipHeader result = DictZipHeader.readHeader(testFile.getAbsolutePath());
129         } catch (IOException ex) {
130             // expected
131             r = true;
132         }
133         assertTrue(r, "IOException Expected and got");
134     }
135
136     /**
137      * Test of readHeader method, of class DictZipHeader.
138      *
139      * @param tempDir JUnit5 temporary directory.
140      * @throws java.lang.Exception if file I/O error occurred.
141      */
142     @Test
143     public void testReadHeaderGZipMagic(@TempDir final Path tempDir) throws Exception {
144         byte[] b = {(byte) 0x1f, (byte) 0x8b, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w'};
145         File testFile = tempDir.resolve("DictZipOutCon.txt.dz").toFile();
146         FileOutputStream outFile = new FileOutputStream(testFile);
147         outFile.write(b);
148         outFile.close();
149         boolean r = false;
150         try {
151             DictZipHeader result = DictZipHeader.readHeader(testFile.getAbsolutePath());
152         } catch (IOException ex) {
153             // expected
154             r = true;
155         }
156         assertTrue(r, "IOException Expected and got");
157     }
158
159     /**
160      * Test of readHeader method on default_compression level file.
161      *
162      * @throws java.lang.Exception if file I/O error occurred.
163      */
164     @Test
165     public void testReaderHeaderDefaultCompression() throws Exception {
166         String dataFile = this.getClass().getResource("/default.dict.dz").getFile();
167         DictZipHeader result = DictZipHeader.readHeader(dataFile);
168         assertEquals(result.getExtraFlag(), CompressionLevel.DEFAULT_COMPRESSION);
169     }
170
171     /**
172      * Test of readHeader method on fast_compression level file.
173      *
174      * @throws java.lang.Exception if file I/O error occurred.
175      */
176     @Test
177     public void testReaderHeaderFastCompression() throws Exception {
178         String dataFile = this.getClass().getResource("/fast.dict.dz").getFile();
179         DictZipHeader result = DictZipHeader.readHeader(dataFile);
180         assertEquals(result.getExtraFlag(), CompressionLevel.BEST_SPEED);
181     }
182
183     /**
184      * Test of readHeader method on best_compression level file.
185      *
186      * @throws java.lang.Exception if file I/O error occurred.
187      */
188     @Test
189     public void testReaderHeaderBestCompression() throws Exception {
190         String dataFile = this.getClass().getResource("/best.dict.dz").getFile();
191         DictZipHeader result = DictZipHeader.readHeader(dataFile);
192         assertEquals(result.getExtraFlag(), CompressionLevel.BEST_COMPRESSION);
193     }
194
195     /**
196      * Test of readHeader method on wrong compression level file.
197      *
198      * @throws java.lang.Exception if file I/O error occurred.
199      */
200     @Test
201     public void testReaderHeaderWrongCompression() throws Exception {
202         String dataFile = this.getClass().getResource("/corrupt.dict.dz").getFile();
203         boolean r = false;
204         try {
205             DictZipHeader result = DictZipHeader.readHeader(dataFile);
206         } catch (IOException ex) {
207             // expected
208             r = true;
209         }
210         assertTrue(r, "IOException Expected and got");
211     }
212
213     /**
214      * Test of getMemberLength method.
215      * @throws Exception when fails.
216      */
217     @Test
218     public void testGetMemberLength() throws Exception {
219         String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
220         DictZipHeader result = DictZipHeader.readHeader(dataFile);
221         assertEquals(result.getMemberLength(), 136856);
222     }
223
224     /**
225      * Test of writeHeader method.
226      *
227      * @param tempDir JUnit5 temporary directory.
228      * @throws Exception if file I/O error occurred.
229      */
230     @Test
231     public void testWriteHeader(@TempDir final Path tempDir) throws Exception {
232         File testFile = tempDir.resolve("DictZipHeader.dz").toFile();
233         FileOutputStream outFile = new FileOutputStream(testFile);
234         DictZipHeader header = new DictZipHeader(1024, 256);
235         header.setExtraFlag(CompressionLevel.BEST_COMPRESSION);
236         header.setComment("test comment");
237         header.setFilename("test.dict");
238         header.setHeaderOS(DictZipHeader.OperatingSystem.AMIGA);
239         header.setMtime(System.currentTimeMillis() / 1000);
240         header.setHeaderCRC(true);
241         header.chunks[0] = 55;
242         header.chunks[1] = 55 + 100;
243         header.chunks[2] = 55 + 100 + 128;
244         header.chunks[3] = 55 + 100 + 128 + 198; // 512
245         DictZipHeader.writeHeader(header, outFile);
246         outFile.close();
247         String expectedHeader = this.getClass().getResource("/test.header.dz").getFile();
248         assertTrue(FileUtils2.contentEquals(testFile, new File(expectedHeader), 8, 45));
249         testFile.deleteOnExit();
250     }
251 }