Repository navigation
Expand file tree
/
Copy pathFileAssertionsExamples.java
More file actions
194 lines (165 loc) · 7.79 KB
/
Copy pathFileAssertionsExamples.java
File metadata and controls
194 lines (165 loc) · 7.79 KB
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
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import org.junit.jupiter.api.Test;
/**
* File and stream usage examples.
*
* @author Joel Costigliola
*/
public class FileAssertionsExamples extends AbstractAssertionsExamples {
@Test
public void file_assertions_examples() throws Exception {
// file assertion
File xFile = writeFile("xFile", "The Truth Is Out There");
assertThat(xFile).exists().isFile().isRelative();
assertThat(xFile).canRead().canWrite();
assertThat(contentOf(xFile)).startsWith("The Truth").contains("Is Out").endsWith("There");
// compare content with another file
File xFileClone = writeFile("xFileClone", "The Truth Is Out There");
assertThat(xFile).hasSameTextualContentAs(xFileClone);
assertThat(xFile).hasSameBinaryContentAs(xFileClone);
// compare content with a string
assertThat(xFile).isNotEmpty()
.hasSize(22)
.hasContent("The Truth Is Out There");
File emptyFile = writeFile("emptyFile.txt", "");
assertThat(emptyFile).isEmpty()
.hasSize(0);
// compare content with a string, specifying a character set
Charset turkishCharset = Charset.forName("windows-1254");
File xFileWithTurkishCharset = writeFile("xFileWithTurkishCharset", "La Vérité Est Ailleurs", turkishCharset);
assertThat(xFileWithTurkishCharset).usingCharset(turkishCharset).hasContent("La Vérité Est Ailleurs");
// compare content with a binary array
byte[] binaryContent = "The Truth Is Out There".getBytes();
assertThat(xFile).hasBinaryContent(binaryContent);
// compare content with a binary array
binaryContent = "La Vérité Est Ailleurs".getBytes(turkishCharset.name());
assertThat(xFileWithTurkishCharset).hasBinaryContent(binaryContent);
File xFileWithExtension = writeFile("xFile.secret", "The Truth Is Out There");
assertThat(xFileWithExtension)
.hasParent("target")
.hasParent(new File("target"))
.hasExtension("secret")
.hasName("xFile.secret");
assertThat(new File("/")).hasNoParent();
}
@Test
public void file_assertions_error_examples() throws Exception {
File xFile = writeFile("xFile", "The Truth Is Out There");
File xFileWithExtension = writeFile("xFile.secret", "The Truth Is Out There");
try {
assertThat(xFileWithExtension).hasParent("xDirectory");
} catch (AssertionError e) {
logAssertionErrorMessage("file with unexpected parent.", e);
}
try {
assertThat(xFileWithExtension).hasExtension("png");
} catch (AssertionError e) {
logAssertionErrorMessage("file with unexpected extension.", e);
}
try {
assertThat(xFile).hasExtension("secret");
} catch (AssertionError e) {
logAssertionErrorMessage("file without extension.", e);
}
try {
assertThat(xFileWithExtension).hasName("xFile");
} catch (AssertionError e) {
logAssertionErrorMessage("file with unexpected name.", e);
}
}
@Test
public void stream_assertions_examples() throws Exception {
assertThat(byteArrayInputStreamFrom("string")).hasSameContentAs(byteArrayInputStreamFrom("string"));
}
@Test
public void improved_diff_assertions_examples() throws Exception {
File actual = writeFile("actual", "aaaaaa\n" +
"bbbbbb\n" +
"cccccc\n" +
"dddddd\n");
File expected = writeFile("expected", "------\n" +
"aaaaaa\n" +
"bbbbbb\n" +
"CCCCCC\n" +
"dddddd\n" +
"eeeeee\n");
try {
assertThat(actual).hasSameTextualContentAs(expected);
} catch (AssertionError e) {
logAssertionErrorMessage("file diff", e);
}
}
@Test
public void should_check_digests() throws Exception {
// GIVEN
File tested = new File("src/test/resources/assertj-core-2.9.0.jar");
byte[] md5Bytes = new byte[] { -36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25 };
byte[] sha1Bytes = new byte[] { 92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44,
26, 44 };
// THEN
assertThat(tested).hasDigest("SHA1", "5c5ae45b58f12023817abe492447cdc7912c1a2c")
.hasDigest(MessageDigest.getInstance("SHA1"), "5c5ae45b58f12023817abe492447cdc7912c1a2c")
.hasDigest("SHA1", sha1Bytes)
.hasDigest(MessageDigest.getInstance("SHA1"), sha1Bytes)
.hasDigest("MD5", "dcb3015cd28447644c810af352832c19")
.hasDigest(MessageDigest.getInstance("MD5"), "dcb3015cd28447644c810af352832c19")
.hasDigest("MD5", md5Bytes)
.hasDigest(MessageDigest.getInstance("MD5"), md5Bytes);
}
@Test
public void directory_assertions() {
File directory = new File("src/test/resources/templates");
assertThat(directory).isNotEmptyDirectory()
.isDirectoryContaining("regex:.*txt")
.isDirectoryContaining("glob:**.txt")
.isDirectoryContaining(file -> file.getName().contains("template"))
.isDirectoryNotContaining("glob:**.java")
.isDirectoryNotContaining("regex:.*java")
.isDirectoryNotContaining(file -> file.getName().endsWith("java"));
Path emptyDirectory = Paths.get("src/test/resources/empty");
assertThat(emptyDirectory).isEmptyDirectory();
// recursive examples
File resourcesDirectory = new File("src/test/resources");
assertThat(resourcesDirectory).isDirectoryRecursivelyContaining(file -> file.getName().contains("template"))
.isDirectoryRecursivelyContaining(file -> file.getName().contains("services"))
.isDirectoryRecursivelyContaining("regex:.*txt")
.isDirectoryRecursivelyContaining("glob:**.txt");
}
// helper methods
private static ByteArrayInputStream byteArrayInputStreamFrom(String string) {
return new ByteArrayInputStream(string.getBytes());
}
private File writeFile(String fileName, String fileContent) throws Exception {
return writeFile(fileName, fileContent, Charset.defaultCharset());
}
private File writeFile(String fileName, String fileContent, Charset charset) throws Exception {
File file = new File("target/" + fileName);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
out.write(fileContent);
out.close();
return file;
}
}