-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPitestComputer.java
94 lines (85 loc) · 3.42 KB
/
PitestComputer.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
/*
* Sonar Pitest Plugin
* Copyright (C) 2009-2018 Vinod Anandan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.pitest;
import java.util.stream.Stream;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.ce.measure.Measure;
import org.sonar.api.ce.measure.MeasureComputer;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
/**
* MeasureComputer that processes the aggregated quantitative metric for a component from all the quantitative metrics
* of its children.
*/
@ComputeEngineSide
@ExtensionPoint
public class PitestComputer implements MeasureComputer {
private static final Logger log = Loggers.get(PitestComputer.class);
private static final String[] measureKeys = {PitestMetrics.MUTATIONS_NOT_COVERED_KEY,
PitestMetrics.MUTATIONS_GENERATED_KEY,
PitestMetrics.MUTATIONS_KILLED_KEY,
PitestMetrics.MUTATIONS_SURVIVED_KEY,
PitestMetrics.MUTATIONS_ERROR_KEY,
PitestMetrics.MUTATIONS_UNKNOWN_KEY
};
private static final String[] derivedKeys = {PitestMetrics.MUTATIONS_DATA_KEY,
PitestMetrics.MUTATIONS_KILLED_PERCENT_KEY};
@Override
public MeasureComputerDefinition define(final MeasureComputerDefinitionContext defContext) {
return defContext.newDefinitionBuilder()
.setOutputMetrics(Stream.of(measureKeys, derivedKeys).flatMap(Stream::of).toArray(String[]::new))
.build();
}
@Override
public void compute(final MeasureComputerContext context) {
for (String metricKey : measureKeys) {
if (context.getMeasure(metricKey) == null) {
computeChildrenMeasurements(context, metricKey);
}
}
computeDerived(context) ;
}
private void computeChildrenMeasurements(final MeasureComputerContext context, String key) {
if (context.getMeasure(key) == null) {
int sum = 0;
for (Measure m : context.getChildrenMeasures(key)) {
try {
sum += m.getIntValue();
} catch (IllegalStateException e) {
log.error("Failed to compute value for {}.", key, e);
}
}
context.addMeasure(key, sum);
}
}
private void computeDerived(final MeasureComputerContext context) {
final Measure mutationsTotal = context.getMeasure(PitestMetrics.MUTATIONS_GENERATED_KEY);
if (mutationsTotal != null) {
final Integer elements = mutationsTotal.getIntValue();
final Measure killed = context.getMeasure(PitestMetrics.MUTATIONS_KILLED_KEY);
if (elements > 0 && killed != null) {
final Integer coveredElements = killed.getIntValue();
final Double coverage = 100.0 * coveredElements / elements;
context.addMeasure(PitestMetrics.MUTATIONS_KILLED_PERCENT_KEY, coverage);
}
}
}
}