Skip to content

Commit da164c7

Browse files
committed
TDD ServerFactory
1 parent 99c0b7e commit da164c7

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

src/main/java/httpserver/ServerFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.IOException;
66
import java.net.ServerSocket;
77
import java.nio.file.Path;
8-
import java.nio.file.Paths;
98

109
public class ServerFactory {
1110
private ServerSocketFactory serverSocketFactory;
@@ -17,7 +16,7 @@ public ServerFactory(PathExaminer pathExaminer, ServerSocketFactory serverSocket
1716
}
1817
public Server makeServer(int port, String fileDirectory) throws IOException {
1918
Path root = pathExaminer.getPath(fileDirectory);
20-
Path logPath = Paths.get(root.toString(), "logs");
19+
Path logPath = pathExaminer.concatenate(root,"logs");
2120
ServerSocket serverSocket = serverSocketFactory.newServerSocket(port);
2221

2322
return new Server(serverSocket, root, logPath);

src/main/java/httpserver/file/PathExaminer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.nio.file.Paths;
88
import java.util.ArrayList;
99
import java.util.List;
10-
import java.util.WeakHashMap;
1110

1211
import static java.nio.file.Files.*;
1312

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package httpserver;
2+
3+
import httpserver.file.PathExaminerSpy;
4+
import org.junit.Test;
5+
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class ServerFactoryTest {
12+
13+
private final Path spyGetPathResult;
14+
private PathExaminerSpy pathExaminerSpy;
15+
private ServerSocketFactorySpy serverSocketFactorySpy;
16+
private ServerFactory serverFactory;
17+
private String fileDirectory;
18+
19+
public ServerFactoryTest() {
20+
spyGetPathResult = Paths.get("example, public");
21+
pathExaminerSpy = new PathExaminerSpy(spyGetPathResult);
22+
serverSocketFactorySpy = new ServerSocketFactorySpy();
23+
serverFactory = new ServerFactory(pathExaminerSpy,
24+
serverSocketFactorySpy);
25+
fileDirectory = "/example/public/";
26+
}
27+
28+
@Test
29+
public void callsGetPathOnPathExaminerWithFileDirectory() throws Exception {
30+
serverFactory.makeServer(5000, fileDirectory);
31+
32+
assertEquals(fileDirectory, pathExaminerSpy.getPathCalledWith);
33+
}
34+
35+
@Test
36+
public void callsPathExaminerConcatenateWithResultOfGetPathAndLogs() throws Exception {
37+
serverFactory.makeServer(5000, fileDirectory);
38+
39+
assertEquals(spyGetPathResult, pathExaminerSpy.concatenateCalledWith1);
40+
assertEquals("logs", pathExaminerSpy.concatenateCalledWith2);
41+
}
42+
43+
@Test
44+
public void callsNewServerSocketOnServerSocketFactoryWithPort() throws Exception {
45+
serverFactory.makeServer(5000, fileDirectory);
46+
47+
assertEquals(5000, serverSocketFactorySpy.newServerSocketCalledWith);
48+
}
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package httpserver;
2+
3+
import java.net.ServerSocket;
4+
5+
public class ServerSocketFactorySpy extends ServerSocketFactory {
6+
public int newServerSocketCalledWith;
7+
8+
@Override
9+
public ServerSocket newServerSocket(int port) {
10+
newServerSocketCalledWith = port;
11+
return null;
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package httpserver.file;
2+
3+
import java.nio.file.Path;
4+
5+
public class PathExaminerSpy extends PathExaminer {
6+
public String getPathCalledWith;
7+
private Path getPathResult;
8+
public Path concatenateCalledWith1;
9+
public String concatenateCalledWith2;
10+
11+
public PathExaminerSpy(Path getPathResult) {
12+
this.getPathCalledWith = null;
13+
this.getPathResult = getPathResult;
14+
}
15+
16+
@Override
17+
public Path getPath(String input) {
18+
this.getPathCalledWith = input;
19+
return getPathResult;
20+
}
21+
22+
@Override
23+
public Path concatenate(Path root, String suffix) {
24+
this.concatenateCalledWith1 = root;
25+
this.concatenateCalledWith2 = suffix;
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)