1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools;
12
13 import static org.mockito.Mockito.when;
14
15 import java.io.IOException;
16 import java.io.PrintStream;
17
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.Test;
20 import org.junit.jupiter.api.extension.ExtendWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.jupiter.MockitoExtension;
23 import org.slf4j.Logger;
24
25
26
27
28 @ExtendWith(MockitoExtension.class)
29 class LogOutputStreamTest {
30
31
32 PrintStream stream;
33
34
35 @Mock
36 Logger log;
37
38
39
40
41
42
43 @Test
44 void loggerTest() throws IOException {
45 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_ERROR);
46 stream.write('\u0001');
47 }
48
49 @Test
50 void testLevelTrace() {
51 when(log.isTraceEnabled()).thenReturn(true);
52 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_TRACE);
53 Assertions.assertNotNull(stream);
54 stream.print("trace message");
55 stream.flush();
56 }
57
58 @Test
59 void testLevelDebug() {
60 when(log.isDebugEnabled()).thenReturn(true);
61 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_DEBUG);
62 Assertions.assertNotNull(stream);
63 stream.print("debug message");
64 stream.flush();
65 }
66
67 @Test
68 void testLevelInfo() {
69 when(log.isInfoEnabled()).thenReturn(true);
70 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_INFO);
71 Assertions.assertNotNull(stream);
72 stream.print("info message");
73 stream.flush();
74 }
75
76 @Test
77 void testLevelWarn() {
78 when(log.isWarnEnabled()).thenReturn(true);
79 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_WARN);
80 Assertions.assertNotNull(stream);
81 stream.print("warn message");
82 stream.flush();
83 }
84
85 @Test
86 void testLevelOff() {
87 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_OFF);
88 Assertions.assertNotNull(stream);
89 stream.print("off message - should not be logged");
90 stream.flush();
91 }
92
93 @Test
94 void testLevelFatal() {
95
96 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_FATAL);
97 Assertions.assertNotNull(stream);
98 stream.print("fatal message");
99 stream.flush();
100 }
101
102 @Test
103 void testFlushWithEmptyBuffer() {
104 when(log.isInfoEnabled()).thenReturn(true);
105 stream = LogOutputStream.createPrintStream(log, LogOutputStream.LEVEL_INFO);
106
107 stream.flush();
108 }
109
110 @Test
111 void testNullLogThrows() {
112 Assertions.assertThrows(IllegalArgumentException.class,
113 () -> LogOutputStream.createPrintStream(null, LogOutputStream.LEVEL_INFO));
114 }
115 }