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