OSDN Git Service

Gradle build
[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 java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.net.URL;
26
27 import junit.framework.TestCase;
28 import org.junit.Test;
29
30 import org.apache.commons.io.FileUtils;
31
32 /**
33  * DictData test.
34  * @author Hiroshi Miura
35  */
36 public class DictDataTest extends TestCase {
37     
38     public DictDataTest() {
39     }
40
41     public static void compareBinary(File f1, File f2) throws Exception {
42         ByteArrayOutputStream d1 = new ByteArrayOutputStream();
43         FileUtils.copyFile(f1, d1);
44
45         ByteArrayOutputStream d2 = new ByteArrayOutputStream();
46         FileUtils.copyFile(f2, d2);
47
48         assertEquals(d1.size(), d2.size());
49         byte[] a1 = d1.toByteArray();
50         byte[] a2 = d2.toByteArray();
51         for (int i = 0; i < d1.size(); i++) {
52           assertEquals(a1[i], a2[i]);
53         }
54     }
55
56     /**
57      * Test of printHeader method, of class DictData
58      * @throws java.lang.Exception if file operation failed.
59      */
60     @Test
61     public void testPrintHeader() throws Exception {
62         System.out.println("printHeader");
63         URL url = this.getClass().getResource("/test.dict.dz");
64         String testFile = url.getFile();
65         DictData instance = new DictData(testFile, null);
66         instance.printHeader();
67     }
68
69     /**
70      * Test of doZip method, of class DictData.
71      * @throws java.lang.Exception if file operation failed.
72      */
73     @Test
74     public void testDoZip() throws Exception {
75         System.out.println("doZip");
76         String testFile = this.getClass().getResource("/test2.dict").getFile();
77         String zippedFile = DictZipUtils.compressedFileName(testFile);
78         DictData instance = new DictData(testFile, zippedFile);
79         instance.doZip();
80         File resultFile = new File(testFile + ".dz");
81         //compareBinary(resultFile, new File("test/data/test2.dict.dz.expected"));
82         resultFile.deleteOnExit();
83     }
84
85     /**
86      * Test of doUnzip method, of class DictData.
87      * @throws java.lang.Exception if file operation failed.
88      */
89     @Test
90     public void testDoUnzip() throws Exception {
91         System.out.println("doUnzip");
92         URL url = this.getClass().getResource("/test.dict.dz");
93         String dzFile = url.getFile();
94         String file = DictZipUtils.uncompressedFileName(dzFile);
95         long start = 0L;
96         int size = 0;
97         DictData instance = new DictData(file, dzFile);
98         instance.doUnzip(start, size);
99         URL resultUrl = this.getClass().getResource("/test.dict");
100         File resultFile = new File(resultUrl.getFile());
101         URL expectedUrl = this.getClass().getResource("/test.dict.expected");
102         compareBinary(resultFile, new File(expectedUrl.getFile()));
103         resultFile.deleteOnExit();
104     }
105 }