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 junit.framework.TestCase;
26  import net.sourceforge.pmd.Report;
27  import net.sourceforge.pmd.ReportListener;
28  import net.sourceforge.pmd.Rule;
29  import net.sourceforge.pmd.RuleContext;
30  import net.sourceforge.pmd.RuleViolation;
31  import net.sourceforge.pmd.renderers.Renderer;
32  import net.sourceforge.pmd.renderers.XMLRenderer;
33  import net.sourceforge.pmd.stat.Metric;
34  import test.net.sourceforge.pmd.testframework.MockRule;
35  
36  import java.util.Iterator;
37  import java.util.Map;
38  
39  public class ReportTest extends TestCase implements ReportListener {
40  
41      private boolean violationSemaphore;
42      private boolean metricSemaphore;
43  
44      public void testBasic() {
45          Report r = new Report();
46          RuleContext ctx = new RuleContext();
47          ctx.setSourceCodeFilename("foo");
48          r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg"), 5, ctx));
49          assertTrue(!r.isEmpty());
50      }
51  
52      public void testMetric0() {
53          Report r = new Report();
54          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
55      }
56  
57      public void testMetric1() {
58          Report r = new Report();
59          assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
60  
61          r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
62          assertTrue("Expected metrics weren't there", r.hasMetrics());
63  
64          Iterator ms = r.metrics();
65          assertTrue("Should have some metrics in there now", ms.hasNext());
66  
67          Object o = ms.next();
68          assertTrue("Expected Metric, got " + o.getClass(), o instanceof Metric);
69  
70          Metric m = (Metric) o;
71          assertEquals("metric name mismatch", "m1", m.getMetricName());
72          assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
73          assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
74          assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
75          assertEquals("wrong std dev value", 4.0, m.getStandardDeviation(), 0.05);
76      }
77  
78  
79      // Files are grouped together now.
80      public void testSortedReport_File() {
81          Report r = new Report();
82          RuleContext ctx = new RuleContext();
83          ctx.setSourceCodeFilename("foo");
84          r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg"), 10, ctx));
85          ctx.setSourceCodeFilename("bar");
86          r.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg"), 20, ctx));
87          Renderer rend = new XMLRenderer();
88          String result = rend.render(r);
89          assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo"));
90      }
91  
92      public void testSortedReport_Line() {
93          Report r = new Report();
94          RuleContext ctx = new RuleContext();
95          ctx.setSourceCodeFilename("foo1");
96          r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg"), 10, ctx));
97          ctx.setSourceCodeFilename("foo2");
98          r.addRuleViolation(new RuleViolation(new MockRule("rule1", "rule1", "msg"), 20, ctx));
99          Renderer rend = new XMLRenderer();
100         String result = rend.render(r);
101         assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1"));
102     }
103 
104     public void testListener() {
105         Report rpt = new Report();
106         rpt.addListener(this);
107         violationSemaphore = false;
108         RuleContext ctx = new RuleContext();
109         ctx.setSourceCodeFilename("file");
110         rpt.addRuleViolation(new RuleViolation(new MockRule("name", "desc", "msg"), 5, ctx));
111         assertTrue(violationSemaphore);
112 
113         metricSemaphore = false;
114         rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
115 
116         assertTrue("no metric", metricSemaphore);
117     }
118 
119     public void ruleViolationAdded(RuleViolation ruleViolation) {
120         violationSemaphore = true;
121     }
122 
123     public void metricAdded(Metric metric) {
124         metricSemaphore = true;
125     }
126 
127     public void testSummary() {
128         Report r = new Report();
129         RuleContext ctx = new RuleContext();
130         ctx.setSourceCodeFilename("foo1");
131         r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2", "msg"), 10, ctx));
132         ctx.setSourceCodeFilename("foo2");
133         Rule mr = new MockRule("rule1", "rule1", "msg");
134         r.addRuleViolation(new RuleViolation(mr, 20, ctx));
135         r.addRuleViolation(new RuleViolation(mr, 30, ctx));
136         Map summary = r.getSummary();
137         assertEquals(summary.keySet().size(), 2);
138         assertTrue(summary.values().contains(new Integer(1)));
139         assertTrue(summary.values().contains(new Integer(2)));
140     }
141 }