forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExampleTest.java
More file actions
153 lines (132 loc) · 3.66 KB
/
FileExampleTest.java
File metadata and controls
153 lines (132 loc) · 3.66 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
package com.examplehub.basics.io;
import static org.junit.jupiter.api.Assertions.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import org.junit.jupiter.api.Test;
class FileExampleTest {
@Test
void testInit() {
File file = new File("pom.xml");
file = new File("/root/example_dir/example_file.txt");
}
@Test
void testLength() {
File file = new File("pom.xml");
assertTrue(file.length() >= 0);
}
@Test
void testLastModified() {
File file = new File("pom.xml");
long lastModified = file.lastModified();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(lastModified));
System.out.println(date); // 2021-08-31 09:27:21
}
@Test
void testGetName() {
File file = new File("pom.xml");
assertEquals("pom.xml", file.getName());
}
@Test
void testPath() throws IOException {
File file = new File("../example.txt");
assertEquals("../example.txt", file.getPath());
// assertEquals("/Users/cswiki/Workspace/Github/examplehub/Java/../example.txt",
// file.getAbsolutePath());
// assertEquals("/Users/cswiki/Workspace/Github/examplehub/example.txt",
// file.getCanonicalPath());
}
@Test
void testSeparator() {
assertEquals("/", File.separator);
}
@Test
void testIsFile() {
File file = new File("pom.xml");
assertTrue(file.isFile());
}
@Test
void testIsDirectory() {
File file = new File("src");
assertTrue(file.isDirectory());
}
@Test
void testCreateDelete() throws IOException {
File file = new File("demo.txt");
assertTrue(file.createNewFile());
assertTrue(file.delete());
}
@Test
void testCreateTempFile() throws IOException {
File tempFile = File.createTempFile("temp-", ".txt");
tempFile.deleteOnExit();
assertTrue(tempFile.isFile());
assertFalse(tempFile.isDirectory());
}
// @Test
void testListFiles() {
File file = new File(".");
for (File f : Objects.requireNonNull(file.listFiles())) {
System.out.println(f.getName());
}
}
@Test
void testList() {
File file = new File(".");
for (String path : Objects.requireNonNull(file.list())) {
System.out.println(path);
}
}
@Test
void testListWithFilter() {
File file = new File(".");
File[] files = file.listFiles((dir, name) -> name.endsWith(".xml"));
assert files != null;
assertEquals(1, files.length);
assertEquals("pom.xml", files[0].getName());
}
@Test
void testMkdir() {
File file = new File("temp");
assertTrue(file.mkdir());
assertTrue(file.delete());
assertFalse(file.exists());
}
@Test
void testMkdirs() {
File file = new File("temp/sub/1");
assertTrue(file.mkdirs());
assertTrue(file.delete());
assertTrue(new File("temp/sub").delete());
assertTrue(new File("temp").delete());
assertFalse(file.exists());
}
@Test
void testPaths() {
Path path = Paths.get(".", "/", "pom.xml");
assertEquals("./pom.xml", path.toString());
System.out.println(path.toAbsolutePath());
assertEquals("pom.xml", path.normalize().toString());
File file = path.toFile();
assertEquals("pom.xml", file.getName());
}
@Test
void testExists() {
File file = new File("pom.xml");
assertTrue(file.exists());
file = new File("pom_bk.xml");
assertFalse(file.exists());
}
@Test
void testDelete() throws IOException {
File file = new File("pom_bk.xml");
assertTrue(file.createNewFile());
assertTrue(file.exists());
assertTrue(file.delete());
assertFalse(file.exists());
}
}