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