-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerTest.java
More file actions
62 lines (47 loc) · 1.85 KB
/
LoggerTest.java
File metadata and controls
62 lines (47 loc) · 1.85 KB
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
package httpserver;
import httpserver.file.FileOperator;
import org.junit.Test;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import static httpserver.file.FileHelpers.tempDir;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Files.write;
import static java.nio.file.Files.exists;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class LoggerTest {
private final Path logPath;
private final Logger logger;
private final PrintStream printStreamMock;
private final FileOperator fileOperatorMock;
public LoggerTest() throws IOException {
logPath = Paths.get(tempDir().toString(), "logs");
printStreamMock = mock(PrintStream.class);
logger = new Logger(logPath, new FileOperator(), printStreamMock);
fileOperatorMock = mock(FileOperator.class);
}
@Test
public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
assertTrue(exists(logPath));
write(logPath, "fake log line\r\n".getBytes(),
StandardOpenOption.APPEND);
assertEquals("fake log line\r\n", new String(readAllBytes(logPath)));
}
@Test
public void writesToLogAndReadsFromLog() throws Exception {
logger.log("POST example");
logger.log("HEAD example");
String log = new String(logger.readLog());
assertEquals("POST example\r\nHEAD example\r\n", log);
}
@Test
public void printsErrorToStdErrIfCantWriteToLog() throws Exception {
doThrow(new IOException()).when(fileOperatorMock).appendToFile(any(), any());
Logger logger = new Logger(logPath, fileOperatorMock, printStreamMock);
logger.log("");
verify(printStreamMock).print(any(Exception.class));
}
}