-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathParserRegistryTest.java
More file actions
125 lines (103 loc) · 5.26 KB
/
Copy pathParserRegistryTest.java
File metadata and controls
125 lines (103 loc) · 5.26 KB
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
package edu.hm.hafner.analysis.registry;
import org.junit.jupiter.api.Test;
import edu.hm.hafner.analysis.FileReaderFactory;
import edu.hm.hafner.analysis.Report.IssueType;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.registry.ParserDescriptor.Option;
import edu.hm.hafner.util.ResourceTest;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import static edu.hm.hafner.analysis.assertions.Assertions.*;
/**
* Tests the class {@link ParserRegistry}.
*
* @author Ullrich Hafner
*/
class ParserRegistryTest extends ResourceTest {
// Note for parser developers: if you add a new parser,
// please check if you are using the correct type and increment the corresponding count
private static final long WARNING_PARSERS_COUNT = 154L;
private static final long BUG_PARSERS_COUNT = 3L;
private static final long VULNERABILITY_PARSERS_COUNT = 17L;
private static final long DUPLICATION_PARSERS_COUNT = 3L;
private static final String SPOTBUGS = "spotbugs";
private static final String CHECKSTYLE = "checkstyle";
private static final String PMD = "pmd";
@Test
void shouldThrowExceptionIfParserNotFound() {
var parserRegistry = new ParserRegistry();
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> parserRegistry.get("-"));
}
/**
* Ensures that new parsers have the correct type assigned.
*/
@Test
void shouldAssignCorrectParserType() {
var parserRegistry = new ParserRegistry();
var typeCountMap = parserRegistry.getAllDescriptors().stream()
.collect(Collectors.groupingBy(ParserDescriptor::getType, Collectors.counting()));
assertThat(typeCountMap)
.containsEntry(IssueType.WARNING, WARNING_PARSERS_COUNT)
.containsEntry(IssueType.BUG, BUG_PARSERS_COUNT)
.containsEntry(IssueType.VULNERABILITY, VULNERABILITY_PARSERS_COUNT)
.containsEntry(IssueType.DUPLICATION, DUPLICATION_PARSERS_COUNT);
}
@Test
void shouldFindSomeParsers() {
var parserRegistry = new ParserRegistry();
assertThat(parserRegistry).hasIds(SPOTBUGS, CHECKSTYLE, PMD).hasNames("SpotBugs", "CheckStyle", "PMD");
assertThat(parserRegistry.get(SPOTBUGS))
.hasId(SPOTBUGS)
.hasName("SpotBugs")
.hasType(IssueType.BUG);
assertThat(parserRegistry.get("detectify"))
.hasId("detectify")
.hasName("Detectify")
.hasType(IssueType.VULNERABILITY);
assertThat(parserRegistry.get("owasp-dependency-check"))
.hasName("OWASP Dependency Check")
.hasType(IssueType.VULNERABILITY);
assertThat(parserRegistry.contains(SPOTBUGS)).isTrue();
assertThat(parserRegistry.contains("nothing")).isFalse();
List<ParserDescriptor> descriptors = parserRegistry.getAllDescriptors();
assertThat(descriptors).filteredOn(d -> "spotbugs".equals(d.getId())).hasSize(1);
descriptors.forEach(d -> assertThat(d.create()).isNotNull());
}
@Test
void shouldConfigureCpdParser() {
var parserRegistry = new ParserRegistry();
var cpdDescriptor = parserRegistry.get("cpd");
assertThat(cpdDescriptor).hasType(IssueType.DUPLICATION).hasName("CPD");
var parser = cpdDescriptor.create();
var report = parser.parse(new FileReaderFactory(getResourceAsFile("one-cpd.xml")));
assertThat(report).hasSize(2).hasSeverities(Severity.WARNING_NORMAL);
var highParser = cpdDescriptor.create(
new Option(CpdDescriptor.HIGH_OPTION_KEY, "20"),
new Option(CpdDescriptor.NORMAL_OPTION_KEY, "10"));
var highReport = highParser.parse(new FileReaderFactory(getResourceAsFile("one-cpd.xml")));
assertThat(highReport).hasSize(2).hasSeverities(Severity.WARNING_HIGH);
var lowParser = cpdDescriptor.create(
new Option(CpdDescriptor.HIGH_OPTION_KEY, "100"),
new Option(CpdDescriptor.NORMAL_OPTION_KEY, "50"));
var lowReport = lowParser.parse(new FileReaderFactory(getResourceAsFile("one-cpd.xml")));
assertThat(lowReport).hasSize(2).hasSeverities(Severity.WARNING_LOW);
}
@Test
void shouldAssignCorrectSeverityForSpotBugs() {
verifyPriority("CONFIDENCE", 1, 11, 0);
verifyPriority("RANK", 0, 0, 12);
}
private void verifyPriority(final String type, final int expectedHighSize, final int expectedNormalSize,
final int expectedLowSize) {
var parserRegistry = new ParserRegistry();
var findbugsDescriptor = parserRegistry.get("findbugs");
var parser = findbugsDescriptor.create(new Option(FindBugsDescriptor.PRIORITY_OPTION_KEY, type));
var confidenceReport = parser.parse(new FileReaderFactory(getResourceAsFile("findbugs-severities.xml")));
assertThat(confidenceReport).hasSize(12);
assertThat(confidenceReport.getSizeOf(Severity.WARNING_HIGH)).isEqualTo(expectedHighSize);
assertThat(confidenceReport.getSizeOf(Severity.WARNING_NORMAL)).isEqualTo(expectedNormalSize);
assertThat(confidenceReport.getSizeOf(Severity.WARNING_LOW)).isEqualTo(expectedLowSize);
}
}