Skip to content

Commit 3f22a70

Browse files
vivekkr12KevinGilmore
authored andcommitted
Add unit test for File path article - BAEL-827 (eugenp#1784)
* BAEL-827 Add unit test for file paths * BAEL-827 - Update path IOException test to execute only on Windows * BAELL-827 - Update unit test method name
1 parent dacc694 commit 3f22a70

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.baeldung.java.io;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.AfterClass;
6+
import org.junit.Assume;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
13+
public class JavaFilePathUnitTest {
14+
15+
private static String userDir;
16+
17+
@BeforeClass
18+
public static void createFilesAndFolders() throws IOException {
19+
userDir = System.getProperty("user.dir");
20+
21+
new File(userDir + "/baeldung/foo").mkdirs();
22+
new File(userDir + "/baeldung/bar/baz").mkdirs();
23+
new File(userDir + "/baeldung/foo/foo-one.txt").createNewFile();
24+
new File(userDir + "/baeldung/foo/foo-two.txt").createNewFile();
25+
new File(userDir + "/baeldung/bar/bar-one.txt").createNewFile();
26+
new File(userDir + "/baeldung/bar/bar-two.txt").createNewFile();
27+
new File(userDir + "/baeldung/bar/baz/baz-one.txt").createNewFile();
28+
new File(userDir + "/baeldung/bar/baz/baz-two.txt").createNewFile();
29+
30+
}
31+
32+
@Test
33+
public void whenPathResolved_thenSuccess() {
34+
File file = new File("baeldung/foo/foo-one.txt");
35+
String expectedPath = isWindows() ? "baeldung\\foo\\foo-one.txt" : "baeldung/foo/foo-one.txt";
36+
String actualPath = file.getPath();
37+
assertEquals(expectedPath, actualPath);
38+
}
39+
40+
@Test
41+
public void whenAbsolutePathResolved_thenSuccess() {
42+
File file = new File("baeldung/foo/foo-one.txt");
43+
String expectedPath = isWindows() ? userDir + "\\baeldung\\foo\\foo-one.txt" : userDir + "/baeldung/foo/foo-one.txt";
44+
String actualPath = file.getAbsolutePath();
45+
assertEquals(expectedPath, actualPath);
46+
}
47+
48+
@Test
49+
public void whenAbsolutePathWithShorthandResolved_thenSuccess() {
50+
File file = new File("baeldung/bar/baz/../bar-one.txt");
51+
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\baz\\..\\bar-one.txt" : userDir + "/baeldung/bar/baz/../bar-one.txt";
52+
String actualPath = file.getAbsolutePath();
53+
assertEquals(expectedPath, actualPath);
54+
}
55+
56+
@Test
57+
public void whenCanonicalPathWithShorthandResolved_thenSuccess() throws IOException {
58+
File file = new File("baeldung/bar/baz/../bar-one.txt");
59+
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\bar-one.txt" : userDir + "/baeldung/bar/bar-one.txt";
60+
String actualPath = file.getCanonicalPath();
61+
assertEquals(expectedPath, actualPath);
62+
}
63+
64+
@Test
65+
public void whenCanonicalPathWithDotShorthandResolved_thenSuccess() throws IOException {
66+
File file = new File("baeldung/bar/baz/./baz-one.txt");
67+
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\baz\\baz-one.txt" : userDir + "/baeldung/bar/baz/baz-one.txt";
68+
String actualPath = file.getCanonicalPath();
69+
assertEquals(expectedPath, actualPath);
70+
}
71+
72+
@Test(expected = IOException.class)
73+
public void givenWindowsOs_whenCanonicalPathWithWildcard_thenIOException() throws IOException {
74+
Assume.assumeTrue(isWindows());
75+
new File("*").getCanonicalPath();
76+
}
77+
78+
@AfterClass
79+
public static void deleteFilesAndFolders() {
80+
File baeldungDir = new File(userDir + "/baeldung");
81+
deleteRecursively(baeldungDir);
82+
}
83+
84+
private static void deleteRecursively(File dir) {
85+
for (File f : dir.listFiles()) {
86+
if (f.isDirectory()) {
87+
deleteRecursively(f);
88+
}
89+
f.delete();
90+
}
91+
dir.delete();
92+
}
93+
94+
private static boolean isWindows() {
95+
String osName = System.getProperty("os.name");
96+
return osName.contains("Windows");
97+
}
98+
99+
}

0 commit comments

Comments
 (0)