OSDN Git Service

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