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 statelessTestsetReporter,
90 consoleOutputReporter,
91 statelessTestsetInfoReporter);
92 Object[] params = {
93 reporterConfiguration.isUseFile(),
94 reporterConfiguration.isPrintSummary(),
95 reporterConfiguration.getReportFormat(),
96 reporterConfiguration.isRedirectTestOutputToFile(),
97 reporterConfiguration.getReportsDirectory(),
98 reporterConfiguration.isTrimStackTrace(),
99 reporterConfiguration.getReportNameSuffix(),
100 reporterConfiguration.getStatisticsFile(),
101 reporterConfiguration.isRequiresRunHistory(),
102 reporterConfiguration.getRerunFailingTestsCount(),
103 reporterConfiguration.getXsdSchemaLocation(),
104 reporterConfiguration.getEncoding().name(),
105 reporterConfiguration.isForking(),
106 reporterConfiguration.getXmlReporter().clone(surefireClassLoader),
107 reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader),
108 reporterConfiguration.getTestsetReporter().clone(surefireClassLoader)
109 };
110 return newInstance(constructor, params);
111 }
112
113 static Object createConsoleLogger(ConsoleLogger consoleLogger, ClassLoader cl) {
114 try {
115 Class<?> decoratorClass = cl.loadClass(ConsoleLoggerDecorator.class.getName());
116 return getConstructor(decoratorClass, Object.class).newInstance(consoleLogger);
117 } catch (Exception e) {
118 throw new SurefireReflectionException(e);
119 }
120 }
121 }