Skip to content

Commit 3da25d3

Browse files
author
Mateusz Szablak
committed
BAEL-4402 Java: Path vs File
1 parent 613a36e commit 3da25d3

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.pathvsfile;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.NoSuchFileException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
15+
import static org.assertj.core.api.Assertions.catchThrowable;
16+
17+
public class PathVsFileUnitTest {
18+
19+
@Test
20+
public void whenCreateFileAndPath_thenShouldPointTheSame() {
21+
File file = new File("baeldung/tutorial.txt");
22+
Path path = Paths.get("baeldung/tutorial.txt");
23+
24+
assertThat(file.toString())
25+
.isEqualTo(path.toString());
26+
assertThat(file.toPath())
27+
.isEqualTo(path);
28+
assertThat(path.toFile())
29+
.isEqualTo(file);
30+
assertThat(file)
31+
.isEqualTo(new File("baeldung", "tutorial.txt"));
32+
assertThat(path)
33+
.isEqualTo(Paths.get("baeldung", "tutorial.txt" ))
34+
.isEqualTo(Paths.get("baeldung").resolve("tutorial.txt"));
35+
}
36+
37+
@Test
38+
public void givenNoFile_whenDelete_thenReportError() {
39+
File file = new File("tutorial.txt");
40+
Path path = Paths.get("tutorial.txt");
41+
42+
assertThat(file.delete()).isFalse();
43+
assertThatThrownBy(() -> Files.delete(path))
44+
.isInstanceOf(IOException.class)
45+
.isInstanceOf(NoSuchFileException.class);
46+
}
47+
48+
@Test
49+
public void givenExistedFile_whenReadMetadata_thenShouldBeSame() throws IOException {
50+
File file = new File("tutorial.txt");
51+
Path path = Paths.get("tutorial.txt");
52+
53+
Throwable throwable = catchThrowable(() -> Files.createFile(path));
54+
assertThat(throwable)
55+
.isNull();
56+
57+
assertThat(file.exists())
58+
.isEqualTo(Files.exists(path));
59+
assertThat(file.isFile())
60+
.isEqualTo(Files.isRegularFile(path));
61+
assertThat(file.isDirectory())
62+
.isEqualTo(Files.isDirectory(path));
63+
assertThat(file.canRead())
64+
.isEqualTo(Files.isReadable(path));
65+
assertThat(file.canWrite())
66+
.isEqualTo(Files.isWritable(path));
67+
assertThat(file.canExecute())
68+
.isEqualTo(Files.isExecutable(path));
69+
assertThat(file.isHidden())
70+
.isEqualTo(Files.isHidden(path));
71+
}
72+
73+
@Test
74+
public void givenExistedFile_whenUriPathOperations_thenShouldBeSame() throws IOException {
75+
File file = new File("tutorial.txt");
76+
Path path = Paths.get("tutorial.txt");
77+
78+
Throwable throwable = catchThrowable(() -> Files.createFile(path));
79+
assertThat(throwable)
80+
.isNull();
81+
82+
assertThat(file.toURI())
83+
.isEqualTo(path.toUri());
84+
assertThat(file.getAbsolutePath())
85+
.isEqualTo(path.toAbsolutePath().toString());
86+
assertThat(file.getCanonicalPath())
87+
.isEqualTo(path.toRealPath().toString())
88+
.isEqualTo(path.toRealPath().normalize().toString());
89+
}
90+
91+
@AfterEach
92+
public void afterEach() throws IOException {
93+
Path path = Paths.get("tutorial.txt");
94+
if (Files.exists(path)) {
95+
Files.delete(path);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)