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