OSDN Git Service

eef7d40ae2d8842c98192b0abe320079e5c43691
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / FileUtils2Test.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 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.Objects;
47
48 import static org.junit.jupiter.api.Assertions.assertFalse;
49 import static org.junit.jupiter.api.Assertions.assertTrue;
50 import static tokyo.northside.io.FileUtils2.contentEquals;
51
52 /**
53  * Created by Hiroshi Miura on 16/04/09.
54  */
55 public class FileUtils2Test {
56
57     /**
58      * Check equals two file contentss.
59      * @throws Exception when fails to open.
60      */
61     @Test
62     public void testContentEquals() throws Exception {
63         Path firstFile = Paths.get(
64                 Objects.requireNonNull(this.getClass().getResource("/test_util.txt")).toURI());
65         Path secondFile = Paths.get(
66                 Objects.requireNonNull(this.getClass().getResource("/test_util1.txt")).toURI());
67         assertTrue(contentEquals(firstFile, secondFile));
68     }
69
70     /**
71      * Check equals two file paths which is canonical path.
72      * @throws Exception when fails to open.
73      */
74     @Test
75     public void testContentEqualsSameCanonicalPath() throws Exception {
76         Path firstFile = Paths.get(
77                 Objects.requireNonNull(this.getClass().getResource("/test_util.txt")).toURI());
78         Path secondFile = Paths.get(
79                 Objects.requireNonNull(this.getClass().getResource("/test_util.txt")).toURI());
80         assertTrue(contentEquals(firstFile, secondFile));
81     }
82
83     /**
84      * Check not equals two file contents.
85      * @throws Exception when fails to open.
86      */
87     @Test
88     public void testContentEqualsFalse() throws Exception {
89         File firstFile = new File(
90                 Objects.requireNonNull(this.getClass().getResource("/test_util.txt")).getFile());
91         File secondFile = new File(
92                 Objects.requireNonNull(this.getClass().getResource("/test_util2.txt")).getFile());
93         assertFalse(contentEquals(firstFile, secondFile));
94     }
95
96     /**
97      * Check equals tow files content with range.
98      * @throws Exception when fails to open.
99      */
100     @Test
101     public void testContentEqualsRange() 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         assertTrue(contentEquals(firstFile, secondFile, 10, 64));
105     }
106
107     /**
108      * Check not equals two file contents with range.
109      * @throws Exception when fails to open.
110      */
111     @Test
112     public void testContentEqualsRangeFalse() throws Exception {
113         File firstFile = new File(this.getClass().getResource("/test_util.txt").getFile());
114         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
115         assertFalse(contentEquals(firstFile, secondFile, 0, 64));
116     }
117
118     /**
119      * Check dictzip inputstream.
120      * @throws Exception when fails.
121      */
122     @Test
123     public void testCheckDictZipInputStreamString() throws Exception {
124         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
125         assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
126     }
127
128     /**
129      * Check dictzip input streasm which is not exist.
130      */
131     @Test
132     public void testCheckDictZipInputStreamStringNoExist() {
133         String targetFile = "false.dict.dz";
134         boolean result;
135         try {
136             DictZipFileUtils.checkDictZipInputStream(targetFile);
137             result = false;
138         } catch (IOException e) {
139             // expected.
140             result = true;
141         }
142         assertTrue(result);
143     }
144
145     /**
146      * Check dictzip input stream.
147      * @throws Exception when fails.
148      */
149     @Test
150     public void testCheckDictZipInputStream() throws Exception {
151         String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
152         try (DictZipInputStream dzin = new DictZipInputStream(new
153                 RandomAccessInputStream(targetFile, "r"))) {
154             assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
155         }
156     }
157 }