Newer
Older
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
package no.uib.inf101.terminal;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestSimpleShellStarter {
static final String orgCwd = System.getProperty("user.dir");
static final String DIR = "testdir";
static final String SUBDIR = "subdir";
private File dir;
private File subdir;
private SimpleShell shell;
////////////////////////////////////////////////////////////////////////
//////// The tests ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
@Test
public void testDoNothing() {
assertEquals("$ ", shell.whatTheScreenLooksLike());
}
@Test
public void testWriteFoo() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
assertEquals("$ foo", shell.whatTheScreenLooksLike());
}
@Test
public void testIllegalCommand() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('\n');
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.whatTheScreenLooksLike());
}
@Test
public void testPwd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
String expected = "$ pwd" + "\n" + this.dir.getCanonicalPath() + "\n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
@Test
public void testLs() throws IOException {
shell.aKeyIsPressed('l');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('\n');
String expected = "$ ls\n" + SUBDIR + " \n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
@Test
public void testCd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('c');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed(' ');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('u');
shell.aKeyIsPressed('b');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('i');
shell.aKeyIsPressed('r');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
String expected = "$ pwd\n"
+ this.dir.getCanonicalPath() + "\n"
+ "$ cd subdir\n"
+ "$ pwd\n"
+ this.subdir.getCanonicalPath() + "\n"
+ "$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
////////////////////////////////////////////////////////////////////////
//////// Preparing the tests //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
@BeforeEach
public void setUp() throws IOException {
// Set up test directory
File cwd = new File(orgCwd);
this.dir = new File(cwd, DIR);
this.subdir = new File(this.dir, SUBDIR);
this.dir.mkdir();
this.subdir.mkdir();
System.setProperty("user.dir", this.dir.getCanonicalPath());
this.shell = new SimpleShell();
}
@AfterEach
public void tearDown() {
File cwd = new File(System.getProperty("user.dir"));
File testDir = new File(cwd, DIR);
deleteFolderAndItsContent(testDir);
System.setProperty("user.dir", orgCwd);
this.shell = null;
}
private void deleteFolderAndItsContent(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
deleteFolderAndItsContent(f);
}
}
file.delete();
}
}