Skip to content

Commit 40796d7

Browse files
committed
Extract AppConfig all the way up to the top level
1 parent a435d62 commit 40796d7

File tree

8 files changed

+42
-78
lines changed

8 files changed

+42
-78
lines changed

src/main/java/httpserver/App.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package httpserver;
22

3+
import httpserver.file.FileOperator;
34
import httpserver.file.PathExaminer;
45

56
import java.io.IOException;
7+
import java.nio.file.Path;
68
import java.util.concurrent.ExecutorService;
79
import java.util.concurrent.Executors;
810

@@ -11,16 +13,23 @@ public class App {
1113
private static ExecutorService threadPool;
1214
private static ServerFactory serverFactory;
1315
private static SocketHandlerFactory socketHandlerFactory;
16+
private static PathExaminer pathExaminer;
1417

1518
public static void main(String[] args) throws IOException {
16-
threadPool = Executors.newFixedThreadPool(16);
17-
serverFactory = new ServerFactory(new PathExaminer(), new ServerSocketFactory());
18-
socketHandlerFactory = new SocketHandlerFactory();
19-
2019
int port = Integer.parseInt(args[1]);
2120
String fileDirectory = args[3];
2221

23-
Server server = serverFactory.makeServer(port, fileDirectory);
22+
pathExaminer = new PathExaminer();
23+
Path root = pathExaminer.getPath(fileDirectory);
24+
Path logPath = pathExaminer.concatenate(root,"logs");
25+
AppConfig appConfig = new AppConfig(root, new Logger(logPath, new FileOperator()));
26+
27+
serverFactory = new ServerFactory(new ServerSocketFactory(), appConfig);
28+
socketHandlerFactory = new SocketHandlerFactory();
29+
30+
threadPool = Executors.newFixedThreadPool(16);
31+
32+
Server server = serverFactory.makeServer(port);
2433
while (true) {
2534
server.acceptConnection(threadPool, socketHandlerFactory);
2635
}

src/main/java/httpserver/Server.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
import java.io.IOException;
44
import java.net.ServerSocket;
55
import java.net.Socket;
6-
import java.nio.file.Path;
76
import java.util.concurrent.ExecutionException;
87
import java.util.concurrent.ExecutorService;
98
import java.util.concurrent.Future;
109

1110
public class Server {
12-
private final Path logPath;
13-
private final Path root;
1411
private final ServerSocket serverSocket;
12+
private final AppConfig appConfig;
1513

16-
public Server(ServerSocket serverSocket, Path root, Path logPath) {
14+
public Server(ServerSocket serverSocket, AppConfig appConfig) {
1715
this.serverSocket = serverSocket;
18-
this.root = root;
19-
this.logPath = logPath;
16+
this.appConfig = appConfig;
2017
}
2118

2219
public void acceptConnection(ExecutorService executorService, SocketHandlerFactory socketHandlerFactory){
2320
try (Socket clientSocket = serverSocket.accept()) {
24-
SocketHandler socketHandler = socketHandlerFactory.newSocketHandler(root, logPath, clientSocket);
21+
SocketHandler socketHandler = socketHandlerFactory.newSocketHandler(appConfig, clientSocket);
2522
Future<?> future = executorService.submit(socketHandler);
2623
future.get();
2724
} catch (IOException e) {
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package httpserver;
22

3-
import httpserver.file.PathExaminer;
4-
53
import java.io.IOException;
64
import java.net.ServerSocket;
7-
import java.nio.file.Path;
85

96
public class ServerFactory {
10-
private ServerSocketFactory serverSocketFactory;
11-
private PathExaminer pathExaminer;
7+
private final AppConfig appConfig;
8+
private final ServerSocketFactory serverSocketFactory;
129

13-
public ServerFactory(PathExaminer pathExaminer, ServerSocketFactory serverSocketFactory) {
10+
public ServerFactory(ServerSocketFactory serverSocketFactory,
11+
AppConfig appConfig) {
1412
this.serverSocketFactory = serverSocketFactory;
15-
this.pathExaminer = pathExaminer;
13+
this.appConfig = appConfig;
1614
}
1715

18-
public Server makeServer(int port, String fileDirectory) throws IOException {
19-
Path root = pathExaminer.getPath(fileDirectory);
20-
Path logPath = pathExaminer.concatenate(root,"logs");
16+
public Server makeServer(int port) throws IOException {
2117
ServerSocket serverSocket = serverSocketFactory.newServerSocket(port);
22-
23-
return new Server(serverSocket, root, logPath);
18+
return new Server(serverSocket, appConfig);
2419
}
2520
}

src/main/java/httpserver/SocketHandlerFactory.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package httpserver;
22

3-
import httpserver.file.FileOperator;
4-
53
import java.io.IOException;
64
import java.net.Socket;
7-
import java.nio.file.Path;
85

96
public class SocketHandlerFactory {
10-
public SocketHandler newSocketHandler(Path root, Path logPath, Socket clientSocket) throws IOException {
11-
return new SocketHandler(root,
12-
new Logger(logPath, new FileOperator()),
7+
public SocketHandler newSocketHandler(AppConfig appConfig, Socket clientSocket) throws IOException {
8+
return new SocketHandler(appConfig.getRoot(),
9+
appConfig.getLogger(),
1310
clientSocket.getInputStream(),
1411
clientSocket.getOutputStream());
1512
}

src/test/java/httpserver/ServerFactoryTest.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
11
package httpserver;
22

3-
import httpserver.file.PathExaminer;
43
import org.junit.Test;
54

6-
import java.nio.file.Paths;
7-
85
import static org.mockito.Mockito.*;
96

107
public class ServerFactoryTest {
11-
12-
private final ServerSocketFactory serverSocketFactoryMock;
13-
private ServerFactory serverFactory;
14-
private String fileDirectory;
15-
private final PathExaminer pathExaminerMock;
16-
17-
public ServerFactoryTest() {
18-
fileDirectory = "/example/public/";
19-
20-
pathExaminerMock = mock(PathExaminer.class);
21-
when(pathExaminerMock.getPath(any())).thenReturn(Paths.get("example", "public"));
22-
23-
serverSocketFactoryMock = mock(ServerSocketFactory.class);
24-
25-
serverFactory = new ServerFactory(pathExaminerMock,
26-
serverSocketFactoryMock);
27-
}
28-
29-
@Test
30-
public void callsGetPathOnPathExaminerWithFileDirectory() throws Exception {
31-
serverFactory.makeServer(5000, fileDirectory);
32-
33-
verify(pathExaminerMock).getPath("/example/public/");
34-
}
35-
36-
@Test
37-
public void callsPathExaminerConcatenateWithResultOfGetPathAndLogs() throws Exception {
38-
serverFactory.makeServer(5000, fileDirectory);
39-
40-
verify(pathExaminerMock).concatenate(Paths.get("example", "public"), "logs");
41-
}
42-
438
@Test
449
public void callsNewServerSocketOnServerSocketFactoryWithPort() throws Exception {
45-
serverFactory.makeServer(5000, fileDirectory);
10+
ServerSocketFactory serverSocketFactoryMock = mock(ServerSocketFactory.class);
11+
AppConfig appConfigMock = mock(AppConfig.class);
12+
ServerFactory serverFactory = new ServerFactory(serverSocketFactoryMock, appConfigMock);
13+
14+
serverFactory.makeServer(5000);
4615

4716
verify(serverSocketFactoryMock).newServerSocket(5000);
4817
}

src/test/java/httpserver/ServerTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55
import java.io.IOException;
66
import java.net.ServerSocket;
77
import java.net.Socket;
8-
import java.nio.file.Path;
9-
import java.nio.file.Paths;
108
import java.util.concurrent.ExecutorService;
119
import java.util.concurrent.Future;
1210

1311
import static org.mockito.Mockito.*;
1412

1513
public class ServerTest {
1614

17-
private final Path root;
18-
private final Path logPath;
1915
private final SocketHandlerFactory socketHandlerFactoryMock;
2016
private final ServerSocket serverSocketMock;
2117
private final Socket socket;
18+
private final AppConfig appConfigMock;
2219
private Server server;
2320
private final ExecutorService executorServiceMock;
2421
private final SocketHandler socketHandlerMock;
@@ -29,13 +26,12 @@ public ServerTest() throws IOException {
2926
serverSocketMock = mock(ServerSocket.class);
3027
when(serverSocketMock.accept()).thenReturn(socket);
3128

32-
root = Paths.get("root");
33-
logPath = Paths.get("root/logs");
34-
server = new Server(serverSocketMock, root, logPath);
29+
appConfigMock = mock(AppConfig.class);
30+
server = new Server(serverSocketMock, appConfigMock);
3531

3632
socketHandlerMock = mock(SocketHandler.class);
3733
socketHandlerFactoryMock = mock(SocketHandlerFactory.class);
38-
when(socketHandlerFactoryMock.newSocketHandler(any(), any(), any())).thenReturn(socketHandlerMock);
34+
when(socketHandlerFactoryMock.newSocketHandler(any(), any())).thenReturn(socketHandlerMock);
3935

4036
executorServiceMock = mock(ExecutorService.class);
4137
futureMock = mock(Future.class);
@@ -53,7 +49,7 @@ public void callsAcceptOnServerSocket() throws Exception {
5349
public void callsNewSocketHandlerWithCorrectArgs() throws Exception {
5450
server.acceptConnection(executorServiceMock, socketHandlerFactoryMock);
5551

56-
verify(socketHandlerFactoryMock).newSocketHandler(root, logPath, socket);
52+
verify(socketHandlerFactoryMock).newSocketHandler(appConfigMock, socket);
5753
}
5854

5955
@Test

src/test/java/httpserver/SocketHandlerFactoryTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.junit.Test;
44

55
import java.net.Socket;
6-
import java.nio.file.Paths;
76

87
import static org.mockito.Mockito.*;
98

@@ -13,8 +12,7 @@ public void callsGetInputStreamOnClientSocket() throws Exception {
1312
SocketHandlerFactory socketHandlerFactory = new SocketHandlerFactory();
1413
Socket socketMock = mock(Socket.class);
1514

16-
socketHandlerFactory.newSocketHandler(Paths.get("example)"),
17-
Paths.get("example", "logs"), socketMock);
15+
socketHandlerFactory.newSocketHandler(mock(AppConfig.class), socketMock);
1816
verify(socketMock).getInputStream();
1917
verify(socketMock).getOutputStream();
2018
}

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public SocketHandlerTest() throws IOException {
2424
relativePath2 = root.relativize(tempFileOptions(root, "bbb", "temp"));
2525
}
2626

27+
@Test
28+
public void callsParseOnRequestParser() {}
29+
2730
@Test
2831
public void returns404ForMalformedRequestLine() throws Exception {
2932
byte[] request = ("".getBytes());

0 commit comments

Comments
 (0)