OSDN Git Service

Reorganize code structures
[dictzip-java/dictzip-java.git] / dictzip-cli / src / test / java / org / dict / zip / cli / DictDataTest.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
21 package org.dict.zip.cli;
22
23
24 import java.io.File;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Objects;
28
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;
34
35
36 /**
37  * DictData test.
38  * @author Hiroshi Miura
39  */
40 public class DictDataTest {
41     /**
42      * Test of printHeader method, of class DictData.
43      * @throws java.lang.Exception if file operation failed.
44      */
45     @Test
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();
50     }
51
52     /**
53      * Test of doZip method, of class DictData.
54      * @param tempDir JUnit5 temporary directory support
55      * @throws java.lang.Exception if file operation failed.
56      */
57     @Test
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
69         // 2. after crc.
70         assertTrue(contentEquals(zippedFile.toFile(), expectFile.toFile(), 9, 14));
71         assertTrue(contentEquals(zippedFile.toFile(), expectFile.toFile(), 39, 512));
72     }
73
74     /**
75      * Test of doUnzip method, of class DictData.
76      * @param tempDir JUnit5 temporary directory support
77      * @throws java.lang.Exception if file operation failed.
78      */
79     @Test
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");
83         long start = 0L;
84         int size = 0;
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)));
89     }
90
91 }