/
FileTest.java
106 lines (90 loc) · 3.34 KB
/
FileTest.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
package tests.java.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.github.jjYBdx4IL.utils.env.Maven;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
/*
* #%L
* Evaluation
* %%
* Copyright (C) 2014 Github jjYBdx4IL Projects
* %%
* #L%
*/
/**
*
* @author Github jjYBdx4IL Projects
*/
public class FileTest {
private static final Logger LOG = LoggerFactory.getLogger(FileTest.class);
private final static File TEMP_DIR = Maven.getTempTestDir(FileTest.class);
@Test
public void testListFiles() {
File f = new File(".").listFiles()[0];
assertFalse(f.isAbsolute());
assertTrue(f.toString().contains(File.separator)); // ie. toString() == .\.classpath
}
@Test
public void testConstructorPathSeparatorHandling() {
File f = new File("a", "b/c");
assertEquals("a/b/c".replace("/", File.separator), f.getPath());
f = new File("a", "b\\c");
assertEquals("a/b\\c".replace("/", File.separator), f.getPath());
}
@Test
public void testGetCanonicalPath() throws IOException {
@SuppressWarnings("deprecation")
File targetDir = new File(Maven.getMavenTargetDir().getCanonicalPath() + File.separator);
LOG.debug(targetDir.getCanonicalPath());
assertFalse(targetDir.getCanonicalPath().endsWith(File.separator));
}
@Test
public void testGetParent() {
assertEquals("a", new File("a/b").getParent());
assertNull(new File("b").getParent());
}
@Test
public void testDelete() {
// test delete of non-existing file
File f1 = new File(TEMP_DIR, "not-existing-file-system-entry");
assertFalse(f1.delete());
}
@Test
public void testRename() throws IOException {
File f1 = new File(TEMP_DIR, "testRename1");
File f2 = new File(TEMP_DIR, "testRename2");
assertEquals("testRename1", f1.getName());
assertEquals("testRename2", f2.getName());
assertNotEquals(f1.getAbsolutePath(), f2.getAbsolutePath());
FileUtils.write(f1, "1", "UTF-8");
FileUtils.write(f2, "2", "UTF-8");
assertTrue(f1.exists());
assertTrue(f2.exists());
assertEquals("1", FileUtils.readFileToString(f1, "UTF-8"));
assertEquals("2", FileUtils.readFileToString(f2, "UTF-8"));
if (SystemUtils.IS_OS_WINDOWS) {
assertFalse(f1.renameTo(f2));
assertTrue(f1.exists());
assertTrue(f2.exists());
} else {
assertTrue(f1.renameTo(f2));
assertFalse(f1.exists());
assertTrue(f2.exists());
assertNotEquals(f1.getAbsolutePath(), f2.getAbsolutePath());
assertEquals("testRename1", f1.getName()); // f1 still points to the
// same file name after
// the rename!
assertEquals("testRename2", f2.getName());
assertEquals("1", FileUtils.readFileToString(f2, "UTF-8"));
}
}
}