-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
TestNGTestFramework.java
212 lines (189 loc) · 8.25 KB
/
TestNGTestFramework.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.tasks.testing.testng;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.internal.tasks.testing.TestClassLoaderFactory;
import org.gradle.api.internal.tasks.testing.TestClassProcessor;
import org.gradle.api.internal.tasks.testing.TestFramework;
import org.gradle.api.internal.tasks.testing.WorkerTestClassProcessorFactory;
import org.gradle.api.internal.tasks.testing.detection.ClassFileExtractionManager;
import org.gradle.api.internal.tasks.testing.filter.DefaultTestFilter;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.reporting.DirectoryReport;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.testing.testng.TestNGOptions;
import org.gradle.internal.Factory;
import org.gradle.internal.actor.ActorFactory;
import org.gradle.internal.id.IdGenerator;
import org.gradle.internal.scan.UsedByScanPlugin;
import org.gradle.internal.service.ServiceRegistry;
import org.gradle.internal.time.Clock;
import org.gradle.process.internal.worker.WorkerProcessBuilder;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
@UsedByScanPlugin("test-retry")
public class TestNGTestFramework implements TestFramework {
private final TestNGOptions options;
private TestNGDetector detector;
private final DefaultTestFilter filter;
private final ObjectFactory objects;
private final String testTaskPath;
private final FileCollection testTaskClasspath;
private final Factory<File> testTaskTemporaryDir;
private transient ClassLoader testClassLoader;
@UsedByScanPlugin("test-retry")
public TestNGTestFramework(final Test testTask, FileCollection classpath, DefaultTestFilter filter, ObjectFactory objects) {
this.filter = filter;
this.objects = objects;
this.testTaskPath = testTask.getPath();
this.testTaskClasspath = classpath;
this.testTaskTemporaryDir = testTask.getTemporaryDirFactory();
options = objects.newInstance(TestNGOptions.class);
conventionMapOutputDirectory(options, testTask.getReports().getHtml());
detector = new TestNGDetector(new ClassFileExtractionManager(testTask.getTemporaryDirFactory()));
}
private static void conventionMapOutputDirectory(TestNGOptions options, final DirectoryReport html) {
new DslObject(options).getConventionMapping().map("outputDirectory", new Callable<File>() {
@Override
public File call() {
return html.getOutputLocation().getAsFile().getOrNull();
}
});
}
@Override
public WorkerTestClassProcessorFactory getProcessorFactory() {
verifyConfigFailurePolicy();
verifyPreserveOrder();
verifyGroupByInstances();
List<File> suiteFiles = options.getSuites(testTaskTemporaryDir.create());
TestNGSpec spec = new TestNGSpec(options, filter);
return new TestClassProcessorFactoryImpl(this.options.getOutputDirectory(), spec, suiteFiles);
}
private void verifyConfigFailurePolicy() {
if (!options.getConfigFailurePolicy().equals(TestNGOptions.DEFAULT_CONFIG_FAILURE_POLICY)) {
String message = String.format("The version of TestNG used does not support setting config failure policy to '%s'.", options.getConfigFailurePolicy());
try {
// for TestNG v6.9.12 and higher
Class<?> failurePolicyEnum = maybeLoadFailurePolicyEnum();
verifyMethodExists("setConfigFailurePolicy", failurePolicyEnum, message);
} catch (ClassNotFoundException cnfe) {
// fallback to legacy String argument
verifyMethodExists("setConfigFailurePolicy", String.class, message);
}
}
}
private void verifyPreserveOrder() {
if (options.getPreserveOrder()) {
verifyMethodExists("setPreserveOrder", boolean.class, "Preserving the order of tests is not supported by this version of TestNG.");
}
}
private void verifyGroupByInstances() {
if (options.getGroupByInstances()) {
verifyMethodExists("setGroupByInstances", boolean.class, "Grouping tests by instances is not supported by this version of TestNG.");
}
}
private void verifyMethodExists(String methodName, Class<?> parameterType, String failureMessage) {
try {
createTestNg().getMethod(methodName, parameterType);
} catch (NoSuchMethodException e) {
throw new InvalidUserDataException(failureMessage, e);
}
}
private Class<?> createTestNg() {
maybeInitTestClassLoader();
try {
return loadClass("org.testng.TestNG");
} catch (ClassNotFoundException e) {
throw new GradleException("Could not load TestNG.", e);
}
}
/**
* Use reflection to load {@link org.testng.xml.XmlSuite.FailurePolicy}, added in TestNG 6.9.12
* @return reference to class, if exists.
* @throws ClassNotFoundException if using older TestNG version.
*/
protected Class<?> maybeLoadFailurePolicyEnum() throws ClassNotFoundException {
return loadClass("org.testng.xml.XmlSuite$FailurePolicy");
}
private Class<?> loadClass(String clazz) throws ClassNotFoundException {
maybeInitTestClassLoader();
return testClassLoader.loadClass(clazz);
}
private void maybeInitTestClassLoader() {
if (testClassLoader == null) {
TestClassLoaderFactory factory = objects.newInstance(
TestClassLoaderFactory.class,
testTaskPath,
testTaskClasspath
);
testClassLoader = factory.create();
}
}
@Override
public Action<WorkerProcessBuilder> getWorkerConfigurationAction() {
return new Action<WorkerProcessBuilder>() {
@Override
public void execute(WorkerProcessBuilder workerProcessBuilder) {
workerProcessBuilder.sharedPackages("org.testng");
}
};
}
@Override
public List<String> getTestWorkerImplementationModules() {
return Collections.emptyList();
}
@Override
public String getDisplayName() {
return "Test NG";
}
@Override
public TestNGOptions getOptions() {
return options;
}
@Override
public TestNGDetector getDetector() {
return detector;
}
@Override
public void close() throws IOException {
// Clear expensive state from the test framework to avoid holding on to memory
// This should probably be a part of the test task and managed there.
testClassLoader = null;
detector = null;
}
private static class TestClassProcessorFactoryImpl implements WorkerTestClassProcessorFactory, Serializable {
private final File testReportDir;
private final TestNGSpec options;
private final List<File> suiteFiles;
public TestClassProcessorFactoryImpl(File testReportDir, TestNGSpec options, List<File> suiteFiles) {
this.testReportDir = testReportDir;
this.options = options;
this.suiteFiles = suiteFiles;
}
@Override
public TestClassProcessor create(ServiceRegistry serviceRegistry) {
return new TestNGTestClassProcessor(testReportDir, options, suiteFiles, serviceRegistry.get(IdGenerator.class), serviceRegistry.get(Clock.class), serviceRegistry.get(ActorFactory.class));
}
}
}