OSDN Git Service

Update javadoc
[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         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
59         File secondFile = new File(this.getClass().getResource("/test_util1.txt").getFile());
60         assertTrue(contentEquals(firstFile, secondFile));
61     }
62
63     /**
64      * Check equals two file paths which is canonical path.
65      * @throws Exception when fails to open.
66      */
67     @Test
68     public void testContentEqualsSameCanonicalPath() throws Exception {
69         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
70         File secondFile = new File(this.getClass().getResource("/test_util.txt").getFile());
71         assertTrue(contentEquals(firstFile, secondFile));
72     }
73
74     /**
75      * Check not equals two file contents.
76      * @throws Exception when fails to open.
77      */
78     @Test
79     public void testContentEqualsFalse() throws Exception {
80         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
81         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
82         assertFalse(contentEquals(firstFile, secondFile));
83     }
84
85     /**
86      * Check equals tow files content with range.
87      * @throws Exception when fails to open.
88      */
89     @Test
90     public void testContentEqualsRange() throws Exception {
91         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
92         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
93         assertTrue(contentEquals(firstFile, secondFile, 10, 64));
94     }
95
96     /**
97      * Check not equals two file contents with range.
98      * @throws Exception when fails to open.
99      */
100     @Test
101     public void testContentEqualsRangeFalse() throws Exception {
102         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
103         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
104         assertFalse(contentEquals(firstFile, secondFile, 0, 64));
105     }
106
107     /**
108      * Check dictzip inputstream.
109      * @throws Exception when fails.
110      */
111     @Test
112     public void testCheckDictZipInputStreamString() throws Exception {
113         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
114         assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
115     }
116
117     /**
118      * Check dictzip input streasm which is not exist.
119      */
120     @Test
121     public void testCheckDictZipInputStreamStringNoExist() {
122         String targetFile = "false.dict.dz";
123         boolean result;
124         try {
125             DictZipFileUtils.checkDictZipInputStream(targetFile);
126             result = false;
127         } catch (IOException e) {
128             // expected.
129             result = true;
130         }
131         assertTrue(result);
132     }
133
134     /**
135      * Check dictzip input stream.
136      * @throws Exception when fails.
137      */
138     @Test
139     public void testCheckDictZipInputStream() throws Exception {
140         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
141         try (DictZipInputStream dzin = new DictZipInputStream(new
142                 RandomAccessInputStream(targetFile, "r"))) {
143             assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
144         }
145     }
146 }