2 * DictZip library test.
4 * Copyright (C) 2016 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.
21 package org.dict.zip.cli;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Objects;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static tokyo.northside.io.FileUtils2.contentEquals;
31 import org.dict.zip.DictZipHeader;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.io.TempDir;
38 * @author Hiroshi Miura
40 public class DictDataTest {
42 * Test of printHeader method, of class DictData.
43 * @throws java.lang.Exception if file operation failed.
46 public void testPrintHeader() throws Exception {
47 String testFile = this.getClass().getResource("/test.dict.dz").getFile();
48 DictData instance = new DictData(testFile, null);
49 instance.printHeader();
53 * Test of doZip method, of class DictData.
54 * @param tempDir JUnit5 temporary directory support
55 * @throws java.lang.Exception if file operation failed.
58 public void testDoZip(@TempDir final Path tempDir) throws Exception {
59 Path testFile = Paths.get(Objects.requireNonNull(
60 this.getClass().getResource("/test_dozip.dict")).toURI());
61 Path zippedFile = tempDir.resolve("test_dozip.dict.dz");
62 DictData instance = new DictData(testFile, zippedFile);
63 instance.doZip(DictZipHeader.CompressionLevel.DEFAULT_COMPRESSION);
64 Path expectFile = Paths.get(Objects.requireNonNull(
65 this.getClass().getResource("/test_dozip.dict.dz.expected")).toURI());
66 // There is 7 chunks, so header become 22(basic header length) + 7 * 2(chunks) + 2(CRC)= 38
67 // mtime and crc fields are different on every test, so we compared
68 // 1. after mtime to crc
70 assertTrue(contentEquals(zippedFile.toFile(), expectFile.toFile(), 9, 14));
71 assertTrue(contentEquals(zippedFile.toFile(), expectFile.toFile(), 39, 512));
75 * Test of doUnzip method, of class DictData.
76 * @param tempDir JUnit5 temporary directory support
77 * @throws java.lang.Exception if file operation failed.
80 public void testDoUnzip(@TempDir final Path tempDir) throws Exception {
81 String dzFile = this.getClass().getResource("/test.dict.dz").getFile();
82 Path decompressed = tempDir.resolve("test.dict");
85 DictData instance = new DictData(decompressed.toFile(), new File(dzFile));
86 instance.doUnzip(start, size);
87 String expected = this.getClass().getResource("/test.dict.expected").getFile();
88 assertTrue(contentEquals(decompressed.toFile(), new File(expected)));