-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileHelperTest.java
151 lines (116 loc) · 6.14 KB
/
FileHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2018 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.spreadsheet.helpers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
/**
* @author Philippe Charles
*/
public class FileHelperTest {
@Test
@SuppressWarnings("null")
public void testHasExtensionFromFile() {
File f = Paths.get("hello.xml").toFile();
assertThat(FileHelper.hasExtension(f)).isFalse();
assertThat(FileHelper.hasExtension(f, ".xml")).isTrue();
assertThat(FileHelper.hasExtension(f, ".xml", ".zip")).isTrue();
assertThat(FileHelper.hasExtension(f, ".zip", ".xml")).isTrue();
assertThat(FileHelper.hasExtension(f, ".zip", ".txt")).isFalse();
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasExtension((File) null, ".xml"));
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasExtension(f, (String[]) null));
}
@Test
@SuppressWarnings("null")
public void testHasExtensionFromPath() {
Path f = Paths.get("hello.xml");
assertThat(FileHelper.hasExtension(f)).isFalse();
assertThat(FileHelper.hasExtension(f, ".xml")).isTrue();
assertThat(FileHelper.hasExtension(f, ".xml", ".zip")).isTrue();
assertThat(FileHelper.hasExtension(f, ".zip", ".xml")).isTrue();
assertThat(FileHelper.hasExtension(f, ".zip", ".txt")).isFalse();
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasExtension((Path) null, ".xml"));
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasExtension(f, (String[]) null));
}
@Test
@SuppressWarnings("null")
public void testHasMagicNumberFromFile(@TempDir Path temp) throws IOException {
byte[] magic = "abc".getBytes(UTF_8);
File file = Files.createFile(temp.resolve("hello.bin")).toFile();
Files.write(file.toPath(), magic);
assertThat(FileHelper.hasMagicNumber(file, magic)).isTrue();
Files.write(file.toPath(), "".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file.toPath(), "abcdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isTrue();
Files.write(file.toPath(), "bcdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file.toPath(), "ab".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file.toPath(), "abdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file.toPath(), "xxx".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, "".getBytes(UTF_8))).isTrue();
Files.write(file.toPath(), "".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, "".getBytes(UTF_8))).isTrue();
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasMagicNumber((File) null, magic));
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasMagicNumber(file, null));
}
@Test
@SuppressWarnings("null")
public void testHasMagicNumberFromPath(@TempDir Path temp) throws IOException {
byte[] magic = "abc".getBytes(UTF_8);
Path file = Files.createFile(temp.resolve("hello.bin"));
Files.write(file, magic);
assertThat(FileHelper.hasMagicNumber(file, magic)).isTrue();
Files.write(file, "".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file, "abcdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isTrue();
Files.write(file, "bcdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file, "ab".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file, "abdefg".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, magic)).isFalse();
Files.write(file, "xxx".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, "".getBytes(UTF_8))).isTrue();
Files.write(file, "".getBytes(UTF_8));
assertThat(FileHelper.hasMagicNumber(file, "".getBytes(UTF_8))).isTrue();
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasMagicNumber((Path) null, magic));
assertThatNullPointerException().isThrownBy(() -> FileHelper.hasMagicNumber(file, null));
}
@Test
public void testAccept() {
assertThatNullPointerException().isThrownBy(() -> FileHelper.accept(null, path -> false));
assertThatNullPointerException().isThrownBy(() -> FileHelper.accept(Paths.get("").toFile(), null));
assertThat(FileHelper.accept(Paths.get("hello.txt").toFile(), path -> true)).isTrue();
assertThat(FileHelper.accept(Paths.get("hello.txt").toFile(), path -> false)).isFalse();
assertThat(FileHelper.accept(Paths.get("hello.txt").toFile(), path -> {
throw new IOException();
})).isFalse();
// assertThat(FileHelper.accept(Paths.get("mapi16:\\{9054}\\x@y($ddab4c7c)\\0\\Inbox\\at=abc:hello.xml\0").toFile(), path -> true)).isFalse();
}
}