1   /***
2    * <copyright>
3    *  Copyright 1997-2002 InfoEther, LLC
4    *  under sponsorship of the Defense Advanced Research Projects Agency
5    (DARPA).
6    *
7    *  This program is free software; you can redistribute it and/or modify
8    *  it under the terms of the Cougaar Open Source License as published
9    by
10   *  DARPA on the Cougaar Open Source Website (www.cougaar.org).
11   *
12   *  THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
13   *  PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
14   *  IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
15   *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
16   *  ANY WARRANTIES AS TO NON-INFRINGEMENT.  IN NO EVENT SHALL COPYRIGHT
17   *  HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
18   *  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
19   *  TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20   *  PERFORMANCE OF THE COUGAAR SOFTWARE.
21   * </copyright>
22   */
23  package test.net.sourceforge.pmd;
24  
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import net.sourceforge.pmd.AbstractRule;
29  import net.sourceforge.pmd.IRuleViolation;
30  import net.sourceforge.pmd.PMD;
31  import net.sourceforge.pmd.Report;
32  import net.sourceforge.pmd.ReportListener;
33  import net.sourceforge.pmd.Rule;
34  import net.sourceforge.pmd.RuleContext;
35  import net.sourceforge.pmd.RuleViolation;
36  import net.sourceforge.pmd.SourceType;
37  import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
38  import net.sourceforge.pmd.ast.SimpleJavaNode;
39  import net.sourceforge.pmd.ast.SimpleNode;
40  import net.sourceforge.pmd.renderers.Renderer;
41  import net.sourceforge.pmd.renderers.XMLRenderer;
42  import net.sourceforge.pmd.stat.Metric;
43  import net.sourceforge.pmd.symboltable.SourceFileScope;
44  import test.net.sourceforge.pmd.testframework.MockRule;
45  import test.net.sourceforge.pmd.testframework.RuleTst;
46  
47  public class ReportTest extends RuleTst implements ReportListener {
48  
49      private static class FooRule extends AbstractRule {
50          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
51              if ("Foo".equals(c.getImage())) addViolation(ctx, c);
52              return ctx;
53          }
54  
55          public String getMessage() {
56              return "blah";
57          }
58  
59          public String getName() {
60              return "Foo";
61          }
62  
63          public String getRuleSetName() {
64              return "RuleSet";
65          }
66  
67          public String getDescription() {
68              return "desc";
69          }
70      }
71  
72      private boolean violationSemaphore;
73      private boolean metricSemaphore;
74  
75      public void ruleViolationAdded(IRuleViolation ruleViolation) {
76          violationSemaphore = true;
77      }
78  
79      public void metricAdded(Metric metric) {
80          metricSemaphore = true;
81      }
82  
83      public void testBasic() throws Throwable {
84          Report r = new Report();
85          runTestFromString(TEST1, new FooRule(), r);
86          assertTrue(!r.isEmpty());
87      }
88  
89  
90      public void testMetric0() {
91          Report r = new Report();
92          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
93      }
94  
95      public void testMetric1() {
96          Report r = new Report();
97          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
98  
99          r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
100         assertTrue("Expected metrics weren't there", r.hasMetrics());
101 
102         Iterator ms = r.metrics();
103         assertTrue("Should have some metrics in there now", ms.hasNext());
104 
105         Object o = ms.next();
106         assertTrue("Expected Metric, got " + o.getClass(), o instanceof Metric);
107 
108         Metric m = (Metric) o;
109         assertEquals("metric name mismatch", "m1", m.getMetricName());
110         assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
111         assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
112         assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
113         assertEquals("wrong std dev value", 4.0, m.getStandardDeviation(), 0.05);
114     }
115 
116     public void testExclusionsInReportWithAnnotations() throws Throwable {
117         Report rpt = new Report();
118         runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
119         assertTrue(rpt.isEmpty());
120         assertEquals(1, rpt.getSuppressedRuleViolations().size());
121     }
122 
123     public void testExclusionsInReportWithNOPMD() throws Throwable {
124         Report rpt = new Report();
125         runTestFromString(TEST3, new FooRule(), rpt);
126         assertTrue(rpt.isEmpty());
127         assertEquals(1, rpt.getSuppressedRuleViolations().size());
128     }
129 
130     private static final String TEST1 =
131             "public class Foo {}" + PMD.EOL;
132 
133     private static final String TEST2 =
134             "@SuppressWarnings(\"PMD\")" + PMD.EOL +
135             "public class Foo {}";
136 
137     private static final String TEST3 =
138             "public class Foo {} // NOPMD";
139 
140     // Files are grouped together now.
141     public void testSortedReport_File() {
142         Report r = new Report();
143         RuleContext ctx = new RuleContext();
144         ctx.setSourceCodeFilename("foo");
145         SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
146         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
147         ctx.setSourceCodeFilename("bar");
148         SimpleNode s1 = getNode(10, 5, ctx.getSourceCodeFilename());
149         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s1));
150         Renderer rend = new XMLRenderer();
151         String result = rend.render(r);
152         assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo"));
153     }
154 
155     public void testSortedReport_Line() {
156         Report r = new Report();
157         RuleContext ctx = new RuleContext();
158         ctx.setSourceCodeFilename("foo1");
159         SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
160         r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg", "rulesetname"), ctx, s));
161         ctx.setSourceCodeFilename("foo2");
162         SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
163         r.addRuleViolation(new RuleViolation(new MockRule("rule1", "rule1", "msg", "rulesetname"), ctx, s1));
164         Renderer rend = new XMLRenderer();
165         String result = rend.render(r);
166         assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1"));
167     }
168 
169     public void testListener() {
170         Report rpt = new Report();
171         rpt.addListener(this);
172         violationSemaphore = false;
173         RuleContext ctx = new RuleContext();
174         ctx.setSourceCodeFilename("file");
175         SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
176         rpt.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
177         assertTrue(violationSemaphore);
178 
179         metricSemaphore = false;
180         rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
181 
182         assertTrue("no metric", metricSemaphore);
183     }
184 
185     public void testSummary() {
186         Report r = new Report();
187         RuleContext ctx = new RuleContext();
188         ctx.setSourceCodeFilename("foo1");
189         SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
190         Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
191         r.addRuleViolation(new RuleViolation(rule, ctx, s));
192         ctx.setSourceCodeFilename("foo2");
193         Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
194         SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
195         SimpleNode s2 = getNode(30, 5, ctx.getSourceCodeFilename());
196         r.addRuleViolation(new RuleViolation(mr, ctx, s1));
197         r.addRuleViolation(new RuleViolation(mr, ctx, s2));
198         Map summary = r.getSummary();
199         assertEquals(summary.keySet().size(), 2);
200         assertTrue(summary.values().contains(new Integer(1)));
201         assertTrue(summary.values().contains(new Integer(2)));
202     }
203     
204     private SimpleNode getNode(int line, int column, String scopeName){
205         SimpleNode s = new SimpleJavaNode(2);
206         SimpleNode parent = new SimpleJavaNode(1);
207         parent.testingOnly__setBeginLine(line);
208         parent.testingOnly__setBeginColumn(column);
209         s.jjtSetParent(parent);
210         s.setScope(new SourceFileScope(scopeName));
211         s.testingOnly__setBeginLine(10);
212         s.testingOnly__setBeginColumn(5);
213         return s;
214     }
215 }