forked from assertj/assertj-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathAssertionsExamples.java
261 lines (216 loc) · 8.95 KB
/
PathAssertionsExamples.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
/**
* 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 java.lang.System.lineSeparator;
import static java.nio.file.Files.createDirectory;
import static java.nio.file.Files.createSymbolicLink;
import static java.nio.file.Files.deleteIfExists;
import static java.nio.file.Files.write;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.Test;
/**
* Path assertions usage examples.
*
* @author Joel Costigliola
*/
public class PathAssertionsExamples extends AbstractAssertionsExamples {
private Path xFile;
private Path xFileTurkish;
private Path xFileFrench;
private Path xFileClone;
private Path rwxFile;
@Override
@Before
public void setup() {
xFile = Paths.get("target/xfile.txt");
xFileFrench = Paths.get("target/xfile-french.txt");
xFileTurkish = Paths.get("target/xfile.turkish");
xFileClone = Paths.get("target/xfile-clone.txt");
rwxFile = Paths.get("target/rwxFile");
}
@Test
public void path_content_assertions_examples() throws Exception {
// check paths content
write(xFile, "The Truth Is Out There".getBytes());
// The default charset is used
assertThat(xFile).hasContent("The Truth Is Out There");
try {
assertThat(xFile).hasContent("La Vérité Est Ailleurs");
} catch (AssertionError e) {
logAssertionErrorMessage("path hasContent", e);
}
// check content using a specific charset
Charset turkishCharset = Charset.forName("windows-1254");
write(xFileTurkish, singleton("Gerçek Başka bir yerde mi"), turkishCharset);
assertThat(xFileTurkish).usingCharset("windows-1254").hasContent("Gerçek Başka bir yerde mi" + lineSeparator());
assertThat(xFileTurkish).usingCharset(turkishCharset).hasContent("Gerçek Başka bir yerde mi" + lineSeparator());
// compare paths content (uses the default charset)
write(xFileClone, "The Truth Is Out There".getBytes());
assertThat(xFile).hasSameContentAs(xFileClone);
write(xFileFrench, "La Vérité Est Ailleurs".getBytes());
try {
assertThat(xFile).hasSameContentAs(xFileFrench);
} catch (AssertionError e) {
logAssertionErrorMessage("path hasSameContentAs", e);
}
}
@Test
public void path_assertions_binary_content() throws Exception {
write(xFile, "The Truth Is Out There".getBytes());
assertThat(xFile).hasBinaryContent("The Truth Is Out There".getBytes());
// using a specific charset
Charset turkishCharset = Charset.forName("windows-1254");
write(xFileTurkish, singleton("Gerçek Başka bir yerde mi"), turkishCharset);
// The following assertion succeeds:
String expectedContent = "Gerçek Başka bir yerde mi" + lineSeparator();
byte[] binaryContent = expectedContent.getBytes(turkishCharset.name());
assertThat(xFileTurkish).hasBinaryContent(binaryContent);
// The following assertion fails ... unless you are in Turkey ;-):
try {
assertThat(xFileTurkish).hasBinaryContent("Gerçek Başka bir yerde mi".getBytes());
} catch (AssertionError e) {
logAssertionErrorMessage("path hasBinaryContent with charset ", e);
}
}
@Test
public void path_rwx_assertion() throws Exception {
assumeTrue(SystemUtils.IS_OS_UNIX);
// Create a file and set permissions to be readable by all.
write(rwxFile, "rwx file".getBytes());
// using PosixFilePermission to set file permissions 777
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
// add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
// add group permissions
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
// add others permissions
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_WRITE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(rwxFile, perms);
final Path symlinkToRwxFile = FileSystems.getDefault().getPath("symlink-to-rwxFile");
if (!Files.exists(symlinkToRwxFile)) {
createSymbolicLink(symlinkToRwxFile, rwxFile);
}
// The following assertions succeed:
assertThat(rwxFile).isReadable()
.isWritable()
.isExecutable();
assertThat(symlinkToRwxFile).isReadable()
.isWritable()
.isExecutable();
}
@Test
public void path_assertions() throws Exception {
assumeTrue(SystemUtils.IS_OS_UNIX);
// Create a regular file, and a symbolic link pointing to it
final Path existingFile = Paths.get("somefile.txt");
write(existingFile, "foo".getBytes());
final Path symlinkToExistingFile = Paths.get("symlink-to-somefile.txt");
deleteIfExists(symlinkToExistingFile);
createSymbolicLink(symlinkToExistingFile, existingFile);
// Create a symbolic link whose target does not exist
final Path nonExistentPath = Paths.get("nonexistent");
final Path symlinkToNonExistentPath = Paths.get("symlinkToNonExistentPath");
deleteIfExists(symlinkToNonExistentPath);
createSymbolicLink(symlinkToNonExistentPath, nonExistentPath);
// create directory and symlink to it
Path dir = Paths.get("target/dir");
deleteIfExists(dir);
createDirectory(dir);
final Path dirSymlink = Paths.get("target", "dirSymlink");
deleteIfExists(dirSymlink);
createSymbolicLink(dirSymlink, dir.toAbsolutePath());
// assertions examples
assertThat(existingFile).exists();
assertThat(symlinkToExistingFile).exists();
assertThat(existingFile).existsNoFollowLinks();
assertThat(symlinkToNonExistentPath).existsNoFollowLinks();
assertThat(nonExistentPath).doesNotExist();
assertThat(existingFile).isRegularFile();
assertThat(symlinkToExistingFile).isRegularFile();
assertThat(symlinkToExistingFile).isSymbolicLink();
assertThat(dirSymlink).isDirectory().isSymbolicLink();
assertThat(symlinkToNonExistentPath).isSymbolicLink();
assertThat(dir).isDirectory();
assertThat(dirSymlink).isDirectory();
assertThat(dir).hasParent(Paths.get("target"))
.hasParent(Paths.get("target/dir/..")) // would fail with hasParentRaw
.hasParentRaw(Paths.get("target"));
assertThat(existingFile.toRealPath()).isCanonical();
assertThat(existingFile).hasFileName("somefile.txt");
assertThat(symlinkToExistingFile).hasFileName("symlink-to-somefile.txt");
assertThat(Paths.get("/foo/bar")).isAbsolute();
assertThat(Paths.get("foo/bar")).isRelative();
assertThat(Paths.get("/usr/lib")).isNormalized();
assertThat(Paths.get("a/b/c")).isNormalized();
assertThat(Paths.get("../d")).isNormalized();
assertThat(Paths.get("/")).hasNoParent();
assertThat(Paths.get("foo")).hasNoParentRaw();
assertThat(Paths.get("/usr/lib")).startsWith(Paths.get("/usr"))
.startsWith(Paths.get("/usr/lib/..")) // would fail with startsWithRaw
.startsWithRaw(Paths.get("/usr"));
assertThat(Paths.get("/usr/lib")).endsWith(Paths.get("lib"))
.endsWith(Paths.get("lib/../lib")) // would fail with endsWithRaw
.endsWithRaw(Paths.get("lib"));
assertThat(Paths.get("abc.txt")).isLessThan(Paths.get("xyz.txt"));
// assertions error examples
try {
assertThat(nonExistentPath).exists();
} catch (AssertionError e) {
logAssertionErrorMessage("path exists", e);
}
try {
assertThat(nonExistentPath).existsNoFollowLinks();
} catch (AssertionError e) {
logAssertionErrorMessage("path existsNoFollowLinks", e);
}
try {
assertThat(symlinkToNonExistentPath).exists();
} catch (AssertionError e) {
logAssertionErrorMessage("path exists", e);
}
try {
assertThat(dir).hasParentRaw(Paths.get("target/dir/.."));
} catch (AssertionError e) {
logAssertionErrorMessage("path hasParentRaw", e);
}
try {
// fail as symlinkToNonExistentPath exists even if its target does not.
assertThat(symlinkToNonExistentPath).doesNotExist();
} catch (AssertionError e) {
logAssertionErrorMessage("path doesNotExist not following links", e);
}
try {
assertThat(nonExistentPath).exists();
} catch (AssertionError e) {
logAssertionErrorMessage("path exists", e);
}
}
}