-
Notifications
You must be signed in to change notification settings - Fork 14
/
PmpMojo.java
380 lines (317 loc) · 11.6 KB
/
PmpMojo.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package org.pitest.maven;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.tooling.CombinedStatistics;
import eu.stamp_project.PmpContext;
import eu.stamp_project.plugins.PmpNonEmptyProjectCheck;
import eu.stamp_project.plugins.PmpProject;
import eu.stamp_project.report.MethodThresholds;
// **********************************************************************
@Mojo(name = "run", defaultPhase = LifecyclePhase.VERIFY,
requiresDependencyResolution = ResolutionScope.TEST, threadSafe
= true)
public class PmpMojo extends AbstractPitMojo
{
// **********************************************************************
// properties
// **********************************************************************
@Parameter(property = "targetModules")
protected ArrayList<String> targetModules;
@Parameter(property = "skippedModules")
protected ArrayList<String> skippedModules;
@Parameter(property = "targetDependencies")
protected ArrayList<String> targetDependencies;
@Parameter(property = "ignoredDependencies")
protected ArrayList<String> ignoredDependencies;
// If specified, modules before this module will be skipped.
@Parameter(property = "continueFromModule")
protected String continueFromModule;
private static Set<String> alreadyVisitedModules = new HashSet<>();
// if true: do not execute PIT, only display information about shouldRun or not
@Parameter(defaultValue = "false", property = "shouldDisplayOnly")
protected boolean _ShouldDisplayOnly;
/**
* Pseudo Tested threshold at which to fail build
*/
@Parameter(defaultValue = "0", property = "pseudoTestedThreshold")
private int pseudoTestedThreshold;
/**
* Partially Tested threshold at which to fail build
*/
@Parameter(defaultValue = "0", property = "partiallyTestedThreshold")
private int partiallyTestedThreshold;
// **********************************************************************
// public
// **********************************************************************
// ******** attributes
public File getBaseDir()
{
return(detectBaseDir());
}
// **********************************************************************
public boolean shouldDisplayOnly()
{
return(_ShouldDisplayOnly);
}
// **********************************************************************
public ArrayList<String> getTargetModules()
{
return(targetModules);
}
// **********
public void setTargetModules(ArrayList<String> newClasses)
{
targetModules = newClasses;
}
// **********
public boolean isInTargetModules(String name)
{
boolean result = false;
for (int i = 0; i < getTargetModules().size() && ! result; i++)
{
result = getTargetModules().get(i).equals(name);
}
return(result);
}
// **********************************************************************
public ArrayList<String> getSkippedModules()
{
return(skippedModules);
}
// **********
public void setSkippedModules(ArrayList<String> newClasses)
{
skippedModules = newClasses;
}
// **********
public boolean isInSkippedModules(MavenProject module)
{
return isInSkippedModules(module.getArtifactId());
}
public boolean isInSkippedModules(String name)
{
boolean result = false;
for (int i = 0; i < getSkippedModules().size() && ! result; i++)
{
result = getSkippedModules().get(i).equals(name);
}
return(result);
}
// **********************************************************************
public ArrayList<String> getTargetDependencies()
{
// cael: debug only
// if (targetDependencies == null)
// {
// System.out.println("######## !!!!!! targetDependencies == null");
// }
return(targetDependencies);
}
// **********
public void setTargetDependencies(ArrayList<String> moduleList)
{
targetDependencies = moduleList;
}
// **********
public boolean isInTargetDependencies(String name)
{
boolean result = false;
for (int i = 0; i < getTargetDependencies().size() && ! result; i++)
{
result = getTargetDependencies().get(i).equals(name);
}
return(result);
}
// **********************************************************************
public ArrayList<String> getIgnoredDependencies()
{
// cael: debug only
// if (ignoredDependencies == null)
// {
// System.out.println("######## !!!!!! ignoredDependencies == null");
// }
return(ignoredDependencies);
}
// **********
public void setIgnoredDependencies(ArrayList<String> moduleList)
{
ignoredDependencies = moduleList;
}
// **********
public boolean isInIgnoredDependencies(String name)
{
boolean result = false;
for (int i = 0; i < getIgnoredDependencies().size() && ! result; i++)
{
result = getIgnoredDependencies().get(i).equals(name);
}
return(result);
}
// **********************************************************************
public void setContinueFromModule(String continueFromModule)
{
this.continueFromModule = continueFromModule;
}
// **********
public String getContinueFromModule()
{
return continueFromModule;
}
// **********
public boolean isContinueFromModuleSatisfied(MavenProject module)
{
if (continueFromModule == null)
{
// property is not specified
return true;
}
// note that the current module has already been added
return alreadyVisitedModules.contains(continueFromModule);
}
// **********************************************************************
// ******** associations
public Predicate<Artifact> getFilter()
{
return(filter);
}
// **********************************************************************
public PluginServices getPlugins()
{
return(plugins);
}
// **********************************************************************
// ******** methods
public PmpMojo()
{
super(new RunPitStrategy(),
new DependencyFilter(new PluginServices(AbstractPitMojo.class.getClassLoader())),
new PluginServices(AbstractPitMojo.class.getClassLoader()),
new PmpNonEmptyProjectCheck());
}
// **********************************************************************
// ******** methods
public void updateTargetClasses()
{
// require(getProject() != null)
ArrayList<String> completeTargetClasses;
ArrayList<String> classList;
ArrayList<MavenProject> moduleList = null;
MavenProject mvnProject;
if (targetClasses == null)
{
targetClasses = new ArrayList<String>();
}
if (targetClasses.isEmpty())
// we need to get the explicit class list of the current project
{
classList = PmpContext.getClasses(getProject());
if (! classList.isEmpty())
{
targetClasses.addAll(classList);
}
}
// else just let the target classes specified in the pom.xml
// complete the target classes with other (dependencies) modules classes
// and add target classes of all getArtifacts which are a project module
moduleList = PmpContext.getInstance().getDependingModules(getProject());
for (int i = 0; i < moduleList.size(); i++)
{
MavenProject module = moduleList.get(i);
if ((getTargetDependencies().size() == 0 ||
isInTargetDependencies(module.getArtifactId())) &&
! isInIgnoredDependencies(module.getArtifactId()))
{
classList = PmpContext.getClasses(module);
if (! classList.isEmpty())
{
PmpContext.addNewStrings(targetClasses, classList);
}
}
}
}
// public void updateTargetTests()
// {
// if (targetTests == null || targetTests.isEmpty())
// {
// targetTests = new ArrayList<>();
// targetTests.addAll(PmpContext.getTestClasses(getProject()));
// }
// }
// **********************************************************************
// protected
// **********************************************************************
// ******** methods
@Override
protected Optional<CombinedStatistics> analyse()
throws MojoExecutionException
{
Optional<CombinedStatistics> result = null;
result = Optional.ofNullable(PmpContext.getInstance().getCurrentProject()
.execute());
return(result);
}
// **********************************************************************
// called at the beginning of AbstractPitMojo.execute
@Override
protected RunDecision shouldRun()
{
MethodThresholds.getInstance().setPartialyTestedThresold(partiallyTestedThreshold);
MethodThresholds.getInstance().setPseudoTestedThresold(pseudoTestedThreshold);
RunDecision theDecision;
PmpProject myPmpProject;
String projectName = getProject().getArtifactId();
alreadyVisitedModules.add(projectName);
// if no targetModules are specified, take all modules
boolean isTargetModule = (getTargetModules() == null ||
getTargetModules().isEmpty() || isInTargetModules(projectName));
String message;
PmpContext.getInstance().updateData(this);
myPmpProject = new PmpProject(this);
PmpContext.getInstance().setCurrentProject(myPmpProject);
updateTargetClasses();
// updateTargetTests();
if (getProject().getPackaging().equals("pom") &&
myPmpProject.hasTestCompileSourceRoots() &&
myPmpProject.hasCompileSourceRoots())
// force packaging to not pom before calling super.shouldRun
{
getProject().setPackaging("jar");
}
theDecision = super.shouldRun();
if (! isTargetModule)
{
message = projectName + " is not a target module";
theDecision.addReason(message);
}
if (isInSkippedModules(projectName))
{
message = projectName + " is a skipped module";
theDecision.addReason(message);
}
if (! isContinueFromModuleSatisfied(getProject()))
{
message = projectName + " is before " + continueFromModule +
" from which the execution shall be continued";
theDecision.addReason(message);
}
if (shouldDisplayOnly())
{
message = "Display only option is true";
theDecision.addReason(message);
}
// PitMojo displays the reasons about why we skip the project
return(theDecision);
}
}