diff --git a/pmd/regress/test/net/sourceforge/pmd/ReportTest.java b/pmd/regress/test/net/sourceforge/pmd/ReportTest.java index a23e0cd0df..a7e6c51178 100644 --- a/pmd/regress/test/net/sourceforge/pmd/ReportTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/ReportTest.java @@ -28,13 +28,25 @@ public class ReportTest extends TestCase { assertTrue(!r.isEmpty()); } - public void testSortedReport() { + // Files are grouped together now. + public void testSortedReport_File() { Report r = new Report(); r.addRuleViolation(new RuleViolation(new MockRule(), 10, "foo")); r.addRuleViolation(new RuleViolation(new MockRule(), 20, "bar")); Renderer rend = new XMLRenderer(); String result = rend.render(r); - assertTrue(result.indexOf("foo") < result.indexOf("bar")); + assertTrue(result.indexOf("bar") < result.indexOf("foo")); + } + + public void testSortedReport_Line() { + Report r = new Report(); + r.addRuleViolation(new RuleViolation(new MockRule("rule2", "rule2"), + 10, "foo")); + r.addRuleViolation(new RuleViolation(new MockRule("rule1", "rule1"), + 20, "foo")); + Renderer rend = new XMLRenderer(); + String result = rend.render(r); + assertTrue(result.indexOf("rule2") < result.indexOf("rule1")); } /* diff --git a/pmd/regress/test/net/sourceforge/pmd/RuleViolationTest.java b/pmd/regress/test/net/sourceforge/pmd/RuleViolationTest.java index ddf134baa7..d52b7eafa6 100644 --- a/pmd/regress/test/net/sourceforge/pmd/RuleViolationTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/RuleViolationTest.java @@ -28,13 +28,18 @@ public class RuleViolationTest extends TestCase { assertEquals("description", r.getDescription()); } + // + // Changed logic of Comparator so that rules in the same file + // get grouped together in the output report. + // DDP 7/11/2002 + // 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)); + assertEquals(-1, comp.compare(r1, r2)); + assertEquals(1, comp.compare(r2, r1)); } public void testComparatorWithSameFileDifferentLines() { diff --git a/pmd/regress/test/net/sourceforge/pmd/renderers/XMLRendererTest.java b/pmd/regress/test/net/sourceforge/pmd/renderers/XMLRendererTest.java new file mode 100644 index 0000000000..5212e3d5fe --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/renderers/XMLRendererTest.java @@ -0,0 +1,224 @@ +package test.net.sourceforge.pmd.renderers; + +import junit.framework.TestCase; + +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +import test.net.sourceforge.pmd.MockRule; + +import net.sourceforge.pmd.renderers.*; +import net.sourceforge.pmd.*; + +public class XMLRendererTest + extends TestCase +{ + private XMLRenderer IUT = null; + private MockRule RULE1 = new MockRule( "RULE1", "RULE1" ); + private MockRule RULE2 = new MockRule( "RULE2", "RULE2" ); + + public XMLRendererTest( String name ) { + super( name ); + } + + public void setUp() { + IUT = new XMLRenderer(); + } + + public void testEmptyReport() + throws Throwable + { + String rendered = IUT.render( new Report() ); + assertTrue("Expected empty PMD tag.", + rendered.indexOf("violation") < 0); + } + + public void testSingleReport() + throws Throwable + { + Report report = new Report(); + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testSingleReport") ); + + String rendered = IUT.render( report ); + + // + // + // + // + // Rule1 + // + // + // + + List expectedStrings = new ArrayList(); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add(""); + + verifyPositions( rendered, expectedStrings ); + } + + public void testDoubleReport() + throws Throwable + { + Report report = new Report(); + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testDoubleReport") ); + + report.addRuleViolation( new RuleViolation( RULE2, + 2, "Rule2", + "testDoubleReport") ); + + String rendered = IUT.render( report ); + + // + // + // + // + // Rule1 + // + // + // Rule2 + // + // + // + + List expectedStrings = new ArrayList(); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add("Rule2"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add(""); + + verifyPositions( rendered, expectedStrings ); + } + + public void testTwoFiles() + throws Throwable + { + Report report = new Report(); + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testTwoFiles_0") ); + + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testTwoFiles_1") ); + + String rendered = IUT.render( report ); + + // + // + // + // + // Rule1 + // + // + // + // + // Rule1 + // + // + // + + List expectedStrings = new ArrayList(); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add(""); + + verifyPositions( rendered, expectedStrings ); + } + + public void testUnorderedFiles() + throws Throwable + { + Report report = new Report(); + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testTwoFiles_0") ); + + report.addRuleViolation( new RuleViolation( RULE1, + 1, "Rule1", + "testTwoFiles_1") ); + + report.addRuleViolation( new RuleViolation( RULE2, + 2, "Rule2", + "testTwoFiles_0") ); + + String rendered = IUT.render( report ); + + // + // + // + // + // Rule1 + // + // + // + // + // Rule1 + // + // + // + + System.err.println( rendered ); + + List expectedStrings = new ArrayList(); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add("Rule2"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add("" ); + expectedStrings.add(""); + expectedStrings.add("Rule1"); + expectedStrings.add(""); + expectedStrings.add(""); + expectedStrings.add(""); + + verifyPositions( rendered, expectedStrings ); + } + + public void verifyPositions( String rendered, + List strings ) { + Iterator i = strings.iterator(); + int currPos = 0; + String lastString = ""; + + while (i.hasNext()) { + String str = (String) i.next(); + + int strPos = rendered.indexOf( str, currPos ); + assertTrue("Expecting: " + str + " after " + lastString, + strPos > currPos ); + currPos = strPos; + lastString = str; + } + } +}