Skip to content

Commit 2557bf6

Browse files
Merge pull request #3 from xdvrx1/unit-tests
Unit tests
2 parents c0dc788 + 4dc6c00 commit 2557bf6

File tree

5 files changed

+333
-0
lines changed

5 files changed

+333
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package xdvrx1_serverProject;
2+
3+
import java.io.*;
4+
import java.net.*;
5+
6+
import org.junit.*;
7+
8+
//streams are expressed referring to files
9+
//but since we want to test the functionality,
10+
//and since streams are treated almost the same,
11+
//we use streams using files
12+
public class ClientRequestTest {
13+
14+
File rootDirectory;
15+
String defaultPage;
16+
Socket connection;
17+
File tempFile;
18+
19+
ClientRequest clientRequest;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
rootDirectory = new File(".");
24+
defaultPage = "index.html";
25+
//a blank socket,
26+
connection = new Socket();
27+
28+
tempFile = File.createTempFile("sample", null);
29+
30+
clientRequest = new ClientRequest(rootDirectory,
31+
defaultPage,
32+
connection);
33+
}
34+
35+
@Test
36+
public void testObjectShouldNotBeNull() throws IOException {
37+
//just a basic test whether this object
38+
//is successfully created
39+
Assert.assertNotNull(clientRequest);
40+
}
41+
42+
@Test(expected = IllegalArgumentException.class)
43+
public void testConstructorShouldThrowException() {
44+
//`tempFile` is not a directory, it is a file
45+
//so, this constructor, as expected, should
46+
//throw IllegalArgumentException
47+
ClientRequest clientRequest2 =
48+
new ClientRequest(tempFile, defaultPage, connection);
49+
}
50+
51+
@Test
52+
public void testConstructorShouldNotThrowException() {
53+
//now, rootDirectory is a real directory
54+
//it will not throw an exception
55+
ClientRequest clientRequest3 =
56+
new ClientRequest(rootDirectory, defaultPage, connection);
57+
58+
}
59+
60+
@After
61+
public void tearDown() throws Exception {
62+
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package xdvrx1_serverProject;
2+
3+
import java.io.*;
4+
5+
import org.junit.*;
6+
7+
public class FileWebServerTest {
8+
9+
File rootDirectory;
10+
FileWebServer fileWebServer;
11+
File tempFile;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
16+
rootDirectory = new File(".");
17+
fileWebServer = new FileWebServer(rootDirectory, 80);
18+
tempFile = File.createTempFile("sample", null);
19+
20+
}
21+
22+
@Test
23+
public void testObjectShouldNotBeNull() {
24+
//a simple test whether the object
25+
//is successfully created
26+
Assert.assertNotNull(fileWebServer);
27+
}
28+
29+
@Test(expected = IOException.class)
30+
public void testConstructorShouldThrowException() throws IOException {
31+
//`tempFile` is not a directory, it is a file
32+
//so this should throw the exception
33+
FileWebServer fileWebServer2 = new FileWebServer(tempFile, 80);
34+
35+
}
36+
37+
@Test
38+
public void testConstructorShouldNotThrowException() throws IOException {
39+
//rootDirectory is a real directory, so
40+
//this will not throw the exception
41+
FileWebServer fileWebServer3 = new FileWebServer(rootDirectory, 80);
42+
43+
}
44+
45+
@After
46+
public void tearDown() throws Exception {
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package xdvrx1_serverProject;
2+
3+
import java.nio.file.Files;
4+
import java.io.*;
5+
import java.net.*;
6+
7+
import org.junit.*;
8+
9+
//streams are expressed referring to files
10+
//but since we want to test the functionality,
11+
//and since streams are treated almost the same,
12+
//we use streams using files
13+
public class GETMethodTest {
14+
15+
File rootDirectory;
16+
String defaultPage;
17+
String http_version;
18+
19+
GETMethod getMethod;
20+
21+
String firstLine;
22+
String[] token;
23+
24+
File tempFile;
25+
OutputStream out;
26+
Writer out2;
27+
File rootDirectoryX;
28+
29+
@Before
30+
public void setUp() throws Exception {
31+
rootDirectoryX = new File(".");
32+
33+
//get the absolute path
34+
//you should not do this when this program
35+
//is run, or else a client will have an access to
36+
//other directories you don't want to expose
37+
//but for testing purposes, we need to get the
38+
//canonical path
39+
String absolutePath = rootDirectoryX.getCanonicalPath();
40+
41+
rootDirectory = new File(absolutePath);
42+
43+
firstLine = "GET /README.md HTTP/1.1";
44+
token = firstLine.split("\\s+");
45+
46+
defaultPage = "index.html";
47+
http_version = null;
48+
49+
//the `tempFile` and `out` are just here
50+
//for testing, but it is not the focus of testing
51+
tempFile = File.createTempFile("tempFileXX", ".txt");
52+
out = new FileOutputStream(tempFile);
53+
out = new BufferedOutputStream(out);
54+
out2 = new OutputStreamWriter(out, "US-ASCII");
55+
56+
getMethod = new GETMethod();
57+
}
58+
59+
@Test
60+
public void testObjectShouldNotBeNull() {
61+
62+
//test both the object and the method
63+
Assert.assertNotNull(getMethod);
64+
Assert.assertNotNull(getMethod
65+
.processGET(rootDirectory, token,
66+
defaultPage, http_version,
67+
out2));
68+
}
69+
70+
@Test
71+
public void testMethodShouldReturnNull() {
72+
//since rootDirectoryX did not get the
73+
//absolute path,
74+
//inside the real method, the
75+
//`if (actualFile.getCanonicalPath().startsWith(root))` will fail
76+
//thereby returning null as indicated in its `else` block
77+
Assert.assertNull(getMethod.
78+
processGET(rootDirectoryX, token,
79+
defaultPage, http_version,
80+
out2));
81+
}
82+
83+
@After
84+
public void tearDown() throws Exception {
85+
86+
}
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xdvrx1_serverProject;
2+
3+
import org.junit.*;
4+
5+
public class POSTMethodTest {
6+
7+
POSTMethod postMethod = new POSTMethod();
8+
//after the third line, that is one blank line
9+
//in HTTP request, following that is the body of the
10+
//request
11+
String userRequestToString =
12+
"first line" + "\r\n" +
13+
"second line" + "\r\n" +
14+
"third line" + "\r\n\r\n" +
15+
"Hello World";
16+
17+
@Test
18+
public void testTheLineEndingsOfClientRequest() {
19+
Assert.assertEquals("Hello World",
20+
postMethod.returnPOSTData(userRequestToString));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package xdvrx1_serverProject;
2+
3+
import java.io.*;
4+
import java.net.*;
5+
6+
import org.junit.*;
7+
8+
//streams are expressed referring to files
9+
//but since we want to test the functionality,
10+
//and since streams are treated almost the same,
11+
//we use streams using files
12+
public class ReadInputStreamTest {
13+
14+
File rootDirectory = new File(".");
15+
File tempFile;
16+
String defaultPage;
17+
Socket connection;
18+
19+
InputStream is;
20+
OutputStream out;
21+
22+
FileInputStream fin;
23+
BufferedInputStream bis;
24+
25+
Reader in;
26+
Writer out2;
27+
28+
ReadInputStream readInputStream;
29+
30+
@Before
31+
public void setUp() throws Exception {
32+
tempFile = File.createTempFile("tempFileX", ".txt");
33+
34+
out = new FileOutputStream(tempFile);
35+
out = new BufferedOutputStream(out);
36+
37+
out2 = new OutputStreamWriter(out, "US-ASCII");
38+
39+
out2.append('a');
40+
out2.append('b');
41+
out2.append('c');
42+
out2.append('\r');
43+
out2.append('\n');
44+
out2.append(' ');
45+
46+
out2.flush();
47+
48+
is = new FileInputStream(tempFile);
49+
bis = new BufferedInputStream(is);
50+
51+
in = new InputStreamReader(bis, "US-ASCII");
52+
53+
readInputStream = new ReadInputStream();
54+
55+
defaultPage = "index.html";
56+
connection = new Socket();
57+
58+
}
59+
60+
@Test
61+
public void testMethodShouldReturnNotNull() {
62+
//test the method whether its returning
63+
//an object
64+
Assert.assertNotNull(readInputStream.
65+
readUserRequest(bis, in, connection));
66+
}
67+
68+
@Test
69+
public void testMethodShouldReturnAnObject() {
70+
71+
//a simple expectation from the encoded data
72+
StringBuffer expectedResult = new StringBuffer();
73+
expectedResult.append('a');
74+
expectedResult.append('b');
75+
expectedResult.append('c');
76+
expectedResult.append('\r');
77+
expectedResult.append('\n');
78+
expectedResult.append(' ');
79+
80+
Assert.assertEquals(expectedResult.toString(),
81+
readInputStream.
82+
readUserRequest(bis, in, connection).toString());
83+
84+
}
85+
86+
@Test(expected = IOException.class)
87+
public void testMethodShouldThrowException() throws IOException {
88+
89+
//when we close the input stream, it should
90+
//throw an exception
91+
in.close();
92+
StringBuffer bufferResult = readInputStream.readUserRequest(bis, in, connection);
93+
//from the method, it will go directly to else
94+
//and will return null, thereby indicating that there
95+
//is an exception
96+
97+
if (bufferResult == null)
98+
//in order for this test to pass
99+
//it should deliberately throw the exception
100+
throw new IOException("IOException");
101+
102+
}
103+
104+
@After
105+
public void tearDown() throws Exception {
106+
107+
}
108+
109+
}

0 commit comments

Comments
 (0)