View Javadoc
1   package org.apache.maven.plugin.surefire;
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.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
23  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
24  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
25  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
26  import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
27  import org.apache.maven.plugin.surefire.log.api.PrintStreamLogger;
28  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
29  import org.hamcrest.MatcherAssert;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.mockito.ArgumentCaptor;
33  
34  import java.io.File;
35  
36  import static java.nio.charset.StandardCharsets.UTF_8;
37  import static org.apache.maven.surefire.api.util.ReflectionUtils.getMethod;
38  import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray;
39  import static org.fest.assertions.Assertions.assertThat;
40  import static org.hamcrest.CoreMatchers.instanceOf;
41  import static org.hamcrest.CoreMatchers.is;
42  import static org.hamcrest.CoreMatchers.not;
43  import static org.hamcrest.CoreMatchers.notNullValue;
44  import static org.hamcrest.CoreMatchers.sameInstance;
45  import static org.mockito.Mockito.mock;
46  import static org.mockito.Mockito.spy;
47  import static org.mockito.Mockito.times;
48  import static org.mockito.Mockito.verify;
49  import static org.mockito.Mockito.when;
50  import static org.powermock.reflect.Whitebox.getInternalState;
51  
52  /**
53   *
54   */
55  public class CommonReflectorTest
56  {
57      private StartupReportConfiguration startupReportConfiguration;
58      private ConsoleLogger consoleLogger;
59      private File reportsDirectory;
60      private File statistics;
61      private SurefireStatelessReporter xmlReporter;
62      private SurefireConsoleOutputReporter consoleOutputReporter = new SurefireConsoleOutputReporter();
63      private SurefireStatelessTestsetInfoReporter infoReporter = new SurefireStatelessTestsetInfoReporter();
64  
65      @Before
66      public void setup()
67      {
68          File target = new File( System.getProperty( "user.dir" ), "target" );
69          reportsDirectory = new File( target, "tmp6" );
70          statistics = new File( reportsDirectory, "TESTHASH" );
71          xmlReporter = new SurefireStatelessReporter();
72          infoReporter = new SurefireStatelessTestsetInfoReporter();
73  
74          startupReportConfiguration = new StartupReportConfiguration( true, true, "PLAIN", false, reportsDirectory,
75                  false, null, statistics, false, 1, null, null, false,
76                  xmlReporter, consoleOutputReporter, infoReporter );
77  
78          consoleLogger = mock( ConsoleLogger.class );
79      }
80  
81      @Test
82      public void createReportingReporterFactory()
83      {
84          CommonReflector reflector = new CommonReflector( Thread.currentThread().getContextClassLoader() );
85          DefaultReporterFactory factory = (DefaultReporterFactory) reflector.createReportingReporterFactory(
86                  startupReportConfiguration, consoleLogger );
87  
88          assertThat( factory )
89                  .isNotNull();
90  
91          StartupReportConfiguration reportConfiguration = getInternalState( factory, "reportConfiguration" );
92          assertThat( reportConfiguration )
93                  .isNotSameAs( startupReportConfiguration );
94          assertThat( reportConfiguration.isUseFile() ).isTrue();
95          assertThat( reportConfiguration.isPrintSummary() ).isTrue();
96          assertThat( reportConfiguration.getReportFormat() ).isEqualTo( "PLAIN" );
97          assertThat( reportConfiguration.isRedirectTestOutputToFile() ).isFalse();
98          assertThat( reportConfiguration.getReportsDirectory() ).isSameAs( reportsDirectory );
99          assertThat( reportConfiguration.isTrimStackTrace() ).isFalse();
100         assertThat( reportConfiguration.getReportNameSuffix() ).isNull();
101         assertThat( reportConfiguration.getStatisticsFile() ).isSameAs( statistics );
102         assertThat( reportConfiguration.isRequiresRunHistory() ).isFalse();
103         assertThat( reportConfiguration.getRerunFailingTestsCount() ).isEqualTo( 1 );
104         assertThat( reportConfiguration.getXsdSchemaLocation() ).isNull();
105         assertThat( reportConfiguration.getEncoding() ).isEqualTo( UTF_8 );
106         assertThat( reportConfiguration.isForkMode() ).isFalse();
107         assertThat( reportConfiguration.getXmlReporter().toString() )
108                 .isEqualTo( xmlReporter.toString() );
109         assertThat( reportConfiguration.getTestsetReporter().toString() )
110                 .isEqualTo( infoReporter.toString() );
111         assertThat( reportConfiguration.getConsoleOutputReporter().toString() )
112                 .isEqualTo( consoleOutputReporter.toString() );
113     }
114 
115     @Test
116     public void shouldProxyConsoleLogger()
117     {
118         ClassLoader cl = Thread.currentThread().getContextClassLoader();
119         ConsoleLogger logger = spy( new PrintStreamLogger( System.out ) );
120         Object mirror = CommonReflector.createConsoleLogger( logger, cl );
121         MatcherAssert.assertThat( mirror, is( notNullValue() ) );
122         MatcherAssert.assertThat( mirror.getClass().getInterfaces()[0].getName(), is( ConsoleLogger.class.getName() ) );
123         MatcherAssert.assertThat( mirror, is( not( sameInstance( (Object) logger ) ) ) );
124         MatcherAssert.assertThat( mirror, is( instanceOf( ConsoleLoggerDecorator.class ) ) );
125         invokeMethodWithArray( mirror, getMethod( mirror, "info", String.class ), "Hi There!" );
126         verify( logger, times( 1 ) ).info( "Hi There!" );
127     }
128 
129     @Test
130     public void testCreateConsoleLogger()
131     {
132         ClassLoader cl = Thread.currentThread().getContextClassLoader();
133         ConsoleLogger consoleLogger = mock( ConsoleLogger.class );
134         ConsoleLogger decorator = (ConsoleLogger) CommonReflector.createConsoleLogger( consoleLogger, cl );
135         assertThat( decorator )
136                 .isNotSameAs( consoleLogger );
137 
138         assertThat( decorator.isDebugEnabled() ).isFalse();
139         when( consoleLogger.isDebugEnabled() ).thenReturn( true );
140         assertThat( decorator.isDebugEnabled() ).isTrue();
141         verify( consoleLogger, times( 2 ) ).isDebugEnabled();
142 
143         decorator.info( "msg" );
144         ArgumentCaptor<String> argumentMsg = ArgumentCaptor.forClass( String.class );
145         verify( consoleLogger, times( 1 ) ).info( argumentMsg.capture() );
146         assertThat( argumentMsg.getAllValues() ).hasSize( 1 );
147         assertThat( argumentMsg.getAllValues().get( 0 ) ).isEqualTo( "msg" );
148     }
149 }