-
Notifications
You must be signed in to change notification settings - Fork 349
/
BlockCoverageDataLoader.java
92 lines (80 loc) · 3.24 KB
/
BlockCoverageDataLoader.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
package org.pitest.aggregate;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.pitest.classinfo.ClassName;
import org.pitest.coverage.BlockCoverage;
import org.pitest.coverage.BlockLocation;
import org.pitest.mutationtest.engine.Location;
class BlockCoverageDataLoader extends DataLoader<BlockCoverage> {
private static final String BLOCK = "block";
private static final String TESTS = "tests";
private static final String TEST = "test";
private static final String NAME = "name";
private static final String METHOD = "method";
private static final String CLASSNAME = "classname";
private static final String NUMBER = "number";
private static final String OPEN_PAREN = "(";
BlockCoverageDataLoader(final Collection<File> filesToLoad) {
super(filesToLoad);
}
@Override
protected Set<BlockCoverage> mapToData(XMLEventReader doc) throws XMLStreamException {
final Set<BlockCoverage> data = new HashSet<>();
List<String> tests = new ArrayList<>();
StartElement enclosingNode = null;
BlockLocation block = null;
while (doc.hasNext()) {
XMLEvent next = doc.peek();
if (next.isStartElement()) {
enclosingNode = next.asStartElement();
String nodeName = next.asStartElement().getName().getLocalPart();
if (nodeName.equals(BLOCK)) {
block = toBlockLocation(enclosingNode);
}
if (nodeName.equals(TESTS)) {
tests = new ArrayList<>();
}
if (nodeName.equals(TEST)) {
tests.add(getAttributeValue(enclosingNode, NAME));
}
}
if (next.isEndElement()) {
String nodeName = next.asEndElement().getName().getLocalPart();
if (nodeName.equals(BLOCK)) {
data.add(new BlockCoverage(block, tests.isEmpty() ? null : tests));
}
}
doc.next();
}
return data;
}
private BlockLocation toBlockLocation(StartElement enclosingNode) {
BlockLocation block;
ClassName className = ClassName.fromString(getAttributeValue(enclosingNode, CLASSNAME));
String method = getAttributeValue(enclosingNode, METHOD);
String methodName = method.substring(0, method.indexOf(OPEN_PAREN));
int blockNum = getAttributeValueAsInt(enclosingNode, NUMBER);
String methodDesc = method.substring(method.indexOf(OPEN_PAREN));
Location location = new Location(className, methodName, methodDesc);
block = new BlockLocation(location, blockNum);
return block;
}
private String getAttributeValue(StartElement enclosingNode, String attributeName) {
Attribute attribute = enclosingNode.getAttributeByName(QName.valueOf(attributeName));
return attribute == null ? null : attribute.getValue();
}
private int getAttributeValueAsInt(StartElement enclosingNode, String attributeName) {
String value = getAttributeValue(enclosingNode, attributeName);
return value == null ? 0 : Integer.parseInt(value);
}
}