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 private XMLRenderer IUT;
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 setUp() {
28 IUT = new XMLRenderer();
29 }
30
31 public void testEmptyReport() throws Throwable {
32 String rendered = IUT.render(new Report());
33 assertTrue("Expected empty PMD tag.", rendered.indexOf("violation") < 0);
34 }
35
36 public void testErrorReport() throws Throwable {
37 Report r = new Report();
38 r.addError(new Report.ProcessingError("test_msg", "test_filename"));
39 assertTrue(IUT.render(r).indexOf("msg=\"test_msg\">") == -1);
40 }
41
42 public void testSingleReport() throws Throwable {
43 Report report = new Report();
44 ctx.setSourceCodeFilename("testSingleReport");
45 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
46
47 String rendered = IUT.render(report);
48
49 // <?xml version="1.0"?>
50 // <pmd>
51 // <file name="testSingleReport">
52 // <violation line="1" rule="RULE1">
53 // Rule1
54 // </violation>
55 // </file>
56 // </pmd>
57
58 List expectedStrings = new ArrayList();
59 expectedStrings.add("<pmd>");
60 expectedStrings.add("<file name=\"testSingleReport\">");
61 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
62 expectedStrings.add("Rule1");
63 expectedStrings.add("</violation>");
64 expectedStrings.add("</file>");
65 expectedStrings.add("</pmd>");
66
67 verifyPositions(rendered, expectedStrings);
68 }
69
70 public void testDoubleReport() throws Throwable {
71 Report report = new Report();
72 ctx.setSourceCodeFilename("testDoubleReport");
73 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
74
75 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
76
77 // <?xml version="1.0"?>
78 // <pmd>
79 // <file name="testSingleReport">
80 // <violation line="1" rule="RULE1">
81 // Rule1
82 // </violation>
83 // <violation line="2" rule="RULE2">
84 // Rule2
85 // </violation>
86 // </file>
87 // </pmd>
88
89 List expectedStrings = new ArrayList();
90 expectedStrings.add("<pmd>");
91 expectedStrings.add("<file name=\"testDoubleReport\">");
92 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
93 expectedStrings.add("Rule1");
94 expectedStrings.add("</violation>");
95 expectedStrings.add("<violation line=\"2\" rule=\"RULE2\">");
96 expectedStrings.add("Rule2");
97 expectedStrings.add("</violation>");
98 expectedStrings.add("</file>");
99 expectedStrings.add("</pmd>");
100
101 verifyPositions(IUT.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 // <?xml version="1.0"?>
113 // <pmd>
114 // <file name="testTwoFiles_0">
115 // <violation line="1" rule="RULE1">
116 // Rule1
117 // </violation>
118 // </file>
119 // <file name="testTwoFiles_1">
120 // <violation line="1" rule="RULE1">
121 // Rule1
122 // </violation>
123 // </file>
124 // </pmd>
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 verifyPositions(IUT.render(report), expectedStrings);
141 }
142
143 public void testUnorderedFiles() throws Throwable {
144 Report report = new Report();
145 ctx.setSourceCodeFilename("testTwoFiles_0");
146 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
147
148 ctx.setSourceCodeFilename("testTwoFiles_1");
149 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
150
151 ctx.setSourceCodeFilename("testTwoFiles_0");
152 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
153
154 // <?xml version="1.0"?>
155 // <pmd>
156 // <file name="testTwoFiles_0">
157 // <violation line="1" rule="RULE1">
158 // Rule1
159 // </violation>
160 // </file>
161 // <file name="testTwoFiles_1">
162 // <violation line="1" rule="RULE1">
163 // Rule1
164 // </violation>
165 // </file>
166 // </pmd>
167
168 List expectedStrings = new ArrayList();
169 expectedStrings.add("<pmd>");
170 expectedStrings.add("<file name=\"testTwoFiles_0\">");
171 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
172 expectedStrings.add("Rule1");
173 expectedStrings.add("</violation>");
174 expectedStrings.add("<violation line=\"2\" rule=\"RULE2\">");
175 expectedStrings.add("Rule2");
176 expectedStrings.add("</violation>");
177 expectedStrings.add("</file>");
178 expectedStrings.add("<file name=\"testTwoFiles_1\">");
179 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
180 expectedStrings.add("Rule1");
181 expectedStrings.add("</violation>");
182 expectedStrings.add("</file>");
183 expectedStrings.add("</pmd>");
184
185 verifyPositions(IUT.render(report), expectedStrings);
186 }
187
188 /***
189 * Verify correct escaping in generated XML.
190 */
191 public void testEscaping() throws Throwable {
192 Report report = new Report();
193 ctx.setSourceCodeFilename("testEscaping: Less than: < Greater than: > Ampersand: & Quote: \" 'e' acute: \u00E9");
194 report.addRuleViolation(new RuleViolation(RULE1, 1, "[RULE] Less than: < Greater than: > Ampersand: & Quote: \" 'e' acute: \u00E9", ctx));
195
196 // <?xml version="1.0"?>
197 // <pmd>
198 // <file name="testEscaping: Less than: < Greater than: > Ampersand: & Quote: " 'e' acute: é">
199 // <violation line="1" rule="RULE1">
200 // [RULE] Less than: < Greater than: > Ampersand: & Quote: " 'e' acute: é
201 // </violation>
202 // </file>
203 // </pmd>
204
205 List expectedStrings = new ArrayList();
206 expectedStrings.add("<pmd>");
207 expectedStrings.add("<file name=\"testEscaping: Less than: ");
208 expectedStrings.add("<");
209 expectedStrings.add(" Greater than: ");
210 expectedStrings.add(">");
211 expectedStrings.add(" Ampersand: ");
212 expectedStrings.add("&");
213 expectedStrings.add(" Quote: ");
214 expectedStrings.add(""");
215 expectedStrings.add(" 'e' acute: ");
216 expectedStrings.add("é");
217 expectedStrings.add("\">");
218 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
219 expectedStrings.add("[RULE] Less than: ");
220 expectedStrings.add("<");
221 expectedStrings.add(" Greater than: ");
222 expectedStrings.add(">");
223 expectedStrings.add(" Ampersand: ");
224 expectedStrings.add("&");
225 expectedStrings.add(" Quote: ");
226 expectedStrings.add(""");
227 expectedStrings.add(" 'e' acute: ");
228 expectedStrings.add("é");
229 expectedStrings.add("</violation>");
230 expectedStrings.add("</file>");
231 expectedStrings.add("</pmd>");
232
233 verifyPositions(IUT.render(report), expectedStrings);
234 }
235
236 public void verifyPositions(String rendered, List strings) {
237 Iterator i = strings.iterator();
238 int currPos = 0;
239 String lastString = "<?xml version=\"1.0\"?>";
240
241 while (i.hasNext()) {
242 String str = (String) i.next();
243
244 int strPos = rendered.indexOf(str, currPos);
245 assertTrue("Expecting: " + str + " after " + lastString, strPos > currPos);
246 currPos = strPos;
247 lastString = str;
248 }
249 }
250 }
This page was automatically generated by Maven