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          // <?xml version="1.0"?>
49          // <pmd>
50          //   <file name="testSingleReport">
51          //     <violation line="1" rule="RULE1">
52          // Rule1
53          //     </violation>
54          //   </file>
55          // </pmd>
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          // <?xml version="1.0"?>
77          // <pmd>
78          //   <file name="testSingleReport">
79          //     <violation line="1" rule="RULE1">
80          // Rule1
81          //     </violation>
82          //     <violation line="2" rule="RULE2">
83          // Rule2
84          //     </violation>
85          //   </file>
86          // </pmd>
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         // <?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         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         // <?xml version="1.0"?>
156         // <pmd>
157         //   <file name="testTwoFiles_0">
158         //     <violation line="1" rule="RULE1">
159         // Rule1
160         //     </violation>
161         //   </file>
162         //   <file name="testTwoFiles_1">
163         //     <violation line="1" rule="RULE1">
164         // Rule1
165         //     </violation>
166         //   </file>
167         // </pmd>
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         // <?xml version="1.0"?>
199         // <pmd>
200         //   <file name="testEscaping: Less than: &lt; Greater than: &gt; Ampersand: &amp; Quote: &quot; 'e' acute: &#233;">
201         //     <violation line="1" rule="RULE1">
202         // [RULE] Less than: &lt; Greater than: &gt; Ampersand: &amp; Quote: &quot; 'e' acute: &#233;
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("&lt;");
211         expectedStrings.add(" Greater than: ");
212         expectedStrings.add("&gt;");
213         expectedStrings.add(" Ampersand: ");
214         expectedStrings.add("&amp;");
215         expectedStrings.add(" Quote: ");
216         expectedStrings.add("&quot;");
217         expectedStrings.add(" 'e' acute: ");
218         expectedStrings.add("&#233;");
219         expectedStrings.add("\">");
220         expectedStrings.add("<violation line=\"1\" rule=\"RULE1\">");
221         expectedStrings.add("[RULE] Less than: ");
222         expectedStrings.add("&lt;");
223         expectedStrings.add(" Greater than: ");
224         expectedStrings.add("&gt;");
225         expectedStrings.add(" Ampersand: ");
226         expectedStrings.add("&amp;");
227         expectedStrings.add(" Quote: ");
228         expectedStrings.add("&quot;");
229         expectedStrings.add(" 'e' acute: ");
230         expectedStrings.add("&#233;");
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 }