2 * DictZip library test.
4 * Copyright (C) 2016 Hiroshi Miura
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.
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.
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.
21 package org.dict.zip.cli;
23 import static org.testng.Assert.assertEquals;
25 import java.io.ByteArrayOutputStream;
29 import org.testng.annotations.Test;
31 import org.apache.commons.io.FileUtils;
35 * @author Hiroshi Miura
37 public class DictDataTest {
39 public DictDataTest() {
42 public static void compareBinary(File f1, File f2) throws Exception {
43 ByteArrayOutputStream d1 = new ByteArrayOutputStream();
44 FileUtils.copyFile(f1, d1);
46 ByteArrayOutputStream d2 = new ByteArrayOutputStream();
47 FileUtils.copyFile(f2, d2);
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]);
58 * Test of printHeader method, of class DictData
59 * @throws java.lang.Exception if file operation failed.
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();
71 * Test of doZip method, of class DictData.
72 * @throws java.lang.Exception if file operation failed.
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);
81 File resultFile = new File(testFile + ".dz");
82 //compareBinary(resultFile, new File("test/data/test2.dict.dz.expected"));
83 resultFile.deleteOnExit();
87 * Test of doUnzip method, of class DictData.
88 * @throws java.lang.Exception if file operation failed.
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);
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();