View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
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   * The Class LogOutputStreamTest.
27   */
28  @ExtendWith(MockitoExtension.class)
29  class LogOutputStreamTest {
30  
31    /** The stream. */
32    PrintStream stream;
33  
34    /** The log. */
35    @Mock
36    Logger log;
37  
38    /**
39     * Logger test.
40     *
41     * @throws IOException Signals that an I/O exception has occurred.
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      // LEVEL_FATAL (6) hits default case in shouldWrite(), returns false so no logging
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     // flush with empty buffer should not log
107     stream.flush();
108   }
109 
110   @Test
111   void testNullLogThrows() {
112     Assertions.assertThrows(IllegalArgumentException.class,
113         () -> LogOutputStream.createPrintStream(null, LogOutputStream.LEVEL_INFO));
114   }
115 }