2 * DictZip Library test.
4 * Copyright (C) 2016,2019 Hiroshi Miura
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.
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.
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.
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
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.
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.nio.file.Path;
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;
49 import static org.junit.jupiter.api.Assertions.assertEquals;
50 import static org.junit.jupiter.api.Assertions.assertTrue;
54 * Test for DictZip Header.
55 * @author Hiroshi Miura
57 public class DictZipHeaderTest {
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";
66 * Test of readHeader method, of class DictZipHeader.
68 * @throws java.lang.Exception if file I/O error occurred.
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());
78 * Test of readHeader method, of class DictZipHeader.
80 * @throws java.lang.Exception if file I/O error occurred.
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");
90 * Test of readHeader method, of class DictZipHeader.
92 * @throws java.lang.Exception if file I/O error occurred.
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");
102 * Test of readHeader method, of class DictZipHeader.
104 * @throws java.lang.Exception if file I/O error occurred.
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);
114 * Test of readHeader method, of class DictZipHeader.
116 * @param tempDir JUnit5 temporary directory.
117 * @throws java.lang.Exception if file I/O error occurred.
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);
128 DictZipHeader result = DictZipHeader.readHeader(testFile.getAbsolutePath());
129 } catch (IOException ex) {
133 assertTrue(r, "IOException Expected and got");
137 * Test of readHeader method, of class DictZipHeader.
139 * @param tempDir JUnit5 temporary directory.
140 * @throws java.lang.Exception if file I/O error occurred.
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);
151 DictZipHeader result = DictZipHeader.readHeader(testFile.getAbsolutePath());
152 } catch (IOException ex) {
156 assertTrue(r, "IOException Expected and got");
160 * Test of readHeader method on default_compression level file.
162 * @throws java.lang.Exception if file I/O error occurred.
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);
172 * Test of readHeader method on fast_compression level file.
174 * @throws java.lang.Exception if file I/O error occurred.
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);
184 * Test of readHeader method on best_compression level file.
186 * @throws java.lang.Exception if file I/O error occurred.
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);
196 * Test of readHeader method on wrong compression level file.
198 * @throws java.lang.Exception if file I/O error occurred.
201 public void testReaderHeaderWrongCompression() throws Exception {
202 String dataFile = this.getClass().getResource("/corrupt.dict.dz").getFile();
205 DictZipHeader result = DictZipHeader.readHeader(dataFile);
206 } catch (IOException ex) {
210 assertTrue(r, "IOException Expected and got");
214 * Test of getMemberLength method.
215 * @throws Exception when fails.
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);
225 * Test of writeHeader method.
227 * @param tempDir JUnit5 temporary directory.
228 * @throws Exception if file I/O error occurred.
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);
247 String expectedHeader = this.getClass().getResource("/test.header.dz").getFile();
248 assertTrue(FileUtils2.contentEquals(testFile, new File(expectedHeader), 8, 45));
249 testFile.deleteOnExit();