1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.renderers;
5
6 import junit.framework.TestCase;
7 import net.sourceforge.pmd.Report;
8 import net.sourceforge.pmd.RuleContext;
9 import net.sourceforge.pmd.RuleViolation;
10 import net.sourceforge.pmd.renderers.XMLRenderer;
11 import test.net.sourceforge.pmd.testframework.MockRule;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 public class XMLRendererTest extends TestCase {
18
19 private MockRule RULE1 = new MockRule("RULE1", "RULE1", "msg");
20 private MockRule RULE2 = new MockRule("RULE2", "RULE2", "msg");
21 private RuleContext ctx = new RuleContext();
22
23 public XMLRendererTest(String name) {
24 super(name);
25 }
26
27 public void testEmptyReport() throws Throwable {
28 XMLRenderer renderer = new XMLRenderer();
29 String rendered = renderer.render(new Report());
30 assertTrue("Expected empty PMD tag.", rendered.indexOf("violation") < 0);
31 }
32
33 public void testErrorReport() throws Throwable {
34 Report r = new Report();
35 r.addError(new Report.ProcessingError("test_msg", "test_filename"));
36 XMLRenderer renderer = new XMLRenderer();
37 assertTrue(renderer.render(r).indexOf("msg=\"test_msg\"/>") != -1);
38 }
39
40 public void testSingleReport() throws Throwable {
41 Report report = new Report();
42 ctx.setSourceCodeFilename("testSingleReport");
43 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
44
45 XMLRenderer renderer = new XMLRenderer();
46 String rendered = renderer.render(report);
47
48
49
50
51
52
53
54
55
56
57 List expectedStrings = new ArrayList();
58 expectedStrings.add("<pmd>");
59 expectedStrings.add("<file name=\"testSingleReport\">");
60 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
61 expectedStrings.add("Rule1");
62 expectedStrings.add("</violation>");
63 expectedStrings.add("</file>");
64 expectedStrings.add("</pmd>");
65
66 verifyPositions(rendered, expectedStrings);
67 }
68
69 public void testDoubleReport() throws Throwable {
70 Report report = new Report();
71 ctx.setSourceCodeFilename("testDoubleReport");
72 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
73
74 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
75
76
77
78
79
80
81
82
83
84
85
86
87
88 List expectedStrings = new ArrayList();
89 expectedStrings.add("<pmd>");
90 expectedStrings.add("<file name=\"testDoubleReport\">");
91 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
92 expectedStrings.add("Rule1");
93 expectedStrings.add("</violation>");
94 expectedStrings.add("<violation line=\"2\" rule=\"RULE2\">");
95 expectedStrings.add("Rule2");
96 expectedStrings.add("</violation>");
97 expectedStrings.add("</file>");
98 expectedStrings.add("</pmd>");
99
100 XMLRenderer renderer = new XMLRenderer();
101 verifyPositions(renderer.render(report), expectedStrings);
102 }
103
104 public void testTwoFiles() throws Throwable {
105 Report report = new Report();
106 ctx.setSourceCodeFilename("testTwoFiles_0");
107 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
108
109 ctx.setSourceCodeFilename("testTwoFiles_1");
110 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 List expectedStrings = new ArrayList();
127 expectedStrings.add("<pmd>");
128 expectedStrings.add("<file name=\"testTwoFiles_0\">");
129 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
130 expectedStrings.add("Rule1");
131 expectedStrings.add("</violation>");
132 expectedStrings.add("</file>");
133 expectedStrings.add("<file name=\"testTwoFiles_1\">");
134 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
135 expectedStrings.add("Rule1");
136 expectedStrings.add("</violation>");
137 expectedStrings.add("</file>");
138 expectedStrings.add("</pmd>");
139
140 XMLRenderer renderer = new XMLRenderer();
141 verifyPositions(renderer.render(report), expectedStrings);
142 }
143
144 public void testUnorderedFiles() throws Throwable {
145 Report report = new Report();
146 ctx.setSourceCodeFilename("testTwoFiles_0");
147 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
148
149 ctx.setSourceCodeFilename("testTwoFiles_1");
150 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
151
152 ctx.setSourceCodeFilename("testTwoFiles_0");
153 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 List expectedStrings = new ArrayList();
170 expectedStrings.add("<pmd>");
171 expectedStrings.add("<file name=\"testTwoFiles_0\">");
172 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
173 expectedStrings.add("Rule1");
174 expectedStrings.add("</violation>");
175 expectedStrings.add("<violation line=\"2\" rule=\"RULE2\">");
176 expectedStrings.add("Rule2");
177 expectedStrings.add("</violation>");
178 expectedStrings.add("</file>");
179 expectedStrings.add("<file name=\"testTwoFiles_1\">");
180 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
181 expectedStrings.add("Rule1");
182 expectedStrings.add("</violation>");
183 expectedStrings.add("</file>");
184 expectedStrings.add("</pmd>");
185
186 XMLRenderer renderer = new XMLRenderer();
187 verifyPositions(renderer.render(report), expectedStrings);
188 }
189
190 /***
191 * Verify correct escaping in generated XML.
192 */
193 public void testEscaping() throws Throwable {
194 Report report = new Report();
195 ctx.setSourceCodeFilename("testEscaping: Less than: < Greater than: > Ampersand: & Quote: \" 'e' acute: \u00E9");
196 report.addRuleViolation(new RuleViolation(RULE1, 1, "[RULE] Less than: < Greater than: > Ampersand: & Quote: \" 'e' acute: \u00E9", ctx));
197
198
199
200
201
202
203
204
205
206
207 List expectedStrings = new ArrayList();
208 expectedStrings.add("<pmd>");
209 expectedStrings.add("<file name=\"testEscaping: Less than: ");
210 expectedStrings.add("<");
211 expectedStrings.add(" Greater than: ");
212 expectedStrings.add(">");
213 expectedStrings.add(" Ampersand: ");
214 expectedStrings.add("&");
215 expectedStrings.add(" Quote: ");
216 expectedStrings.add(""");
217 expectedStrings.add(" 'e' acute: ");
218 expectedStrings.add("é");
219 expectedStrings.add("\">");
220 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
221 expectedStrings.add("[RULE] Less than: ");
222 expectedStrings.add("<");
223 expectedStrings.add(" Greater than: ");
224 expectedStrings.add(">");
225 expectedStrings.add(" Ampersand: ");
226 expectedStrings.add("&");
227 expectedStrings.add(" Quote: ");
228 expectedStrings.add(""");
229 expectedStrings.add(" 'e' acute: ");
230 expectedStrings.add("é");
231 expectedStrings.add("</violation>");
232 expectedStrings.add("</file>");
233 expectedStrings.add("</pmd>");
234
235 XMLRenderer renderer = new XMLRenderer();
236 verifyPositions(renderer.render(report), expectedStrings);
237 }
238
239 public void verifyPositions(String rendered, List strings) {
240 Iterator i = strings.iterator();
241 int currPos = 0;
242 String lastString = "<?xml version=\"1.0\"?>";
243
244 while (i.hasNext()) {
245 String str = (String) i.next();
246
247 int strPos = rendered.indexOf(str, currPos);
248 assertTrue("Expecting: " + str + " after " + lastString, strPos > currPos);
249 currPos = strPos;
250 lastString = str;
251 }
252 }
253 }