Skip to content

Commit 95a46b3

Browse files
committed
TDD logger behaviour
1 parent cf3dea9 commit 95a46b3

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package httpserver;
2+
3+
import httpserver.file.FileOperator;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Path;
7+
8+
public class Logger {
9+
private final FileOperator fileOperator;
10+
private final Path logPath;
11+
12+
public Logger(Path logPath, FileOperator fileOperator) {
13+
this.fileOperator = fileOperator;
14+
this.logPath = logPath;
15+
createFileIfDoesntExist(logPath);
16+
}
17+
18+
public void log(String logString) throws IOException {
19+
byte[] logBytes = (logString + "\r\n").getBytes();
20+
fileOperator.appendToFile(logPath, logBytes);
21+
}
22+
23+
private void createFileIfDoesntExist(Path path) {
24+
try {
25+
fileOperator.createFileAtPath(path);
26+
} catch (IOException e) {}
27+
}
28+
}

src/main/java/httpserver/file/FileOperator.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,16 @@
99
import static java.nio.file.Files.write;
1010

1111
public class FileOperator {
12-
public void createFileAtPath(Path path) {
13-
try {
14-
createFile(path);
15-
} catch (IOException e) {
16-
e.printStackTrace();
17-
}
12+
public void createFileAtPath(Path path) throws IOException {
13+
createFile(path);
1814
}
1915

20-
public void deleteFileAtPath(Path path) {
21-
try {
22-
deleteIfExists(path);
23-
} catch (IOException e) {
24-
e.printStackTrace();
25-
}
16+
public void deleteFileAtPath(Path path) throws IOException {
17+
deleteIfExists(path);
2618
}
2719

28-
public void appendToFile(Path path, byte[] contents) {
29-
try {
30-
write(path, contents, StandardOpenOption.APPEND);
31-
} catch (IOException e) {
32-
e.printStackTrace();
33-
}
20+
public void appendToFile(Path path, byte[] contents) throws IOException {
21+
write(path, contents, StandardOpenOption.APPEND);
3422
}
3523

3624
public void replaceContents(Path path, byte[] contents) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package httpserver;
2+
3+
import httpserver.file.FileOperator;
4+
import org.junit.Test;
5+
6+
import java.io.File;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.nio.file.StandardOpenOption;
10+
11+
import static httpserver.file.FileHelpers.tempDir;
12+
import static java.nio.file.Files.readAllBytes;
13+
import static java.nio.file.Files.write;
14+
import static java.nio.file.Files.exists;
15+
import static org.junit.Assert.*;
16+
17+
public class LoggerTest {
18+
@Test
19+
public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
20+
Path logPath = Paths.get(tempDir().toString(), "logs");
21+
22+
new Logger(logPath, new FileOperator());
23+
24+
assertTrue(exists(logPath));
25+
26+
write(logPath, "fake log line\r\n".getBytes(),
27+
StandardOpenOption.APPEND);
28+
29+
new Logger(logPath, new FileOperator());
30+
31+
assertEquals("fake log line\r\n", new String(readAllBytes(logPath)));
32+
}
33+
34+
@Test
35+
public void onLogCallsFileOperatorAppendWithPathAndPayload() throws Exception {
36+
Path logPath = Paths.get(tempDir().toString(), "logs");
37+
Logger logger = new Logger(logPath, new FileOperator());
38+
39+
logger.log("POST example");
40+
logger.log("HEAD example");
41+
42+
assertEquals("POST example\r\nHEAD example\r\n", new String(readAllBytes(logPath)));
43+
}
44+
}

src/test/java/httpserver/file/FileOperatorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
public class FileOperatorTest {
1515

1616
private final Path path;
17-
private final Path dir;
1817
private FileOperator fileOperator;
1918

2019
public FileOperatorTest() throws IOException {
21-
dir = tempDir();
20+
Path dir = tempDir();
2221
path = Paths.get(dir.toString(), "/example.txt");
2322
fileOperator = new FileOperator();
2423
}

0 commit comments

Comments
 (0)