-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
JsonRendererTest.java
99 lines (83 loc) · 3.1 KB
/
JsonRendererTest.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
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.ReportTest;
public class JsonRendererTest extends AbstractRendererTest {
@Override
public Renderer getRenderer() {
return new JsonRenderer();
}
@Override
public String getExpected() {
return readFile("expected.json");
}
@Override
public String getExpectedEmpty() {
return readFile("empty.json");
}
@Override
public String getExpectedMultiple() {
return readFile("expected-multiple.json");
}
@Override
public String getExpectedError(ProcessingError error) {
String expected = readFile("expected-processingerror.json");
expected = expected.replace("###REPLACE_ME###", error.getDetail()
.replaceAll("\r", "\\\\r")
.replaceAll("\n", "\\\\n")
.replaceAll("\t", "\\\\t"));
return expected;
}
@Override
public String getExpectedError(ConfigurationError error) {
return readFile("expected-configurationerror.json");
}
@Override
public String getExpectedErrorWithoutMessage(ProcessingError error) {
String expected = readFile("expected-processingerror-no-message.json");
expected = expected.replace("###REPLACE_ME###", error.getDetail()
.replaceAll("\r", "\\\\r")
.replaceAll("\n", "\\\\n")
.replaceAll("\t", "\\\\t"));
return expected;
}
private String readFile(String name) {
try (InputStream in = JsonRendererTest.class.getResourceAsStream("json/" + name)) {
return IOUtils.toString(in, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String filter(String expected) {
String result = expected
.replaceAll("\"timestamp\":\\s*\"[^\"]+\"", "\"timestamp\": \"--replaced--\"")
.replaceAll("\"pmdVersion\":\\s*\"[^\"]+\"", "\"pmdVersion\": \"unknown\"")
.replaceAll("\r\n", "\n"); // make the test run on Windows, too
return result;
}
@Test
public void suppressedViolations() throws IOException {
Report rep = new Report();
Map<Integer, String> suppressedLines = new HashMap<>();
suppressedLines.put(1, "test");
rep.suppress(suppressedLines);
rep.addRuleViolation(newRuleViolation(1, 1, 1, 1, new FooRule()));
String actual = ReportTest.render(getRenderer(), rep);
String expected = readFile("expected-suppressed.json");
Assert.assertEquals(filter(expected), filter(actual));
}
}