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 = null;
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 testSingleReport() throws Throwable {
37 Report report = new Report();
38 ctx.setSourceCodeFilename("testSingleReport");
39 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
40
41 String rendered = IUT.render(report);
42
43 // <?xml version="1.0"?>
44 // <pmd>
45 // <file name="testSingleReport">
46 // <violation line="1" rule="RULE1">
47 // Rule1
48 // </violation>
49 // </file>
50 // </pmd>
51
52 List expectedStrings = new ArrayList();
53 expectedStrings.add("<pmd>");
54 expectedStrings.add("<file name=\"testSingleReport\">");
55 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
56 expectedStrings.add("Rule1");
57 expectedStrings.add("</violation>");
58 expectedStrings.add("</file>");
59 expectedStrings.add("</pmd>");
60
61 verifyPositions(rendered, expectedStrings);
62 }
63
64 public void testDoubleReport() throws Throwable {
65 Report report = new Report();
66 ctx.setSourceCodeFilename("testDoubleReport");
67 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
68
69 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
70
71 String rendered = IUT.render(report);
72
73 // <?xml version="1.0"?>
74 // <pmd>
75 // <file name="testSingleReport">
76 // <violation line="1" rule="RULE1">
77 // Rule1
78 // </violation>
79 // <violation line="2" rule="RULE2">
80 // Rule2
81 // </violation>
82 // </file>
83 // </pmd>
84
85 List expectedStrings = new ArrayList();
86 expectedStrings.add("<pmd>");
87 expectedStrings.add("<file name=\"testDoubleReport\">");
88 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
89 expectedStrings.add("Rule1");
90 expectedStrings.add("</violation>");
91 expectedStrings.add("<violation line=\"2\" rule=\"RULE2\">");
92 expectedStrings.add("Rule2");
93 expectedStrings.add("</violation>");
94 expectedStrings.add("</file>");
95 expectedStrings.add("</pmd>");
96
97 verifyPositions(rendered, expectedStrings);
98 }
99
100 public void testTwoFiles() throws Throwable {
101 Report report = new Report();
102 ctx.setSourceCodeFilename("testTwoFiles_0");
103 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
104
105 ctx.setSourceCodeFilename("testTwoFiles_1");
106 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
107
108 String rendered = IUT.render(report);
109
110 // <?xml version="1.0"?>
111 // <pmd>
112 // <file name="testTwoFiles_0">
113 // <violation line="1" rule="RULE1">
114 // Rule1
115 // </violation>
116 // </file>
117 // <file name="testTwoFiles_1">
118 // <violation line="1" rule="RULE1">
119 // Rule1
120 // </violation>
121 // </file>
122 // </pmd>
123
124 List expectedStrings = new ArrayList();
125 expectedStrings.add("<pmd>");
126 expectedStrings.add("<file name=\"testTwoFiles_0\">");
127 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
128 expectedStrings.add("Rule1");
129 expectedStrings.add("</violation>");
130 expectedStrings.add("</file>");
131 expectedStrings.add("<file name=\"testTwoFiles_1\">");
132 expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
133 expectedStrings.add("Rule1");
134 expectedStrings.add("</violation>");
135 expectedStrings.add("</file>");
136 expectedStrings.add("</pmd>");
137
138 verifyPositions(rendered, expectedStrings);
139 }
140
141 public void testUnorderedFiles() throws Throwable {
142 Report report = new Report();
143 ctx.setSourceCodeFilename("testTwoFiles_0");
144 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
145
146 ctx.setSourceCodeFilename("testTwoFiles_1");
147 report.addRuleViolation(new RuleViolation(RULE1, 1, "Rule1", ctx));
148
149 ctx.setSourceCodeFilename("testTwoFiles_0");
150 report.addRuleViolation(new RuleViolation(RULE2, 2, "Rule2", ctx));
151
152 String rendered = IUT.render(report);
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(rendered, 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 String rendered = IUT.render(report);
197
198 // <?xml version="1.0"?>
199 // <pmd>
200 // <file name="testEscaping: Less than: < Greater than: > Ampersand: & Quote: " 'e' acute: é">
201 // <violation line="1" rule="RULE1">
202 // [RULE] Less than: < Greater than: > Ampersand: & Quote: " 'e' acute: é
203 // </violation>
204 // </file>
205 // </pmd>
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 verifyPositions(rendered, expectedStrings);
236 }
237
238 public void verifyPositions(String rendered, List strings) {
239 Iterator i = strings.iterator();
240 int currPos = 0;
241 String lastString = "<?xml version=\"1.0\"?>";
242
243 while (i.hasNext()) {
244 String str = (String) i.next();
245
246 int strPos = rendered.indexOf(str, currPos);
247 assertTrue("Expecting: " + str + " after " + lastString, strPos > currPos);
248 currPos = strPos;
249 lastString = str;
250 }
251 }
252 }
This page was automatically generated by Maven