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 javax.annotation.Nonnull;
22
23 import java.io.File;
24 import java.lang.reflect.Constructor;
25
26 import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
27 import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
28 import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
29 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
30 import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
31 import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
32 import org.apache.maven.surefire.api.util.SurefireReflectionException;
33
34 import static org.apache.maven.surefire.api.util.ReflectionUtils.getConstructor;
35 import static org.apache.maven.surefire.api.util.ReflectionUtils.instantiateObject;
36 import static org.apache.maven.surefire.api.util.ReflectionUtils.newInstance;
37
38
39
40
41 public class CommonReflector {
42 private final Class<?> startupReportConfiguration;
43 private final Class<?> consoleLogger;
44 private final Class<?> statelessTestsetReporter;
45 private final Class<?> consoleOutputReporter;
46 private final Class<?> statelessTestsetInfoReporter;
47 private final ClassLoader surefireClassLoader;
48
49 public CommonReflector(@Nonnull ClassLoader surefireClassLoader) {
50 this.surefireClassLoader = surefireClassLoader;
51
52 try {
53 startupReportConfiguration = surefireClassLoader.loadClass(StartupReportConfiguration.class.getName());
54 consoleLogger = surefireClassLoader.loadClass(ConsoleLogger.class.getName());
55 statelessTestsetReporter = surefireClassLoader.loadClass(SurefireStatelessReporter.class.getName());
56 consoleOutputReporter = surefireClassLoader.loadClass(SurefireConsoleOutputReporter.class.getName());
57 statelessTestsetInfoReporter =
58 surefireClassLoader.loadClass(SurefireStatelessTestsetInfoReporter.class.getName());
59 } catch (ClassNotFoundException e) {
60 throw new SurefireReflectionException(e);
61 }
62 }
63
64 public Object createReportingReporterFactory(
65 @Nonnull StartupReportConfiguration startupReportConfiguration, @Nonnull ConsoleLogger consoleLogger) {
66 Class<?>[] args = {this.startupReportConfiguration, this.consoleLogger};
67 Object src = createStartupReportConfiguration(startupReportConfiguration);
68 Object logger = createConsoleLogger(consoleLogger, surefireClassLoader);
69 Object[] params = {src, logger};
70 return instantiateObject(DefaultReporterFactory.class.getName(), args, params, surefireClassLoader);
71 }
72
73 private Object createStartupReportConfiguration(@Nonnull StartupReportConfiguration reporterConfiguration) {
74 Constructor<?> constructor = getConstructor(
75 startupReportConfiguration,
76 boolean.class,
77 boolean.class,
78 String.class,
79 boolean.class,
80 File.class,
81 boolean.class,
82 String.class,
83 File.class,
84 boolean.class,
85 int.class,
86 String.class,
87 String.class,
88 boolean.class,
89 boolean.class,
90 boolean.class,
91 statelessTestsetReporter,
92 consoleOutputReporter,
93 statelessTestsetInfoReporter);
94 Object[] params = {
95 reporterConfiguration.isUseFile(),
96 reporterConfiguration.isPrintSummary(),
97 reporterConfiguration.getReportFormat(),
98 reporterConfiguration.isRedirectTestOutputToFile(),
99 reporterConfiguration.getReportsDirectory(),
100 reporterConfiguration.isTrimStackTrace(),
101 reporterConfiguration.getReportNameSuffix(),
102 reporterConfiguration.getStatisticsFile(),
103 reporterConfiguration.isRequiresRunHistory(),
104 reporterConfiguration.getRerunFailingTestsCount(),
105 reporterConfiguration.getXsdSchemaLocation(),
106 reporterConfiguration.getEncoding().name(),
107 reporterConfiguration.isForking(),
108 reporterConfiguration.isEnableOutErrElements(),
109 reporterConfiguration.isEnablePropertiesElement(),
110 reporterConfiguration.getXmlReporter().clone(surefireClassLoader),
111 reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader),
112 reporterConfiguration.getTestsetReporter().clone(surefireClassLoader)
113 };
114 return newInstance(constructor, params);
115 }
116
117 static Object createConsoleLogger(ConsoleLogger consoleLogger, ClassLoader cl) {
118 try {
119 Class<?> decoratorClass = cl.loadClass(ConsoleLoggerDecorator.class.getName());
120 return getConstructor(decoratorClass, Object.class).newInstance(consoleLogger);
121 } catch (Exception e) {
122 throw new SurefireReflectionException(e);
123 }
124 }
125 }