OSDN Git Service

CLI: Implement compression level option
[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 import static org.testng.Assert.assertTrue;
24
25 import java.io.File;
26 import java.net.URL;
27
28 import org.dict.zip.DictZipHeader;
29
30 import org.testng.annotations.Test;
31
32
33 /**
34  * DictData test.
35  * @author Hiroshi Miura
36  */
37 public class DictDataTest {
38     /**
39      * Test of printHeader method, of class DictData
40      * @throws java.lang.Exception if file operation failed.
41      */
42     @Test
43     public void testPrintHeader() throws Exception {
44         System.out.println("printHeader");
45         URL url = this.getClass().getResource("/test.dict.dz");
46         String testFile = url.getFile();
47         DictData instance = new DictData(testFile, null);
48         instance.printHeader();
49     }
50
51     /**
52      * Test of doZip method, of class DictData.
53      * @throws java.lang.Exception if file operation failed.
54      */
55     @Test
56     public void testDoZip() throws Exception {
57         System.out.println("doZip");
58         String testFile = this.getClass().getResource("/test_dozip.dict").getFile();
59         String zippedFile = DictZipUtils.compressedFileName(testFile);
60         DictData instance = new DictData(testFile, zippedFile);
61         instance.doZip(DictZipHeader.CompressionLevel.DEFAULT_COMPRESSION);
62         File resultFile = new File(testFile + ".dz");
63         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected").getFile());
64         assertTrue(StaticUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
65         resultFile.deleteOnExit();
66     }
67
68     /**
69      * Test of doUnzip method, of class DictData.
70      * @throws java.lang.Exception if file operation failed.
71      */
72     @Test
73     public void testDoZip_best() throws Exception {
74         System.out.println("doZip_best");
75         String testFile = this.getClass().getResource("/test_dozip.dict").getFile();
76         String zippedFile = testFile + "_best.dz";
77         DictData instance = new DictData(testFile, zippedFile);
78         instance.doZip(DictZipHeader.CompressionLevel.BEST_COMPRESSION);
79         File resultFile = new File(zippedFile);
80         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected.best")
81                  .getFile());
82         assertTrue(StaticUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
83         resultFile.deleteOnExit();
84     }
85
86     /**
87      * Test of doUnzip method, of class DictData.
88      * @throws java.lang.Exception if file operation failed.
89      */
90     @Test
91     public void testDoZip_fast() throws Exception {
92         System.out.println("doZip_fast");
93         String testFile = this.getClass().getResource("/test_dozip.dict").getFile();
94         String zippedFile = testFile + "_fast.dz";
95         DictData instance = new DictData(testFile, zippedFile);
96         instance.doZip(DictZipHeader.CompressionLevel.BEST_SPEED);
97         File resultFile = new File(zippedFile);
98         File expectFile = new File(this.getClass().getResource("/test_dozip.dict.dz.expected.fast")
99                  .getFile());
100         assertTrue(StaticUtils.isFileBinaryEquals(resultFile, expectFile, 10, 512));
101         resultFile.deleteOnExit();
102     }
103
104     /**
105      * Test of doUnzip method, of class DictData.
106      * @throws java.lang.Exception if file operation failed.
107      */
108     @Test
109     public void testDoUnzip() throws Exception {
110         System.out.println("doUnzip");
111         URL url = this.getClass().getResource("/test.dict.dz");
112         String dzFile = url.getFile();
113         String file = DictZipUtils.uncompressedFileName(dzFile);
114         long start = 0L;
115         int size = 0;
116         DictData instance = new DictData(file, dzFile);
117         instance.doUnzip(start, size);
118         URL resultUrl = this.getClass().getResource("/test.dict");
119         File resultFile = new File(resultUrl.getFile());
120         URL expectedUrl = this.getClass().getResource("/test.dict.expected");
121         assertTrue(StaticUtils.isFileBinaryEquals(resultFile, new File(expectedUrl.getFile())));
122         resultFile.deleteOnExit();
123     }
124 }