|
| 1 | +package httpserver; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.Socket; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.Paths; |
| 9 | + |
| 10 | +import static org.junit.Assert.*; |
| 11 | + |
| 12 | +public class ServerTest { |
| 13 | + |
| 14 | + private final Path root; |
| 15 | + private final Path logPath; |
| 16 | + private final SocketHandler socketHandler; |
| 17 | + private Socket acceptResultSocket; |
| 18 | + private ServerSocketSpy serverSocketSpy; |
| 19 | + private Server server; |
| 20 | + private SocketHandlerFactorySpy socketHandlerFactorySpy; |
| 21 | + private ExecutorSpy executorSpy; |
| 22 | + |
| 23 | + public ServerTest() throws IOException { |
| 24 | + acceptResultSocket = new Socket(); |
| 25 | + serverSocketSpy = new ServerSocketSpy(acceptResultSocket); |
| 26 | + root = Paths.get("root"); |
| 27 | + logPath = Paths.get("root/logs"); |
| 28 | + server = new Server(serverSocketSpy, root, logPath); |
| 29 | + |
| 30 | + socketHandler = new SocketHandler(null, null, null, null); |
| 31 | + socketHandlerFactorySpy = new SocketHandlerFactorySpy(socketHandler); |
| 32 | + executorSpy = new ExecutorSpy(); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void callsAcceptOnServerSocket() throws Exception { |
| 37 | + server.start(executorSpy, socketHandlerFactorySpy); |
| 38 | + server.exit(); |
| 39 | + |
| 40 | + assertTrue(serverSocketSpy.acceptCalled); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void callsNewSocketHandlerWithCorrectArgs() throws Exception { |
| 45 | + server.start(executorSpy, socketHandlerFactorySpy); |
| 46 | + server.exit(); |
| 47 | + |
| 48 | + assertEquals(root, socketHandlerFactorySpy.newSocketHandlerArg1); |
| 49 | + assertEquals(logPath, socketHandlerFactorySpy.newSocketHandlerArg2); |
| 50 | + assertEquals(acceptResultSocket, socketHandlerFactorySpy.newSocketHandlerArg3); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void callsExecutorExecuteWithSocketHandler() throws Exception { |
| 55 | + server.start(executorSpy, socketHandlerFactorySpy); |
| 56 | + server.exit(); |
| 57 | + |
| 58 | + assertEquals(socketHandler, executorSpy.executeArg); |
| 59 | + } |
| 60 | +} |
0 commit comments