1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  import junit.framework.JUnit4TestAdapter;
13  import net.sourceforge.pmd.AbstractRule;
14  import net.sourceforge.pmd.IRuleViolation;
15  import net.sourceforge.pmd.MockRule;
16  import net.sourceforge.pmd.PMD;
17  import net.sourceforge.pmd.Report;
18  import net.sourceforge.pmd.ReportListener;
19  import net.sourceforge.pmd.Rule;
20  import net.sourceforge.pmd.RuleContext;
21  import net.sourceforge.pmd.RuleViolation;
22  import net.sourceforge.pmd.SourceType;
23  import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
24  import net.sourceforge.pmd.ast.SimpleJavaNode;
25  import net.sourceforge.pmd.ast.SimpleNode;
26  import net.sourceforge.pmd.renderers.Renderer;
27  import net.sourceforge.pmd.renderers.XMLRenderer;
28  import net.sourceforge.pmd.stat.Metric;
29  import net.sourceforge.pmd.symboltable.SourceFileScope;
30  
31  import org.junit.Test;
32  
33  import test.net.sourceforge.pmd.testframework.RuleTst;
34  
35  public class ReportTest extends RuleTst implements ReportListener {
36  
37      private static class FooRule extends AbstractRule {
38          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
39              if ("Foo".equals(c.getImage())) addViolation(ctx, c);
40              return ctx;
41          }
42  
43          public String getMessage() {
44              return "blah";
45          }
46  
47          public String getName() {
48              return "Foo";
49          }
50  
51          public String getRuleSetName() {
52              return "RuleSet";
53          }
54  
55          public String getDescription() {
56              return "desc";
57          }
58      }
59  
60      private boolean violationSemaphore;
61      private boolean metricSemaphore;
62  
63      public void ruleViolationAdded(IRuleViolation ruleViolation) {
64          violationSemaphore = true;
65      }
66  
67      public void metricAdded(Metric metric) {
68          metricSemaphore = true;
69      }
70  
71      @Test
72      public void testBasic() throws Throwable {
73          Report r = new Report();
74          runTestFromString(TEST1, new FooRule(), r);
75          assertTrue(!r.isEmpty());
76      }
77  
78      @Test
79      public void testMetric0() {
80          Report r = new Report();
81          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
82      }
83  
84      @Test
85      public void testMetric1() {
86          Report r = new Report();
87          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
88  
89          r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
90          assertTrue("Expected metrics weren't there", r.hasMetrics());
91  
92          Iterator ms = r.metrics();
93          assertTrue("Should have some metrics in there now", ms.hasNext());
94  
95          Object o = ms.next();
96          assertTrue("Expected Metric, got " + o.getClass(), o instanceof Metric);
97  
98          Metric m = (Metric) o;
99          assertEquals("metric name mismatch", "m1", m.getMetricName());
100         assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
101         assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
102         assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
103         assertEquals("wrong std dev value", 4.0, m.getStandardDeviation(), 0.05);
104     }
105 
106     @Test
107     public void testExclusionsInReportWithAnnotations() throws Throwable {
108         Report rpt = new Report();
109         runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
110         assertTrue(rpt.isEmpty());
111         assertEquals(1, rpt.getSuppressedRuleViolations().size());
112     }
113 
114     @Test
115     public void testExclusionsInReportWithNOPMD() throws Throwable {
116         Report rpt = new Report();
117         runTestFromString(TEST3, new FooRule(), rpt);
118         assertTrue(rpt.isEmpty());
119         assertEquals(1, rpt.getSuppressedRuleViolations().size());
120     }
121 
122     private static final String TEST1 =
123             "public class Foo {}" + PMD.EOL;
124 
125     private static final String TEST2 =
126             "@SuppressWarnings(\"PMD\")" + PMD.EOL +
127             "public class Foo {}";
128 
129     private static final String TEST3 =
130             "public class Foo {} // NOPMD";
131 
132     // Files are grouped together now.
133     @Test
134     public void testSortedReport_File() {
135         Report r = new Report();
136         RuleContext ctx = new RuleContext();
137         ctx.setSourceCodeFilename("foo");
138         SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
139         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
140         ctx.setSourceCodeFilename("bar");
141         SimpleNode s1 = getNode(10, 5, ctx.getSourceCodeFilename());
142         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s1));
143         Renderer rend = new XMLRenderer();
144         String result = rend.render(r);
145         assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo"));
146     }
147 
148     @Test
149     public void testSortedReport_Line() {
150         Report r = new Report();
151         RuleContext ctx = new RuleContext();
152         ctx.setSourceCodeFilename("foo1");
153         SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
154         r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg", "rulesetname"), ctx, s));
155         ctx.setSourceCodeFilename("foo2");
156         SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
157         r.addRuleViolation(new RuleViolation(new MockRule("rule1", "rule1", "msg", "rulesetname"), ctx, s1));
158         Renderer rend = new XMLRenderer();
159         String result = rend.render(r);
160         assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1"));
161     }
162 
163     @Test
164     public void testListener() {
165         Report rpt = new Report();
166         rpt.addListener(this);
167         violationSemaphore = false;
168         RuleContext ctx = new RuleContext();
169         ctx.setSourceCodeFilename("file");
170         SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
171         rpt.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
172         assertTrue(violationSemaphore);
173 
174         metricSemaphore = false;
175         rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
176 
177         assertTrue("no metric", metricSemaphore);
178     }
179 
180     @Test
181     public void testSummary() {
182         Report r = new Report();
183         RuleContext ctx = new RuleContext();
184         ctx.setSourceCodeFilename("foo1");
185         SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
186         Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
187         r.addRuleViolation(new RuleViolation(rule, ctx, s));
188         ctx.setSourceCodeFilename("foo2");
189         Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
190         SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
191         SimpleNode s2 = getNode(30, 5, ctx.getSourceCodeFilename());
192         r.addRuleViolation(new RuleViolation(mr, ctx, s1));
193         r.addRuleViolation(new RuleViolation(mr, ctx, s2));
194         Map summary = r.getSummary();
195         assertEquals(summary.keySet().size(), 2);
196         assertTrue(summary.values().contains(new Integer(1)));
197         assertTrue(summary.values().contains(new Integer(2)));
198     }
199     
200     private SimpleNode getNode(int line, int column, String scopeName){
201         SimpleNode s = new SimpleJavaNode(2);
202         SimpleNode parent = new SimpleJavaNode(1);
203         parent.testingOnly__setBeginLine(line);
204         parent.testingOnly__setBeginColumn(column);
205         s.jjtSetParent(parent);
206         s.setScope(new SourceFileScope(scopeName));
207         s.testingOnly__setBeginLine(10);
208         s.testingOnly__setBeginColumn(5);
209         return s;
210     }
211     
212     public static junit.framework.Test suite() {
213         return new JUnit4TestAdapter(ReportTest.class);
214     }
215 
216 }