OSDN Git Service

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