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