OSDN Git Service

Merge remote-tracking branch 'origin/master' into topic-test-large-file
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / DictZipFileUtilsTest.java
1 /*
2  * DictZip Library test.
3  *
4  * Copyright (C) 2016,2019 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  * Linking this library statically or dynamically with other modules is
21  * making a combined work based on this library.  Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * As a special exception, the copyright holders of this library give you
26  * permission to link this library with independent modules to produce an
27  * executable, regardless of the license terms of these independent
28  * modules, and to copy and distribute the resulting executable under
29  * terms of your choice, provided that you also meet, for each linked
30  * independent module, the terms and conditions of the license of that
31  * module.  An independent module is a module which is not derived from
32  * or based on this library.  If you modify this library, you may extend
33  * this exception to your version of the library, but you are not
34  * obligated to do so.  If you do not wish to do so, delete this
35  * exception statement from your version.
36  */
37 package org.dict.zip;
38
39
40 import org.junit.jupiter.api.Test;
41
42 import java.io.File;
43 import java.io.IOException;
44
45 import static org.junit.jupiter.api.Assertions.assertFalse;
46 import static org.junit.jupiter.api.Assertions.assertTrue;
47 import static tokyo.northside.io.FileUtils2.contentEquals;
48
49 /**
50  * Created by Hiroshi Miura on 16/04/09.
51  */
52 public class DictZipFileUtilsTest {
53     /**
54      * Check equals two file contentss.
55      * @throws Exception when fails to open.
56      */
57     public void testContentEquals() throws Exception {
58         System.out.println("isFileBinaryEquals");
59         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
60         File secondFile = new File(this.getClass().getResource("/test_util1.txt").getFile());
61         assertTrue(contentEquals(firstFile, secondFile));
62     }
63
64     /**
65      * Check equals two file paths which is canonical path.
66      * @throws Exception when fails to open.
67      */
68     @Test
69     public void testContentEqualsSameCanonicalPath() throws Exception {
70         System.out.println("isFileBinaryEquals with same canonical path");
71         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
72         File secondFile = new File(this.getClass().getResource("/test_util.txt").getFile());
73         assertTrue(contentEquals(firstFile, secondFile));
74     }
75
76     /**
77      * Check not equals two file contents.
78      * @throws Exception when fails to open.
79      */
80     @Test
81     public void testContentEqualsFalse() throws Exception {
82         System.out.println("isFileBinaryEquals_false");
83         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
84         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
85         assertFalse(contentEquals(firstFile, secondFile));
86     }
87
88     /**
89      * Check equals tow files content with range.
90      * @throws Exception when fails to open.
91      */
92     @Test
93     public void testContentEqualsRange() throws Exception {
94         System.out.println("isFileBinaryEquals_range");
95         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
96         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
97         assertTrue(contentEquals(firstFile, secondFile, 10, 64));
98     }
99
100     /**
101      * Check not equals two file contents with range.
102      * @throws Exception when fails to open.
103      */
104     @Test
105     public void testContentEqualsRangeFalse() throws Exception {
106         System.out.println("isFileBinaryEquals_range_false");
107         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
108         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
109         assertFalse(contentEquals(firstFile, secondFile, 0, 64));
110     }
111
112     /**
113      * Check dictzip inputstream.
114      * @throws Exception when fails.
115      */
116     @Test
117     public void testCheckDictZipInputStreamString() throws Exception {
118         System.out.println("checkDictZipInputStream_string");
119         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
120         assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
121     }
122
123     /**
124      * Check dictzip input streasm which is not exist.
125      * @throws Exception when fails.
126      */
127     @Test
128     public void testCheckDictZipInputStreamStringNoExist() throws Exception {
129         System.out.println("checkDictZipInputStream_string");
130         String targetFile = "false.dict.dz";
131         boolean result;
132         try {
133             DictZipFileUtils.checkDictZipInputStream(targetFile);
134             result = false;
135         } catch (IOException e) {
136             // expected.
137             result = true;
138         }
139         assertTrue(result);
140     }
141
142     /**
143      * Check dictzip input stream.
144      * @throws Exception when fails.
145      */
146     @Test
147     public void testCheckDictZipInputStream() throws Exception {
148         System.out.println("checkDictZipInputStream");
149         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
150         try (DictZipInputStream dzin = new DictZipInputStream(new
151                 RandomAccessInputStream(targetFile, "r"))) {
152             assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
153         }
154     }
155 }