1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 false,
89 xmlReporter,
90 consoleOutputReporter,
91 infoReporter,
92 new ReporterFactoryOptions());
93
94 consoleLogger = mock(ConsoleLogger.class);
95 }
96
97 @Test
98 public void createReportingReporterFactory() {
99 CommonReflector reflector = new CommonReflector(Thread.currentThread().getContextClassLoader());
100 DefaultReporterFactory factory = (DefaultReporterFactory)
101 reflector.createReportingReporterFactory(startupReportConfiguration, consoleLogger);
102
103 assertThat(factory).isNotNull();
104
105 StartupReportConfiguration reportConfiguration = getInternalState(factory, "reportConfiguration");
106 assertThat(reportConfiguration).isNotSameAs(startupReportConfiguration);
107 assertThat(reportConfiguration.isUseFile()).isTrue();
108 assertThat(reportConfiguration.isPrintSummary()).isTrue();
109 assertThat(reportConfiguration.getReportFormat()).isEqualTo("PLAIN");
110 assertThat(reportConfiguration.isRedirectTestOutputToFile()).isFalse();
111 assertThat(reportConfiguration.getReportsDirectory()).isSameAs(reportsDirectory);
112 assertThat(reportConfiguration.isTrimStackTrace()).isFalse();
113 assertThat(reportConfiguration.getReportNameSuffix()).isNull();
114 assertThat(reportConfiguration.getStatisticsFile()).isSameAs(statistics);
115 assertThat(reportConfiguration.isRequiresRunHistory()).isFalse();
116 assertThat(reportConfiguration.getRerunFailingTestsCount()).isEqualTo(1);
117 assertThat(reportConfiguration.getXsdSchemaLocation()).isNull();
118 assertThat(reportConfiguration.getEncoding()).isEqualTo(UTF_8);
119 assertThat(reportConfiguration.isForking()).isFalse();
120 assertThat(reportConfiguration.getXmlReporter().toString()).isEqualTo(xmlReporter.toString());
121 assertThat(reportConfiguration.getTestsetReporter().toString()).isEqualTo(infoReporter.toString());
122 assertThat(reportConfiguration.getConsoleOutputReporter().toString())
123 .isEqualTo(consoleOutputReporter.toString());
124 }
125
126 @Test
127 public void shouldProxyConsoleLogger() {
128 ClassLoader cl = Thread.currentThread().getContextClassLoader();
129 ConsoleLogger logger = spy(new PrintStreamLogger(System.out));
130 Object mirror = CommonReflector.createConsoleLogger(logger, cl);
131 MatcherAssert.assertThat(mirror, is(notNullValue()));
132 MatcherAssert.assertThat(mirror.getClass().getInterfaces()[0].getName(), is(ConsoleLogger.class.getName()));
133 MatcherAssert.assertThat(mirror, is(not(sameInstance((Object) logger))));
134 MatcherAssert.assertThat(mirror, is(instanceOf(ConsoleLoggerDecorator.class)));
135 invokeMethodWithArray(mirror, getMethod(mirror, "info", String.class), "Hi There!");
136 verify(logger, times(1)).info("Hi There!");
137 }
138
139 @Test
140 public void testCreateConsoleLogger() {
141 ClassLoader cl = Thread.currentThread().getContextClassLoader();
142 ConsoleLogger consoleLogger = mock(ConsoleLogger.class);
143 ConsoleLogger decorator = (ConsoleLogger) CommonReflector.createConsoleLogger(consoleLogger, cl);
144 assertThat(decorator).isNotSameAs(consoleLogger);
145
146 assertThat(decorator.isDebugEnabled()).isFalse();
147 when(consoleLogger.isDebugEnabled()).thenReturn(true);
148 assertThat(decorator.isDebugEnabled()).isTrue();
149 verify(consoleLogger, times(2)).isDebugEnabled();
150
151 decorator.info("msg");
152 ArgumentCaptor<String> argumentMsg = ArgumentCaptor.forClass(String.class);
153 verify(consoleLogger, times(1)).info(argumentMsg.capture());
154 assertThat(argumentMsg.getAllValues()).hasSize(1);
155 assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("msg");
156 }
157 }