View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.renderers;
5   
6   import static org.junit.Assert.assertEquals;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import net.sourceforge.pmd.PMD;
12  import net.sourceforge.pmd.Report;
13  import net.sourceforge.pmd.Report.ProcessingError;
14  import net.sourceforge.pmd.ReportTest;
15  import net.sourceforge.pmd.RuleContext;
16  import net.sourceforge.pmd.RuleViolation;
17  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
18  import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
19  
20  import org.junit.Test;
21  
22  public class SummaryHTMLRendererTest extends AbstractRendererTst {
23  
24      @Override
25      public Renderer getRenderer() {
26  	Renderer result = new SummaryHTMLRenderer();
27  	result.setProperty(HTMLRenderer.LINK_PREFIX, "link_prefix");
28  	result.setProperty(HTMLRenderer.LINE_PREFIX, "line_prefix");
29  	return result;
30      }
31  
32      @Override
33      public String getExpected() {
34          return "<html><head><title>PMD</title></head><body>" + PMD.EOL +
35                  "<center><h2>Summary</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr><th>Rule name</th><th>Number of violations</th></tr><tr><td>Foo</td><td align=center>1</td></tr></table><center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
36                  "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
37                  "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL +
38                  "<tr bgcolor=\"lightgrey\"> " + PMD.EOL +
39                  "<td align=\"center\">1</td>" + PMD.EOL +
40                  "<td width=\"*%\"><a href=\"link_prefixn/a.html#line_prefix1\">n/a</a></td>" + PMD.EOL +
41                  "<td align=\"center\" width=\"5%\">1</td>" + PMD.EOL +
42                  "<td width=\"*\">msg</td>" + PMD.EOL +
43                  "</tr>" + PMD.EOL +
44                  "</table></tr></table></body></html>" + PMD.EOL;
45  
46      }
47  
48      @Override
49      public String getExpectedEmpty() {
50          return "<html><head><title>PMD</title></head><body>" + PMD.EOL +
51          "<center><h2>Summary</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr><th>Rule name</th><th>Number of violations</th></tr></table><center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
52          "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
53          "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL +
54          "</table></tr></table></body></html>" + PMD.EOL;
55      }
56  
57      @Override
58      public String getExpectedMultiple() {
59          return "<html><head><title>PMD</title></head><body>" + PMD.EOL +
60          "<center><h2>Summary</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr><th>Rule name</th><th>Number of violations</th></tr><tr><td>Foo</td><td align=center>2</td></tr></table><center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
61          "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
62          "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL +
63          "<tr bgcolor=\"lightgrey\"> " + PMD.EOL +
64          "<td align=\"center\">1</td>" + PMD.EOL +
65          "<td width=\"*%\"><a href=\"link_prefixn/a.html#line_prefix1\">n/a</a></td>" + PMD.EOL +
66          "<td align=\"center\" width=\"5%\">1</td>" + PMD.EOL +
67          "<td width=\"*\">msg</td>" + PMD.EOL +
68          "</tr>" + PMD.EOL +
69          "<tr> " + PMD.EOL +
70          "<td align=\"center\">2</td>" + PMD.EOL +
71          "<td width=\"*%\"><a href=\"link_prefixn/a.html#line_prefix1\">n/a</a></td>" + PMD.EOL +
72          "<td align=\"center\" width=\"5%\">1</td>" + PMD.EOL +
73          "<td width=\"*\">msg</td>" + PMD.EOL +
74          "</tr>" + PMD.EOL +
75          "</table></tr></table></body></html>" + PMD.EOL;
76      }
77  
78      @Override
79      public String getExpectedError(ProcessingError error) {
80          return "<html><head><title>PMD</title></head><body>" + PMD.EOL +
81          "<center><h2>Summary</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr><th>Rule name</th><th>Number of violations</th></tr></table><center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
82          "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
83          "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL +
84          "</table><hr/><center><h3>Processing errors</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
85          "<th>File</th><th>Problem</th></tr>" + PMD.EOL +
86          "<tr bgcolor=\"lightgrey\"> " + PMD.EOL +
87          "<td>file</td>" + PMD.EOL +
88          "<td>Error</td>" + PMD.EOL +
89          "</tr>" + PMD.EOL +
90          "</table></tr></table></body></html>" + PMD.EOL;
91      }
92  
93      @Test
94      public void testShowSuppressions() throws Exception {
95          Report rep = createEmptyReportWithSuppression();
96          Renderer renderer = getRenderer();
97          renderer.setShowSuppressedViolations(true);
98          String actual = ReportTest.render(renderer, rep);
99          assertEquals("<html><head><title>PMD</title></head><body>" + PMD.EOL +
100                 "<center><h2>Summary</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr><th>Rule name</th><th>Number of violations</th></tr></table><center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
101                 "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
102                 "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL +
103                 "</table><hr/><center><h3>Suppressed warnings</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL +
104                 "<th>File</th><th>Line</th><th>Rule</th><th>NOPMD or Annotation</th><th>Reason</th></tr>" + PMD.EOL +
105                 "<tr bgcolor=\"lightgrey\"> " + PMD.EOL +
106                 "<td align=\"left\"></td>" + PMD.EOL +
107                 "<td align=\"center\">1</td>" + PMD.EOL +
108                 "<td align=\"center\">net.sourceforge.pmd.renderers.SummaryHTMLRendererTest$1</td>" + PMD.EOL +
109                 "<td align=\"center\">NOPMD</td>" + PMD.EOL +
110                 "<td align=\"center\">test</td>" + PMD.EOL +
111                 "</tr>" + PMD.EOL +
112                 "</table></tr></table></body></html>" + PMD.EOL
113             , actual);
114     }
115 
116     @Test
117     public void testHideSuppressions() throws Exception {
118         Report rep = createEmptyReportWithSuppression();
119         Renderer renderer = getRenderer();
120         renderer.setShowSuppressedViolations(false);
121         String actual = ReportTest.render(renderer, rep);
122         assertEquals(getExpectedEmpty(), actual);
123     }
124 
125     private Report createEmptyReportWithSuppression() {
126         Report rep = new Report();
127         Map<Integer, String> suppressions = new HashMap<Integer, String>();
128         suppressions.put(1, "test");
129         rep.suppress(suppressions);
130         RuleContext ctx = new RuleContext();
131         RuleViolation violation = new JavaRuleViolation(new AbstractJavaRule() {}, ctx, null, "suppress test", 1, 1);
132         rep.addRuleViolation(violation);
133         return rep;
134     }
135 
136     public static junit.framework.Test suite() {
137         return new junit.framework.JUnit4TestAdapter(SummaryHTMLRendererTest.class);
138     }
139 }
140