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
This commit is contained in:
Allan Caplan
2007-01-30 00:34:24 +00:00
parent bfc8b24bd2
commit c81767249b
3 changed files with 54 additions and 12 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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<String, Integer> summary = report.getCountSummary();
for (Map.Entry<String, Integer> 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<Report.ProcessingError> 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.