/
MavenJUnitPatcher.java
301 lines (273 loc) · 14.1 KB
/
MavenJUnitPatcher.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.maven.execution;
import com.intellij.execution.JUnitPatcher;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.ParametersList;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.CompilerModuleExtension;
import com.intellij.openapi.util.PropertiesUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import one.util.streamex.StreamEx;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.MavenDomUtil;
import org.jetbrains.idea.maven.dom.MavenPropertyResolver;
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
import org.jetbrains.idea.maven.model.MavenArtifact;
import org.jetbrains.idea.maven.model.MavenPlugin;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectSettings;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.project.MavenTestRunningSettings;
import org.jetbrains.idea.maven.utils.MavenJDOMUtil;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class MavenJUnitPatcher extends JUnitPatcher {
public static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{(.+?)}");
public static final Pattern ARG_LINE_PATTERN = Pattern.compile("@\\{(.+?)}");
private static final Logger LOG = Logger.getInstance(MavenJUnitPatcher.class);
private static final Set<String> EXCLUDE_SUBTAG_NAMES =
Set.of("classpathDependencyExclude", "classpathDependencyExcludes", "dependencyExclude");
// See org.apache.maven.artifact.resolver.filter.AbstractScopeArtifactFilter
private static final Map<String, List<String>> SCOPE_FILTER = Map.of(
"compile", Arrays.asList("system", "provided", "compile"),
"runtime", Arrays.asList("compile", "runtime"),
"compile+runtime", Arrays.asList("system", "provided", "compile", "runtime"),
"runtime+system", Arrays.asList("system", "compile", "runtime"),
"test", Arrays.asList("system", "provided", "compile", "runtime", "test"));
@Override
public void patchJavaParameters(@Nullable Module module, JavaParameters javaParameters) {
if (module == null) return;
MavenProject mavenProject = MavenProjectsManager.getInstance(module.getProject()).findProject(module);
if (mavenProject == null) return;
UnaryOperator<String> runtimeProperties = getDynamicConfigurationProperties(module, mavenProject, javaParameters);
configureFromPlugin(module, javaParameters, mavenProject, runtimeProperties, "maven-surefire-plugin", "surefire");
configureFromPlugin(module, javaParameters, mavenProject, runtimeProperties, "maven-failsafe-plugin", "failsafe");
}
private static void configureFromPlugin(@NotNull Module module,
JavaParameters javaParameters,
MavenProject mavenProject,
UnaryOperator<String> runtimeProperties,
String pluginArtifact,
String pluginName) {
MavenPlugin plugin = mavenProject.findPlugin("org.apache.maven.plugins", pluginArtifact);
if (plugin != null) {
Element config = mavenProject.getPluginGoalConfiguration(plugin, null);
if (config == null) {
config = new Element("configuration");
}
patchJavaParameters(module, javaParameters, mavenProject, pluginName, config, runtimeProperties);
}
}
private static UnaryOperator<String> getDynamicConfigurationProperties(Module module,
MavenProject mavenProject,
JavaParameters javaParameters) {
MavenDomProjectModel domModel = MavenDomUtil.getMavenDomProjectModel(module.getProject(), mavenProject.getFile());
if (domModel == null) {
return s -> s;
}
var staticProperties = MavenPropertyResolver.collectPropertyMapFromDOM(mavenProject, domModel);
Properties modelProperties = mavenProject.getProperties();
String jaCoCoConfigProperty = getJaCoCoArgLineProperty(mavenProject);
ParametersList vmParameters = javaParameters.getVMParametersList();
return name -> {
String vmPropertyValue = vmParameters.getPropertyValue(name);
if (vmPropertyValue != null) {
return vmPropertyValue;
}
String staticPropertyValue = staticProperties.get(name);
if (staticPropertyValue != null) {
return MavenPropertyResolver.resolve(staticPropertyValue, domModel);
}
String modelPropertyValue = modelProperties.getProperty(name);
if (modelPropertyValue != null) {
return modelPropertyValue;
}
if (name.equals(jaCoCoConfigProperty)) {
return "";
}
return null;
};
}
private static String getJaCoCoArgLineProperty(MavenProject mavenProject) {
String jaCoCoConfigProperty = "argLine";
Element jaCoCoConfig = mavenProject.getPluginConfiguration("org.jacoco", "jacoco-maven-plugin");
if (jaCoCoConfig != null) {
Element propertyName = jaCoCoConfig.getChild("propertyName");
if (propertyName != null) {
jaCoCoConfigProperty = propertyName.getTextTrim();
}
}
Element jaCoCoGoalConfig = mavenProject.getPluginGoalConfiguration("org.jacoco", "jacoco-maven-plugin", "prepare-agent");
if (jaCoCoGoalConfig != null) {
Element propertyName = jaCoCoGoalConfig.getChild("propertyName");
if (propertyName != null) {
jaCoCoConfigProperty = propertyName.getTextTrim();
}
}
return jaCoCoConfigProperty;
}
private static void patchJavaParameters(@NotNull Module module,
@NotNull JavaParameters javaParameters,
@NotNull MavenProject mavenProject,
@NotNull String plugin,
@NotNull Element config,
@NotNull UnaryOperator<String> runtimeProperties) {
MavenDomProjectModel domModel = MavenDomUtil.getMavenDomProjectModel(module.getProject(), mavenProject.getFile());
MavenTestRunningSettings testRunningSettings = MavenProjectSettings.getInstance(module.getProject()).getTestRunningSettings();
List<String> paths = MavenJDOMUtil.findChildrenValuesByPath(config, "additionalClasspathElements", "additionalClasspathElement");
if (!paths.isEmpty()) {
for (String pathLine : paths) {
for (String path : pathLine.split(",")) {
javaParameters.getClassPath().add(resolvePluginProperties(plugin, path.trim(), domModel));
}
}
}
List<String> excludes = getExcludedCoordinates(config);
String scopeExclude = MavenJDOMUtil.findChildValueByPath(config, "classpathDependencyScopeExclude");
MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(module.getProject());
if (scopeExclude != null || !excludes.isEmpty()) {
for (MavenArtifact dependency : mavenProject.getDependencies()) {
if (scopeExclude!=null && SCOPE_FILTER.getOrDefault(scopeExclude, Collections.emptyList()).contains(dependency.getScope()) ||
excludes.contains(dependency.getGroupId() + ":" + dependency.getArtifactId())) {
File file = dependency.getFile();
javaParameters.getClassPath().remove(file.getAbsolutePath());
Optional.ofNullable(mavenProjectsManager.findProject(dependency.getMavenId()))
.map(mavenProjectsManager::findModule)
.map(CompilerModuleExtension::getInstance)
.map(CompilerModuleExtension::getCompilerOutputPath)
.ifPresent(compilerOutputPath -> javaParameters.getClassPath().remove(compilerOutputPath));
}
}
}
if (testRunningSettings.isPassSystemProperties()) {
if (isEnabled(plugin, "systemPropertyVariables")) {
Element systemPropertyVariables = config.getChild("systemPropertyVariables");
if (systemPropertyVariables != null) {
for (Element element : systemPropertyVariables.getChildren()) {
String propertyName = element.getName();
if (!javaParameters.getVMParametersList().hasProperty(propertyName)) {
String value = resolvePluginProperties(plugin, element.getValue(), domModel);
value = resolveVmProperties(javaParameters.getVMParametersList(), value);
if (isResolved(plugin, value)) {
javaParameters.getVMParametersList().addProperty(propertyName, value);
}
}
}
}
}
if (isEnabled(plugin, "systemPropertiesFile")) {
Element systemPropertiesFile = config.getChild("systemPropertiesFile");
if (systemPropertiesFile != null) {
String systemPropertiesFilePath = systemPropertiesFile.getTextTrim();
if (StringUtil.isNotEmpty(systemPropertiesFilePath) && !FileUtil.isAbsolute(systemPropertiesFilePath)) {
systemPropertiesFilePath = mavenProject.getDirectory() + '/' + systemPropertiesFilePath;
}
if (StringUtil.isNotEmpty(systemPropertiesFilePath) && new File(systemPropertiesFilePath).exists()) {
try (Reader fis = Files.newBufferedReader(Paths.get(systemPropertiesFilePath), StandardCharsets.ISO_8859_1)) {
Map<String, String> properties = PropertiesUtil.loadProperties(fis);
properties.forEach((pName, pValue) -> javaParameters.getVMParametersList().addProperty(pName, pValue));
}
catch (IOException e) {
LOG.warn("Can't read property file '" + systemPropertiesFilePath + "': " + e.getMessage());
}
}
}
}
}
if (testRunningSettings.isPassEnvironmentVariables() && isEnabled(plugin, "environmentVariables")) {
Element environmentVariables = config.getChild("environmentVariables");
if (environmentVariables != null) {
for (Element element : environmentVariables.getChildren()) {
String variableName = element.getName();
if (!javaParameters.getEnv().containsKey(variableName)) {
String value = resolvePluginProperties(plugin, element.getValue(), domModel);
value = resolveVmProperties(javaParameters.getVMParametersList(), value);
if (isResolved(plugin, value)) {
javaParameters.addEnv(variableName, value);
}
}
}
}
}
if (testRunningSettings.isPassArgLine() && isEnabled(plugin, "argLine")) {
Element argLine = config.getChild("argLine");
String propertyText = argLine != null ? argLine.getTextTrim() : "${argLine}";
String value = resolvePluginProperties(plugin, propertyText, domModel);
value = resolveVmProperties(javaParameters.getVMParametersList(), value);
if (StringUtil.isNotEmpty(value) && isResolved(plugin, value)) {
value = resolveRuntimeProperties(value, runtimeProperties);
javaParameters.getVMParametersList().addParametersString(value);
}
}
}
private static String resolveRuntimeProperties(String value, UnaryOperator<String> runtimeProperties) {
Matcher matcher = ARG_LINE_PATTERN.matcher(value);
StringBuilder sb = new StringBuilder();
while (matcher.find()) {
String replacement = runtimeProperties.apply(matcher.group(1));
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement == null ? matcher.group() : replacement));
}
matcher.appendTail(sb);
return sb.toString();
}
@NotNull
private static List<String> getExcludedCoordinates(@NotNull Element config) {
Element excludesElement = config.getChild("classpathDependencyExcludes");
if (excludesElement == null) {
return Collections.emptyList();
}
String rawText = excludesElement.getTextTrim();
List<String> excludes = new ArrayList<>();
if (!rawText.isEmpty()) {
StreamEx.split(rawText, ',').map(String::trim).into(excludes);
}
for (Element child : excludesElement.getChildren()) {
String name = child.getName();
if (name != null && EXCLUDE_SUBTAG_NAMES.contains(name)) {
String excludeItem = child.getTextTrim();
if (!excludeItem.isEmpty()) {
StreamEx.split(excludeItem, ',').map(String::trim).into(excludes);
}
}
}
return excludes;
}
private static String resolvePluginProperties(@NotNull String plugin, @NotNull String value, @Nullable MavenDomProjectModel domModel) {
if (domModel != null) {
value = MavenPropertyResolver.resolve(value, domModel);
}
return value.replaceAll("\\$\\{" + plugin + "\\.(forkNumber|threadNumber)}", "1");
}
private static String resolveVmProperties(@NotNull ParametersList vmParameters, @NotNull String value) {
Matcher matcher = PROPERTY_PATTERN.matcher(value);
Map<String, String> toReplace = new HashMap<>();
while (matcher.find()) {
String finding = matcher.group();
final String propertyValue = vmParameters.getPropertyValue(finding.substring(2, finding.length() - 1));
if(propertyValue == null) continue;
toReplace.put(finding, propertyValue);
}
for (Map.Entry<String, String> entry : toReplace.entrySet()) {
value = value.replace(entry.getKey(), entry.getValue());
}
return value;
}
private static boolean isEnabled(String plugin, String s) {
return !Boolean.parseBoolean(System.getProperty("idea.maven." + plugin + ".disable." + s));
}
private static boolean isResolved(String plugin, String s) {
return !s.contains("${") || Boolean.parseBoolean(System.getProperty("idea.maven." + plugin + ".allPropertiesAreResolved"));
}
}