33import httpserver .file .FileOperator ;
44import org .junit .Test ;
55
6+ import java .io .IOException ;
67import java .nio .file .Path ;
78import java .nio .file .Paths ;
89import java .nio .file .StandardOpenOption ;
1415import static org .junit .Assert .*;
1516
1617public class LoggerTest {
17- @ Test
18- public void createsLogFileOnConstructDoesntOverwrite () throws Exception {
19- Path logPath = Paths .get (tempDir ().toString (), "logs" );
2018
21- new Logger (logPath , new FileOperator ());
19+ private final Path logPath ;
20+ private final Logger logger ;
2221
22+ public LoggerTest () throws IOException {
23+ logPath = Paths .get (tempDir ().toString (), "logs" );
24+ logger = new Logger (logPath , new FileOperator ());
25+ }
26+ @ Test
27+ public void createsLogFileOnConstructDoesntOverwrite () throws Exception {
2328 assertTrue (exists (logPath ));
2429
2530 write (logPath , "fake log line\r \n " .getBytes (),
@@ -31,24 +36,12 @@ public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
3136 }
3237
3338 @ Test
34- public void onLogCallsFileOperatorAppendWithPathAndPayload () throws Exception {
35- Path logPath = Paths .get (tempDir ().toString (), "logs" );
36- Logger logger = new Logger (logPath , new FileOperator ());
37-
39+ public void writesToLogAndReadsFromLog () throws Exception {
3840 logger .log ("POST example" );
3941 logger .log ("HEAD example" );
4042
41- assertEquals ("POST example\r \n HEAD example\r \n " , new String (readAllBytes (logPath )));
42- }
43-
44- @ Test
45- public void readsLog () throws Exception {
46- Path logPath = Paths .get (tempDir ().toString (), "logs" );
47- Logger logger = new Logger (logPath , new FileOperator ());
48-
49- logger .log ("POST example" );
50- logger .log ("HEAD example" );
43+ String log = new String (logger .readLog ());
5144
52- assertEquals ("POST example\r \n HEAD example\r \n " , new String ( logger . readLog ()) );
45+ assertEquals ("POST example\r \n HEAD example\r \n " , log );
5346 }
5447}
0 commit comments