Added a Comparator for RuleViolation so reports look nicer
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@218 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -28,21 +28,15 @@ public class MockRule implements Rule {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setMessage(String description) {
|
||||
this.desc = description;
|
||||
}
|
||||
public void setName(String name) {this.name = name;}
|
||||
public String getName() {return name;}
|
||||
public String getMessage() {return desc;}
|
||||
public void setMessage(String description) {this.desc = description;}
|
||||
|
||||
public void addViolation( RuleViolation violation ) {
|
||||
violations.add( violation );
|
||||
}
|
||||
|
||||
public String getName() {return name;}
|
||||
public String getMessage() {return desc;}
|
||||
|
||||
public void apply(List astCompilationUnits, RuleContext ctx) {
|
||||
Report report = ctx.getReport();
|
||||
|
||||
|
@ -28,6 +28,15 @@ public class ReportTest extends TestCase {
|
||||
assertTrue(!r.isEmpty());
|
||||
}
|
||||
|
||||
public void testSortedReport() {
|
||||
ReportFactory rf = new ReportFactory();
|
||||
Report r = rf.createReport("xml");
|
||||
r.addRuleViolation(new RuleViolation(new MockRule(), 10, "foo"));
|
||||
r.addRuleViolation(new RuleViolation(new MockRule(), 20, "bar"));
|
||||
String result = r.render();
|
||||
assertTrue(result.indexOf("foo") < result.indexOf("bar"));
|
||||
}
|
||||
|
||||
/*
|
||||
public void testRenderXML() {
|
||||
Report r = new Report("xml");
|
||||
|
@ -27,4 +27,31 @@ public class RuleViolationTest extends TestCase {
|
||||
assertEquals("filename", r.getFilename());
|
||||
assertEquals("description", r.getDescription());
|
||||
}
|
||||
|
||||
public void testComparatorWithDifferentFilenames() {
|
||||
Rule rule = new MockRule("name", "desc");
|
||||
RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
|
||||
RuleViolation r1 = new RuleViolation(rule, 10, "description", "filename1");
|
||||
RuleViolation r2 = new RuleViolation(rule, 20, "description", "filename2");
|
||||
assertEquals(0, comp.compare(r1, r2));
|
||||
assertEquals(0, comp.compare(r2, r1));
|
||||
}
|
||||
|
||||
public void testComparatorWithSameFileDifferentLines() {
|
||||
Rule rule = new MockRule("name", "desc");
|
||||
RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
|
||||
RuleViolation r1 = new RuleViolation(rule, 10, "description", "filename");
|
||||
RuleViolation r2 = new RuleViolation(rule, 20, "description", "filename");
|
||||
assertTrue(comp.compare(r1, r2) < 0);
|
||||
assertTrue(comp.compare(r2, r1) > 0);
|
||||
}
|
||||
|
||||
public void testComparatorWithSameFileSameLines() {
|
||||
Rule rule = new MockRule("name", "desc");
|
||||
RuleViolation.RuleViolationComparator comp = new RuleViolation.RuleViolationComparator();
|
||||
RuleViolation r1 = new RuleViolation(rule, 10, "description", "filename");
|
||||
RuleViolation r2 = new RuleViolation(rule, 10, "description", "filename");
|
||||
assertEquals(0, comp.compare(r1, r2));
|
||||
assertEquals(0, comp.compare(r2, r1));
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,22 @@
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
public class RuleViolation {
|
||||
|
||||
public static class RuleViolationComparator implements Comparator {
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
RuleViolation r1 = (RuleViolation)o1;
|
||||
RuleViolation r2 = (RuleViolation)o2;
|
||||
if (!r1.getFilename().equals(r2.getFilename())) {
|
||||
return 0;
|
||||
}
|
||||
return r1.getLine() - r2.getLine();
|
||||
}
|
||||
}
|
||||
|
||||
private int line;
|
||||
private Rule rule;
|
||||
private String description;
|
||||
|
@ -10,10 +10,11 @@ import net.sourceforge.pmd.RuleViolation;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collections;
|
||||
|
||||
public abstract class AbstractReport implements Report {
|
||||
|
||||
protected List violations = new ArrayList();
|
||||
private List violations = new ArrayList();
|
||||
|
||||
public void addRuleViolation(RuleViolation violation) {
|
||||
violations.add(violation);
|
||||
@ -26,11 +27,16 @@ public abstract class AbstractReport implements Report {
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
sort();
|
||||
return violations.iterator();
|
||||
}
|
||||
|
||||
|
||||
public int size() {
|
||||
return violations.size();
|
||||
}
|
||||
|
||||
private void sort() {
|
||||
Collections.sort(violations, new RuleViolation.RuleViolationComparator());
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class HTMLReport extends AbstractReport {
|
||||
|
||||
public String render() {
|
||||
StringBuffer buf = new StringBuffer("<html><head><title>PMD</title></head><body>" + System.getProperty("line.separator")+ "<table><tr>" + System.getProperty("line.separator")+ "<th>File</th><th>Line</th><th>Problem</th></tr>" + System.getProperty("line.separator"));
|
||||
for (Iterator i = violations.iterator(); i.hasNext();) {
|
||||
for (Iterator i = super.iterator(); i.hasNext();) {
|
||||
RuleViolation rv = (RuleViolation) i.next();
|
||||
buf.append("<tr>" + System.getProperty("line.separator")+ "<td>" + rv.getFilename() + "</td>" + System.getProperty("line.separator"));
|
||||
buf.append("<td>" + Integer.toString(rv.getLine()) + "</td>" + System.getProperty("line.separator"));
|
||||
|
@ -13,7 +13,7 @@ public class XMLReport extends AbstractReport {
|
||||
|
||||
public String render() {
|
||||
StringBuffer buf = new StringBuffer("<?xml version=\"1.0\"?><pmd>" + System.getProperty("line.separator"));
|
||||
for (Iterator i = violations.iterator(); i.hasNext();) {
|
||||
for (Iterator i = super.iterator(); i.hasNext();) {
|
||||
RuleViolation rv = (RuleViolation) i.next();
|
||||
buf.append("<ruleviolation>" + System.getProperty("line.separator"));
|
||||
buf.append("<file>" + rv.getFilename() + "</file>" + System.getProperty("line.separator"));
|
||||
|
Reference in New Issue
Block a user