-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMFSTest.java
130 lines (128 loc) · 4.5 KB
/
MFSTest.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
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class MFSTest {
private final MFS mfs = new MFS();
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
@BeforeEach
public void setUpStreams() {
System.setOut(new PrintStream(outputStream));
}
@AfterEach
public void restoreStreams() {
System.setOut(originalOut);
}
@AfterAll
static void cleanUp() {
MFS.deleteInitFolder();
}
@Test
@Order(1)
//Add file
public void test1() {
String command = "touch root-firstFile! ls root";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "File: firstFile!";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(2)
//Add folder
public void test2() {
String command = "mkdir root-firstDirectory! ls root";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "Directory: firstDirectory!\r\n" +
"File: firstFile!";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(3)
//Delete folder
public void test3() {
String filePath = "target/firstFolder";
long timeoutMillis = 2000;
long startTime = System.currentTimeMillis();
while (!Files.exists(Path.of(filePath))) {
long currentTime = System.currentTimeMillis();
if (currentTime - startTime > timeoutMillis) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
String command = "rmdir root-firstDirectory! ls root";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "File: firstFile!";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(4)
//Move folder
public void test4() {
String command = "mkdir root-firstDirectory mkdir root-secondDirectory mvdir root-secondDirectory root-firstDirectory ls root-firstDirectory-secondDirectory";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(5)
//Print folder
public void test5() {
String command = "ls root";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "Directory: firstDirectory\r\n" +
"File: firstFile!";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(6)
//Write to file
public void test6() {
String command = "echo root-firstFile! hello,Jack cat root-firstFile!";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "hello,Jack";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(7)
//Copy file
public void test7() {
String command = "copy root-firstFile! root-firstDirectory ls root-firstDirectory";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "Directory: secondDirectory\r\n" +
"File: firstFile!";
assertEquals(expectedOutput, consoleOutput);
}
@Test
@Order(8)
//remove file
public void test8() {
String command = "delete root-firstFile! ls root";
mfs.mainInternal(command.split(" "));
String consoleOutput = outputStream.toString().trim();
System.out.println(consoleOutput);
String expectedOutput = "Directory: firstDirectory";
assertEquals(expectedOutput, consoleOutput);
}
}