View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.log.api;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  
24  import org.junit.Test;
25  import org.mockito.ArgumentCaptor;
26  
27  import static org.assertj.core.api.Assertions.assertThat;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.times;
30  import static org.mockito.Mockito.verify;
31  import static org.mockito.Mockito.when;
32  
33  /**
34   * Tests for {@link ConsoleLoggerDecorator}, {@link NullConsoleLogger} and {@link PrintStreamLogger}.
35   */
36  public class LoggersTest {
37      @Test
38      public void testPrintStreamLogger() {
39          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
40          PrintStream printStream = new PrintStream(outputStream);
41          PrintStreamLogger logger = new PrintStreamLogger(printStream);
42  
43          assertThat(logger.isErrorEnabled()).isTrue();
44          assertThat(logger.isWarnEnabled()).isTrue();
45          assertThat(logger.isInfoEnabled()).isTrue();
46          assertThat(logger.isDebugEnabled()).isTrue();
47  
48          logger.error("error");
49          logger.debug("debug");
50          logger.info("info");
51          logger.warning("warning");
52  
53          String line = System.lineSeparator();
54          assertThat(outputStream.toString())
55                  .isEqualTo("error" + line + "debug" + line + "info" + line + "warning" + line);
56  
57          Exception e = new Exception("exception");
58          outputStream.reset();
59          logger.error(e);
60          assertThat(outputStream.toString())
61                  .contains("java.lang.Exception: exception")
62                  .contains("at " + getClass().getName() + ".testPrintStreamLogger(LoggersTest.java");
63      }
64  
65      @Test(expected = NullPointerException.class)
66      public void shouldThrowNPE() {
67          new ConsoleLoggerDecorator(null);
68      }
69  
70      @Test
71      public void testDecorator() {
72          ConsoleLogger logger = mock(ConsoleLogger.class);
73          ConsoleLoggerDecorator decorator = new ConsoleLoggerDecorator(logger);
74  
75          assertThat(decorator.isDebugEnabled()).isFalse();
76          when(logger.isDebugEnabled()).thenReturn(true);
77          assertThat(decorator.isDebugEnabled()).isTrue();
78  
79          assertThat(decorator.isInfoEnabled()).isFalse();
80          when(logger.isInfoEnabled()).thenReturn(true);
81          assertThat(decorator.isInfoEnabled()).isTrue();
82  
83          assertThat(decorator.isWarnEnabled()).isFalse();
84          when(logger.isWarnEnabled()).thenReturn(true);
85          assertThat(decorator.isWarnEnabled()).isTrue();
86  
87          assertThat(decorator.isErrorEnabled()).isFalse();
88          when(logger.isErrorEnabled()).thenReturn(true);
89          assertThat(decorator.isErrorEnabled()).isTrue();
90  
91          ArgumentCaptor<String> argumentMsg = ArgumentCaptor.forClass(String.class);
92          decorator.debug("debug");
93          verify(logger, times(1)).debug(argumentMsg.capture());
94          assertThat(argumentMsg.getAllValues()).hasSize(1);
95          assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("debug");
96  
97          argumentMsg = ArgumentCaptor.forClass(String.class);
98          decorator.info("info");
99          verify(logger, times(1)).info(argumentMsg.capture());
100         assertThat(argumentMsg.getAllValues()).hasSize(1);
101         assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("info");
102 
103         argumentMsg = ArgumentCaptor.forClass(String.class);
104         decorator.warning("warning");
105         verify(logger, times(1)).warning(argumentMsg.capture());
106         assertThat(argumentMsg.getAllValues()).hasSize(1);
107         assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("warning");
108 
109         argumentMsg = ArgumentCaptor.forClass(String.class);
110         decorator.error("error");
111         verify(logger, times(1)).error(argumentMsg.capture());
112         assertThat(argumentMsg.getAllValues()).hasSize(1);
113         assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("error");
114 
115         ArgumentCaptor<Throwable> argumentThrowable = ArgumentCaptor.forClass(Throwable.class);
116         argumentMsg = ArgumentCaptor.forClass(String.class);
117         Exception e = new Exception();
118         decorator.error("error", e);
119         verify(logger, times(1)).error(argumentMsg.capture(), argumentThrowable.capture());
120         assertThat(argumentMsg.getAllValues()).hasSize(1);
121         assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("error");
122         assertThat(argumentThrowable.getAllValues()).hasSize(1);
123         assertThat(argumentThrowable.getAllValues().get(0)).isSameAs(e);
124 
125         argumentThrowable = ArgumentCaptor.forClass(Throwable.class);
126         decorator.error(e);
127         verify(logger, times(1)).error(argumentThrowable.capture());
128         assertThat(argumentThrowable.getAllValues()).hasSize(1);
129         assertThat(argumentThrowable.getAllValues().get(0)).isSameAs(e);
130     }
131 }