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 net.sourceforge.pmd.AbstractRule;
26  import net.sourceforge.pmd.PMD;
27  import net.sourceforge.pmd.Report;
28  import net.sourceforge.pmd.ReportListener;
29  import net.sourceforge.pmd.RuleViolation;
30  import net.sourceforge.pmd.IRuleViolation;
31  import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
32  import net.sourceforge.pmd.stat.Metric;
33  import test.net.sourceforge.pmd.testframework.RuleTst;
34  
35  import java.util.Iterator;
36  
37  public class ReportTest extends RuleTst implements ReportListener {
38  
39      private static class FooRule extends AbstractRule {
40          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
41              if (c.getImage().equals("Foo")) addViolation(ctx, c);
42              return ctx;
43          }
44  
45          public String getMessage() {
46              return "blah";
47          }
48  
49          public String getName() {
50              return "Foo";
51          }
52  
53          public String getRuleSetName() {
54              return "RuleSet";
55          }
56  
57          public String getDescription() {
58              return "desc";
59          }
60      }
61  
62      private boolean violationSemaphore;
63      private boolean metricSemaphore;
64  
65      public void ruleViolationAdded(IRuleViolation ruleViolation) {
66          violationSemaphore = true;
67      }
68  
69      public void metricAdded(Metric metric) {
70          metricSemaphore = true;
71      }
72  
73      public void testBasic() throws Throwable {
74          Report r = new Report();
75          runTestFromString(TEST1, new FooRule(), r);
76          assertTrue(!r.isEmpty());
77      }
78  
79  
80      public void testMetric0() {
81          Report r = new Report();
82          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
83      }
84  
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     public void testExclusionsInReportWithAnnotations() throws Throwable {
107         Report rpt = new Report();
108         runTestFromString15(TEST2, new FooRule(), rpt);
109         assertTrue(rpt.isEmpty());
110         assertEquals(1, rpt.getSuppressedRuleViolations().size());
111     }
112 
113     public void testExclusionsInReportWithNOPMD() throws Throwable {
114         Report rpt = new Report();
115         runTestFromString(TEST3, new FooRule(), rpt);
116         assertTrue(rpt.isEmpty());
117         assertEquals(1, rpt.getSuppressedRuleViolations().size());
118     }
119 
120     private static final String TEST1 =
121             "public class Foo {}" + PMD.EOL;
122 
123     private static final String TEST2 =
124             "@SuppressWarnings(\"\")" + PMD.EOL +
125             "public class Foo {}";
126 
127     private static final String TEST3 =
128             "public class Foo {} // NOPMD";
129 /*
130 
131     // Files are grouped together now.
132     public void testSortedReport_File() {
133         Report r = new Report();
134         RuleContext ctx = new RuleContext();
135         ctx.setSourceCodeFilename("foo");
136         SimpleNode s = new SimpleJavaNode(1);
137         s.testingOnly__setBeginLine(10);
138         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
139         ctx.setSourceCodeFilename("bar");
140         SimpleNode s1 = new SimpleJavaNode(1);
141         s1.testingOnly__setBeginLine(20);
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     public void testSortedReport_Line() {
149         Report r = new Report();
150         RuleContext ctx = new RuleContext();
151         ctx.setSourceCodeFilename("foo1");
152         SimpleNode s = new SimpleJavaNode(1);
153         s.testingOnly__setBeginLine(10);
154         r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg", "rulesetname"), ctx, s));
155         ctx.setSourceCodeFilename("foo2");
156         SimpleNode s1 = new SimpleJavaNode(1);
157         s1.testingOnly__setBeginLine(20);
158         r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg", "rulesetname"), ctx, s1));
159         Renderer rend = new XMLRenderer();
160         String result = rend.render(r);
161         assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1"));
162     }
163 
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 = new SimpleJavaNode(1);
171         s.testingOnly__setBeginLine(5);
172         rpt.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
173         assertTrue(violationSemaphore);
174 
175         metricSemaphore = false;
176         rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
177 
178         assertTrue("no metric", metricSemaphore);
179     }
180 
181     public void testSummary() {
182         Report r = new Report();
183         RuleContext ctx = new RuleContext();
184         ctx.setSourceCodeFilename("foo1");
185         SimpleNode s = new SimpleJavaNode(1);
186         s.testingOnly__setBeginLine(10);
187         r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg", "rulesetname"), ctx, s));
188         ctx.setSourceCodeFilename("foo2");
189         Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
190         SimpleNode s1 = new SimpleJavaNode(1);
191         s1.testingOnly__setBeginLine(20);
192         SimpleNode s2 = new SimpleJavaNode(1);
193         s2.testingOnly__setBeginLine(30);
194         r.addRuleViolation(new RuleViolation(mr, ctx, s1));
195         r.addRuleViolation(new RuleViolation(mr, ctx, s2));
196         Map summary = r.getSummary();
197         assertEquals(summary.keySet().size(), 2);
198         assertTrue(summary.values().contains(new Integer(1)));
199         assertTrue(summary.values().contains(new Integer(2)));
200     }
201 */
202 }