From c81767249b6cfacacedcc3916c5d17904647e494 Mon Sep 17 00:00:00 2001 From: Allan Caplan Date: Tue, 30 Jan 2007 00:34:24 +0000 Subject: [PATCH] Checking in some Java 5 changes. Adding a test for PapariTextRenderer - modified the class slightly to be able to mock the Reader in the JUnit test git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5008 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/renderers/AbstractRendererTst.java | 8 ++-- .../pmd/renderers/PapariTextRendererTest.java | 37 +++++++++++++++++++ .../pmd/renderers/PapariTextRenderer.java | 21 +++++++---- 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/renderers/PapariTextRendererTest.java diff --git a/pmd/regress/test/net/sourceforge/pmd/renderers/AbstractRendererTst.java b/pmd/regress/test/net/sourceforge/pmd/renderers/AbstractRendererTst.java index b876e85fe4..d52ff59989 100644 --- a/pmd/regress/test/net/sourceforge/pmd/renderers/AbstractRendererTst.java +++ b/pmd/regress/test/net/sourceforge/pmd/renderers/AbstractRendererTst.java @@ -61,20 +61,20 @@ public abstract class AbstractRendererTst extends RuleTst { Report rep = new Report(); runTestFromString(TEST1, new FooRule(), rep); String actual = getRenderer().render(rep); - assertEquals(actual, getExpected()); + assertEquals(getExpected(), actual); } public void testRendererEmpty() throws Throwable { Report rep = new Report(); String actual = getRenderer().render(rep); - assertEquals(actual, getExpectedEmpty()); + assertEquals(getExpectedEmpty(), actual); } public void testRendererMultiple() throws Throwable { Report rep = new Report(); runTestFromString(TEST1, new FooRule2(), rep); String actual = getRenderer().render(rep); - assertEquals(actual, getExpectedMultiple()); + assertEquals(getExpectedMultiple(), actual); } public void testError() throws Throwable { @@ -82,7 +82,7 @@ public abstract class AbstractRendererTst extends RuleTst { Report.ProcessingError err = new Report.ProcessingError("Error", "file"); rep.addError(err); String actual = getRenderer().render(rep); - assertEquals(actual, getExpectedError(err)); + assertEquals(getExpectedError(err), actual); } private static final String TEST1 = "public class Foo {}" + PMD.EOL; diff --git a/pmd/regress/test/net/sourceforge/pmd/renderers/PapariTextRendererTest.java b/pmd/regress/test/net/sourceforge/pmd/renderers/PapariTextRendererTest.java new file mode 100644 index 0000000000..6ffb3b281a --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/renderers/PapariTextRendererTest.java @@ -0,0 +1,37 @@ +package test.net.sourceforge.pmd.renderers; + +import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.Report.ProcessingError; +import net.sourceforge.pmd.renderers.AbstractRenderer; +import net.sourceforge.pmd.renderers.PapariTextRenderer; + +import java.io.FileNotFoundException; +import java.io.Reader; +import java.io.StringReader; + +public class PapariTextRendererTest extends AbstractRendererTst { + + public AbstractRenderer getRenderer() { + return new PapariTextRenderer(){ + protected Reader getReader(String sourceFile) throws FileNotFoundException { + return new StringReader("public class Foo {}"); + } + }; + } + + public String getExpected() { + return "* file: n/a" + PMD.EOL + " src: n/a:1:1" + PMD.EOL + " rule: Foo" + PMD.EOL + " msg: msg" + PMD.EOL + " code: public class Foo {}" + PMD.EOL + PMD.EOL + PMD.EOL + PMD.EOL + "Summary:" + PMD.EOL + PMD.EOL + " : 1" + PMD.EOL + "* warnings: 1" + PMD.EOL; + } + + public String getExpectedEmpty() { + return PMD.EOL + PMD.EOL + "Summary:" + PMD.EOL + PMD.EOL + "* warnings: 0" + PMD.EOL; + } + + public String getExpectedMultiple() { + return "* file: n/a" + PMD.EOL + " src: n/a:1:1" + PMD.EOL + " rule: Foo" + PMD.EOL + " msg: msg" + PMD.EOL + " code: public class Foo {}" + PMD.EOL + PMD.EOL + " src: n/a:1:1" + PMD.EOL + " rule: Foo" + PMD.EOL + " msg: msg" + PMD.EOL + " code: public class Foo {}" + PMD.EOL + PMD.EOL + PMD.EOL + PMD.EOL + "Summary:" + PMD.EOL + PMD.EOL + " : 2" + PMD.EOL + "* warnings: 2" + PMD.EOL; + } + + public String getExpectedError(ProcessingError error) { + return PMD.EOL + PMD.EOL + "Summary:" + PMD.EOL + PMD.EOL + " err: Error" + PMD.EOL + PMD.EOL + "* errors: 0" + PMD.EOL + "* warnings: 0" + PMD.EOL; + } +} diff --git a/pmd/src/net/sourceforge/pmd/renderers/PapariTextRenderer.java b/pmd/src/net/sourceforge/pmd/renderers/PapariTextRenderer.java index 95b775caa0..77f08d1834 100644 --- a/pmd/src/net/sourceforge/pmd/renderers/PapariTextRenderer.java +++ b/pmd/src/net/sourceforge/pmd/renderers/PapariTextRenderer.java @@ -9,8 +9,10 @@ import net.sourceforge.pmd.Report; import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.io.Reader; import java.io.Writer; import java.util.Iterator; import java.util.Map; @@ -102,19 +104,18 @@ public class PapariTextRenderer extends AbstractRenderer { } writer.write(PMD.EOL + PMD.EOL); writer.write("Summary:" + PMD.EOL + PMD.EOL); - Map summary = report.getCountSummary(); - for (Iterator i = summary.entrySet().iterator(); i.hasNext();) { + Map summary = report.getCountSummary(); + for (Map.Entry entry : summary.entrySet()) { buf.setLength(0); - Map.Entry entry = (Map.Entry) i.next(); - String key = (String) entry.getKey(); - buf.append(key + " : " + ((Integer) entry.getValue()).intValue() + PMD.EOL); + String key = entry.getKey(); + buf.append(key).append(" : ").append(entry.getValue()).append(PMD.EOL); writer.write(buf.toString()); } - for (Iterator i = report.errors(); i.hasNext();) { + for (Iterator i = report.errors(); i.hasNext();) { buf.setLength(0); numberOfErrors++; - Report.ProcessingError error = (Report.ProcessingError) i.next(); + Report.ProcessingError error = i.next(); if (error.getFile().equals(lastFile)) { lastFile = error.getFile(); buf.append(this.redBold + "*" + this.colorReset + " file: " + this.whiteBold + this.getRelativePath(lastFile) + this.colorReset + PMD.EOL); @@ -141,7 +142,7 @@ public class PapariTextRenderer extends AbstractRenderer { String code = null; BufferedReader br = null; try { - br = new BufferedReader(new FileReader(new File(sourceFile))); + br = new BufferedReader(getReader(sourceFile)); for (int i = 0; line > i; i++) { code = br.readLine().trim(); } @@ -159,6 +160,10 @@ public class PapariTextRenderer extends AbstractRenderer { return code; } + protected Reader getReader(String sourceFile) throws FileNotFoundException { + return new FileReader(new File(sourceFile)); + } + /** * Attempts to determine the relative path to the file. If relative path cannot be found, * the original path is returnedi, ie - the current path for the supplied file.