-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBookFactoryAssert.java
283 lines (243 loc) · 12.1 KB
/
BookFactoryAssert.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright 2016 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.tck;
import ec.util.spreadsheet.Book;
import ec.util.spreadsheet.helpers.ArrayBook;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.SoftAssertions;
import java.io.*;
import java.net.URL;
import java.nio.file.*;
import java.util.Optional;
import static ec.util.spreadsheet.tck.Assertions.msg;
/**
* @author Philippe Charles
*/
public class BookFactoryAssert extends AbstractAssert<BookFactoryAssert, Book.Factory> {
public BookFactoryAssert(Book.Factory actual) {
super(actual, BookFactoryAssert.class);
}
public static BookFactoryAssert assertThat(Book.Factory actual) {
return new BookFactoryAssert(actual);
}
public BookFactoryAssert isCompliant(SampleSet samples, Path temp) throws IOException {
File valid = samples.getValid().file(temp);
isNotNull();
SoftAssertions s = new SoftAssertions();
assertCompliance(s, actual, valid, Optional.ofNullable(samples.getInvalidFormat()).map(sample -> sample.file(temp)));
s.assertAll();
return this;
}
public static void assertReadWrite(Book.Factory reader, Book.Factory writer, File inputFile, File outputFile) throws IOException {
try (Book original = reader.load(inputFile)) {
writer.store(outputFile, original);
try (Book modified = reader.load(outputFile)) {
BookAssert.assertThat(modified)
.hasSameContentAs(modified, true);
}
}
}
//<editor-fold defaultstate="collapsed" desc="Internal implementation">
// private static final File INVALID_PATH = Paths.get("mapi16:\\{9054}\\x@y($ddab4c7c)\\0\\Inbox\\at=abc:hello.xml\0").toFile();
private static void assertCompliance(SoftAssertions s, Book.Factory factory, File valid, Optional<File> invalid) throws IOException {
s.assertThat(factory.getName()).isNotNull();
s.assertThat(factory.accept(valid)).isTrue();
// s.assertThat(factory.accept(INVALID_PATH)).isFalse();
if (invalid.isPresent()) {
// FIXME: must add better invalid definition
// s.assertThat(factory.accept(invalid.get())).isTrue();
}
s.assertThat(factory.getExtensionsByMediaType())
.isNotNull()
.allSatisfy((key, values) -> {
org.assertj.core.api.Assertions.assertThat(key).isNotNull().isNotBlank();
org.assertj.core.api.Assertions.assertThat(values).isNotNull()
.allSatisfy(value -> org.assertj.core.api.Assertions.assertThat(value).isNotNull().isNotBlank());
});
s.assertThatThrownBy(() -> factory.isSupportedDataType(NULL_CLASS))
.as(msg(factory, "isSupportedDataType(nullClass)", NullPointerException.class))
.isInstanceOf(NullPointerException.class);
if (factory.canLoad()) {
assertLoadNull(s, factory);
assertLoadValid(s, factory, valid);
if (invalid.isPresent()) {
assertLoadInvalid(s, factory, invalid.get());
}
assertLoadEmpty(s, factory);
assertLoadMissing(s, factory);
assertLoadDir(s, factory);
// s.assertThatThrownBy(() -> factory.load(INVALID_PATH))
// .as(msg(factory, "load(invalidPath)", IOException.class))
// .isInstanceOf(IOException.class);
} else {
assertLoadUnsupported(s, factory, valid);
}
if (factory.canStore()) {
// s.assertThatThrownBy(() -> factory.store(INVALID_PATH, ArrayBook.builder().build()))
// .as(msg(factory, "store(invalidPath, book)", IOException.class))
// .isInstanceOf(IOException.class);
}
if (factory.canLoad() && factory.canStore()) {
assertLoadStore(s, factory, valid.toURI().toURL());
}
}
private static void assertLoadNull(SoftAssertions s, Book.Factory factory) {
s.assertThatThrownBy(() -> factory.load(NULL_FILE))
.as(msg(factory, "load(nullFile)", NullPointerException.class))
.isInstanceOf(NullPointerException.class);
s.assertThatThrownBy(() -> factory.load(NULL_INPUT_STREAM))
.as(msg(factory, "load(nullInputStream)", NullPointerException.class))
.isInstanceOf(NullPointerException.class);
s.assertThatThrownBy(() -> factory.load(NULL_PATH))
.as(msg(factory, "load(nullPath)", NullPointerException.class))
.isInstanceOf(NullPointerException.class);
s.assertThatThrownBy(() -> factory.load(NULL_URL))
.as(msg(factory, "load(nullURL)", NullPointerException.class))
.isInstanceOf(NullPointerException.class);
}
private static void assertLoadValid(SoftAssertions s, Book.Factory factory, File valid) throws IOException {
ArrayBook b;
try (Book x = factory.load(valid)) {
BookAssert.assertCompliance(s, x);
b = ArrayBook.copyOf(x);
}
try (InputStream stream = Files.newInputStream(valid.toPath())) {
try (Book x = factory.load(stream)) {
BookAssert.assertContentEquals(s, b, x, true);
}
}
try (Book x = factory.load(valid.toPath())) {
BookAssert.assertContentEquals(s, b, x, true);
}
try (Book x = factory.load(valid.toURI().toURL())) {
BookAssert.assertContentEquals(s, b, x, true);
}
}
private static void assertLoadInvalid(SoftAssertions s, Book.Factory f, File invalidFile) throws IOException {
Class<? extends IOException> invalidException = IOException.class;
s.assertThatThrownBy(() -> {
try (Book ignored = f.load(invalidFile)) {
}
})
.as(msg(f, "load(invalidFile)", invalidException))
.isNotInstanceOf(EOFException.class)
.isInstanceOf(invalidException)
.hasMessageContaining(invalidFile.getName());
try (InputStream stream = Files.newInputStream(invalidFile.toPath())) {
s.assertThatThrownBy(() -> {
try (Book ignored = f.load(stream)) {
}
})
.as(msg(f, "load(invalidStream)", invalidException))
.isNotInstanceOf(EOFException.class)
.isInstanceOf(invalidException);
}
s.assertThatThrownBy(() -> {
try (Book ignored = f.load(invalidFile.toPath())) {
}
})
.as(msg(f, "load(invalidPath)", invalidException))
.isNotInstanceOf(EOFException.class)
.isInstanceOf(invalidException)
.hasMessageContaining(invalidFile.getName());
s.assertThatThrownBy(() -> {
try (Book ignored = f.load(invalidFile.toURI().toURL())) {
}
})
.as(msg(f, "load(invalidURL)", invalidException))
.isNotInstanceOf(EOFException.class)
.isInstanceOf(invalidException);
}
private static void assertLoadEmpty(SoftAssertions s, Book.Factory f) throws IOException {
File empty = File.createTempFile("empty", "file");
s.assertThatThrownBy(() -> f.load(empty))
.as(msg(f, "load(emptyFile)", EOFException.class))
.isInstanceOf(EOFException.class)
.hasMessage(empty.getPath());
try (InputStream stream = Files.newInputStream(empty.toPath())) {
s.assertThatThrownBy(() -> f.load(stream))
.as(msg(f, "load(emptyStream)", EOFException.class))
.isInstanceOf(EOFException.class);
}
s.assertThatThrownBy(() -> f.load(empty.toPath()))
.as(msg(f, "load(emptyPath)", EOFException.class))
.isInstanceOf(EOFException.class)
.hasMessage(empty.getPath());
s.assertThatThrownBy(() -> f.load(empty.toURI().toURL()))
.as(msg(f, "load(emptyURL)", EOFException.class))
.isInstanceOf(EOFException.class);
empty.delete();
}
private static void assertLoadMissing(SoftAssertions s, Book.Factory f) throws IOException {
File missing = File.createTempFile("missing", "file");
missing.delete();
s.assertThatThrownBy(() -> f.load(missing))
.as(msg(f, "load(missingFile)", NoSuchFileException.class))
.isInstanceOf(NoSuchFileException.class)
.hasMessage(missing.getPath());
s.assertThatThrownBy(() -> f.load(missing.toPath()))
.as(msg(f, "load(missingPath)", NoSuchFileException.class))
.isInstanceOf(NoSuchFileException.class)
.hasMessage(missing.getPath());
s.assertThatThrownBy(() -> f.load(missing.toURI().toURL()))
.as(msg(f, "load(missingURL)", NoSuchFileException.class))
.isInstanceOf(NoSuchFileException.class);
}
private static void assertLoadDir(SoftAssertions s, Book.Factory f) throws IOException {
File folder = org.assertj.core.util.Files.newTemporaryFolder();
s.assertThatThrownBy(() -> f.load(folder))
.as(msg(f, "load(folderAsFile)", AccessDeniedException.class))
.isInstanceOf(AccessDeniedException.class)
.hasMessage(folder.getPath());
s.assertThatThrownBy(() -> f.load(folder.toPath()))
.as(msg(f, "load(folderAsPath)", AccessDeniedException.class))
.isInstanceOf(AccessDeniedException.class)
.hasMessage(folder.getPath());
// s.assertThatThrownBy(() -> f.load(folder.toURI().toURL()))
// .as(msg(f, "load(folderAsURL)", AccessDeniedException.class))
// .isInstanceOf(AccessDeniedException.class);
folder.delete();
}
private static void assertLoadUnsupported(SoftAssertions s, Book.Factory f, File valid) throws IOException {
s.assertThatThrownBy(() -> f.load(valid))
.isInstanceOf(UnsupportedOperationException.class);
try (InputStream stream = Files.newInputStream(valid.toPath())) {
s.assertThatThrownBy(() -> f.load(stream))
.isInstanceOf(UnsupportedOperationException.class);
}
s.assertThatThrownBy(() -> f.load(valid.toPath()))
.isInstanceOf(UnsupportedOperationException.class);
s.assertThatThrownBy(() -> f.load(valid.toURI().toURL()))
.isInstanceOf(UnsupportedOperationException.class);
}
private static void assertLoadStore(SoftAssertions s, Book.Factory factory, URL sample) throws IOException {
try (Book original = factory.load(sample)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
factory.store(outputStream, original);
byte[] data = outputStream.toByteArray();
try (Book result = factory.load(new ByteArrayInputStream(data))) {
BookAssert.assertContentEquals(s, original, result, false);
}
}
}
private static final Class<?> NULL_CLASS = null;
private static final File NULL_FILE = null;
private static final InputStream NULL_INPUT_STREAM = null;
private static final Path NULL_PATH = null;
private static final URL NULL_URL = null;
//</editor-fold>
}