001: /**
002: * <copyright>
003: * Copyright 1997-2002 InfoEther, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency
005: (DARPA).
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the Cougaar Open Source License as published
009: by
010: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
013: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
014: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
015: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
016: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
017: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
018: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
019: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
020: * PERFORMANCE OF THE COUGAAR SOFTWARE.
021: * </copyright>
022: */package test.net.sourceforge.pmd;
023:
024: import static org.junit.Assert.assertEquals;
025: import static org.junit.Assert.assertTrue;
026:
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import junit.framework.JUnit4TestAdapter;
031: import net.sourceforge.pmd.AbstractRule;
032: import net.sourceforge.pmd.IRuleViolation;
033: import net.sourceforge.pmd.MockRule;
034: import net.sourceforge.pmd.PMD;
035: import net.sourceforge.pmd.Report;
036: import net.sourceforge.pmd.ReportListener;
037: import net.sourceforge.pmd.Rule;
038: import net.sourceforge.pmd.RuleContext;
039: import net.sourceforge.pmd.RuleViolation;
040: import net.sourceforge.pmd.SourceType;
041: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
042: import net.sourceforge.pmd.ast.SimpleJavaNode;
043: import net.sourceforge.pmd.ast.SimpleNode;
044: import net.sourceforge.pmd.renderers.Renderer;
045: import net.sourceforge.pmd.renderers.XMLRenderer;
046: import net.sourceforge.pmd.stat.Metric;
047: import net.sourceforge.pmd.symboltable.SourceFileScope;
048:
049: import org.junit.Test;
050:
051: import test.net.sourceforge.pmd.testframework.RuleTst;
052:
053: public class ReportTest extends RuleTst implements ReportListener {
054:
055: private static class FooRule extends AbstractRule {
056: public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
057: if ("Foo".equals(c.getImage()))
058: addViolation(ctx, c);
059: return ctx;
060: }
061:
062: public String getMessage() {
063: return "blah";
064: }
065:
066: public String getName() {
067: return "Foo";
068: }
069:
070: public String getRuleSetName() {
071: return "RuleSet";
072: }
073:
074: public String getDescription() {
075: return "desc";
076: }
077: }
078:
079: private boolean violationSemaphore;
080: private boolean metricSemaphore;
081:
082: public void ruleViolationAdded(IRuleViolation ruleViolation) {
083: violationSemaphore = true;
084: }
085:
086: public void metricAdded(Metric metric) {
087: metricSemaphore = true;
088: }
089:
090: @Test
091: public void testBasic() throws Throwable {
092: Report r = new Report();
093: runTestFromString(TEST1, new FooRule(), r);
094: assertTrue(!r.isEmpty());
095: }
096:
097: @Test
098: public void testMetric0() {
099: Report r = new Report();
100: assertTrue("Default report shouldn't contain metrics", !r
101: .hasMetrics());
102: }
103:
104: @Test
105: public void testMetric1() {
106: Report r = new Report();
107: assertTrue("Default report shouldn't contain metrics", !r
108: .hasMetrics());
109:
110: r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
111: assertTrue("Expected metrics weren't there", r.hasMetrics());
112:
113: Iterator ms = r.metrics();
114: assertTrue("Should have some metrics in there now", ms
115: .hasNext());
116:
117: Object o = ms.next();
118: assertTrue("Expected Metric, got " + o.getClass(),
119: o instanceof Metric);
120:
121: Metric m = (Metric) o;
122: assertEquals("metric name mismatch", "m1", m.getMetricName());
123: assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
124: assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
125: assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
126: assertEquals("wrong std dev value", 4.0, m
127: .getStandardDeviation(), 0.05);
128: }
129:
130: @Test
131: public void testExclusionsInReportWithAnnotations()
132: throws Throwable {
133: Report rpt = new Report();
134: runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
135: assertTrue(rpt.isEmpty());
136: assertEquals(1, rpt.getSuppressedRuleViolations().size());
137: }
138:
139: @Test
140: public void testExclusionsInReportWithNOPMD() throws Throwable {
141: Report rpt = new Report();
142: runTestFromString(TEST3, new FooRule(), rpt);
143: assertTrue(rpt.isEmpty());
144: assertEquals(1, rpt.getSuppressedRuleViolations().size());
145: }
146:
147: private static final String TEST1 = "public class Foo {}" + PMD.EOL;
148:
149: private static final String TEST2 = "@SuppressWarnings(\"PMD\")"
150: + PMD.EOL + "public class Foo {}";
151:
152: private static final String TEST3 = "public class Foo {} // NOPMD";
153:
154: // Files are grouped together now.
155: @Test
156: public void testSortedReport_File() {
157: Report r = new Report();
158: RuleContext ctx = new RuleContext();
159: ctx.setSourceCodeFilename("foo");
160: SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
161: r.addRuleViolation(new RuleViolation(new MockRule("name",
162: "desc", "msg", "rulesetname"), ctx, s));
163: ctx.setSourceCodeFilename("bar");
164: SimpleNode s1 = getNode(10, 5, ctx.getSourceCodeFilename());
165: r.addRuleViolation(new RuleViolation(new MockRule("name",
166: "desc", "msg", "rulesetname"), ctx, s1));
167: Renderer rend = new XMLRenderer();
168: String result = rend.render(r);
169: assertTrue("sort order wrong", result.indexOf("bar") < result
170: .indexOf("foo"));
171: }
172:
173: @Test
174: public void testSortedReport_Line() {
175: Report r = new Report();
176: RuleContext ctx = new RuleContext();
177: ctx.setSourceCodeFilename("foo1");
178: SimpleNode s = getNode(10, 5, ctx.getSourceCodeFilename());
179: r.addRuleViolation(new RuleViolation(new MockRule("rule2",
180: "rule2", "msg", "rulesetname"), ctx, s));
181: ctx.setSourceCodeFilename("foo2");
182: SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
183: r.addRuleViolation(new RuleViolation(new MockRule("rule1",
184: "rule1", "msg", "rulesetname"), ctx, s1));
185: Renderer rend = new XMLRenderer();
186: String result = rend.render(r);
187: assertTrue("sort order wrong", result.indexOf("rule2") < result
188: .indexOf("rule1"));
189: }
190:
191: @Test
192: public void testListener() {
193: Report rpt = new Report();
194: rpt.addListener(this );
195: violationSemaphore = false;
196: RuleContext ctx = new RuleContext();
197: ctx.setSourceCodeFilename("file");
198: SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
199: rpt.addRuleViolation(new RuleViolation(new MockRule("name",
200: "desc", "msg", "rulesetname"), ctx, s));
201: assertTrue(violationSemaphore);
202:
203: metricSemaphore = false;
204: rpt.addMetric(new Metric("test", 0, 0.0, 0.0, 0.0, 0.0, 0.0));
205:
206: assertTrue("no metric", metricSemaphore);
207: }
208:
209: @Test
210: public void testSummary() {
211: Report r = new Report();
212: RuleContext ctx = new RuleContext();
213: ctx.setSourceCodeFilename("foo1");
214: SimpleNode s = getNode(5, 5, ctx.getSourceCodeFilename());
215: Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
216: r.addRuleViolation(new RuleViolation(rule, ctx, s));
217: ctx.setSourceCodeFilename("foo2");
218: Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
219: SimpleNode s1 = getNode(20, 5, ctx.getSourceCodeFilename());
220: SimpleNode s2 = getNode(30, 5, ctx.getSourceCodeFilename());
221: r.addRuleViolation(new RuleViolation(mr, ctx, s1));
222: r.addRuleViolation(new RuleViolation(mr, ctx, s2));
223: Map summary = r.getSummary();
224: assertEquals(summary.keySet().size(), 2);
225: assertTrue(summary.values().contains(new Integer(1)));
226: assertTrue(summary.values().contains(new Integer(2)));
227: }
228:
229: private SimpleNode getNode(int line, int column, String scopeName) {
230: SimpleNode s = new SimpleJavaNode(2);
231: SimpleNode parent = new SimpleJavaNode(1);
232: parent.testingOnly__setBeginLine(line);
233: parent.testingOnly__setBeginColumn(column);
234: s.jjtSetParent(parent);
235: s.setScope(new SourceFileScope(scopeName));
236: s.testingOnly__setBeginLine(10);
237: s.testingOnly__setBeginColumn(5);
238: return s;
239: }
240:
241: public static junit.framework.Test suite() {
242: return new JUnit4TestAdapter(ReportTest.class);
243: }
244:
245: }
|