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 import java.lang.reflect.Field;
23
24 import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
25 import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
26 import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
27 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
28 import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
29 import org.apache.maven.plugin.surefire.log.api.PrintStreamLogger;
30 import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
31 import org.apache.maven.surefire.api.report.ReporterFactoryOptions;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.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.mockito.Mockito.mock;
41 import static org.mockito.Mockito.spy;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.when;
45
46
47
48
49 public class CommonReflectorTest {
50
51 @SuppressWarnings("unchecked")
52 private static <T> T getInternalState(Object target, String fieldName) {
53 try {
54 Class<?> clazz = target.getClass();
55 while (clazz != null) {
56 try {
57 Field field = clazz.getDeclaredField(fieldName);
58 field.setAccessible(true);
59 return (T) field.get(target);
60 } catch (NoSuchFieldException e) {
61 clazz = clazz.getSuperclass();
62 }
63 }
64 throw new NoSuchFieldException(fieldName);
65 } catch (Exception e) {
66 throw new RuntimeException(e);
67 }
68 }
69
70 private StartupReportConfiguration startupReportConfiguration;
71 private ConsoleLogger consoleLogger;
72 private File reportsDirectory;
73 private File statistics;
74 private SurefireStatelessReporter xmlReporter;
75 private SurefireConsoleOutputReporter consoleOutputReporter = new SurefireConsoleOutputReporter();
76 private SurefireStatelessTestsetInfoReporter infoReporter = new SurefireStatelessTestsetInfoReporter();
77
78 @BeforeEach
79 public void setup() {
80 File target = new File(System.getProperty("user.dir"), "target");
81 reportsDirectory = new File(target, "tmp6");
82 statistics = new File(reportsDirectory, "TESTHASH");
83 xmlReporter = new SurefireStatelessReporter();
84 infoReporter = new SurefireStatelessTestsetInfoReporter();
85
86 startupReportConfiguration = new StartupReportConfiguration(
87 true,
88 true,
89 "PLAIN",
90 false,
91 reportsDirectory,
92 false,
93 null,
94 statistics,
95 false,
96 1,
97 null,
98 null,
99 false,
100 true,
101 true,
102 false,
103 xmlReporter,
104 consoleOutputReporter,
105 infoReporter,
106 new ReporterFactoryOptions());
107
108 consoleLogger = mock(ConsoleLogger.class);
109 }
110
111 @Test
112 public void createReportingReporterFactory() {
113 CommonReflector reflector = new CommonReflector(Thread.currentThread().getContextClassLoader());
114 DefaultReporterFactory factory = (DefaultReporterFactory)
115 reflector.createReportingReporterFactory(startupReportConfiguration, consoleLogger);
116
117 assertThat(factory).isNotNull();
118
119 StartupReportConfiguration reportConfiguration = getInternalState(factory, "reportConfiguration");
120 assertThat(reportConfiguration).isNotSameAs(startupReportConfiguration);
121 assertThat(reportConfiguration.isUseFile()).isTrue();
122 assertThat(reportConfiguration.isPrintSummary()).isTrue();
123 assertThat(reportConfiguration.getReportFormat()).isEqualTo("PLAIN");
124 assertThat(reportConfiguration.isRedirectTestOutputToFile()).isFalse();
125 assertThat(reportConfiguration.getReportsDirectory()).isSameAs(reportsDirectory);
126 assertThat(reportConfiguration.isTrimStackTrace()).isFalse();
127 assertThat(reportConfiguration.getReportNameSuffix()).isNull();
128 assertThat(reportConfiguration.getStatisticsFile()).isSameAs(statistics);
129 assertThat(reportConfiguration.isRequiresRunHistory()).isFalse();
130 assertThat(reportConfiguration.getRerunFailingTestsCount()).isEqualTo(1);
131 assertThat(reportConfiguration.getXsdSchemaLocation()).isNull();
132 assertThat(reportConfiguration.getEncoding()).isEqualTo(UTF_8);
133 assertThat(reportConfiguration.isForking()).isFalse();
134 assertThat(reportConfiguration.getXmlReporter().toString()).isEqualTo(xmlReporter.toString());
135 assertThat(reportConfiguration.getTestsetReporter().toString()).isEqualTo(infoReporter.toString());
136 assertThat(reportConfiguration.getConsoleOutputReporter().toString())
137 .isEqualTo(consoleOutputReporter.toString());
138 }
139
140 @Test
141 public void shouldProxyConsoleLogger() {
142 ClassLoader cl = Thread.currentThread().getContextClassLoader();
143 ConsoleLogger logger = spy(new PrintStreamLogger(System.out));
144 Object mirror = CommonReflector.createConsoleLogger(logger, cl);
145 assertThat(mirror).isNotNull();
146 assertThat(mirror.getClass().getInterfaces()[0].getName()).isEqualTo(ConsoleLogger.class.getName());
147 assertThat(mirror).isNotSameAs(logger);
148 assertThat(mirror).isInstanceOf(ConsoleLoggerDecorator.class);
149 invokeMethodWithArray(mirror, getMethod(mirror, "info", String.class), "Hi There!");
150 verify(logger, times(1)).info("Hi There!");
151 }
152
153 @Test
154 public void testCreateConsoleLogger() {
155 ClassLoader cl = Thread.currentThread().getContextClassLoader();
156 ConsoleLogger consoleLogger = mock(ConsoleLogger.class);
157 ConsoleLogger decorator = (ConsoleLogger) CommonReflector.createConsoleLogger(consoleLogger, cl);
158 assertThat(decorator).isNotSameAs(consoleLogger);
159
160 assertThat(decorator.isDebugEnabled()).isFalse();
161 when(consoleLogger.isDebugEnabled()).thenReturn(true);
162 assertThat(decorator.isDebugEnabled()).isTrue();
163 verify(consoleLogger, times(2)).isDebugEnabled();
164
165 decorator.info("msg");
166 ArgumentCaptor<String> argumentMsg = ArgumentCaptor.forClass(String.class);
167 verify(consoleLogger, times(1)).info(argumentMsg.capture());
168 assertThat(argumentMsg.getAllValues()).hasSize(1);
169 assertThat(argumentMsg.getAllValues().get(0)).isEqualTo("msg");
170 }
171 }