pmd: fix #1121 NullPointerException when invoking XPathCLI

This commit is contained in:
Andreas Dangel committed 2013-10-11 19:31:03 +02:00
1 parent 64e35caa50
commit 74e8e41d15
3 files changed
+50 -2

No files matched your search

+1
View File
@@ -4,6 +4,7 @@ Fixed bug 881: private final without setter is flagged
Fixed bug 1059: Change rule name "Use Singleton" should be "Use Utility class"
Fixed bug 1106: PMD 5.0.4 fails with NPE on parsing java enum with inner class instance creation
Fixed bug 1115: commentRequiredRule in pmd 5.1 is not working properly
Fixed bug 1121: NullPointerException when invoking XPathCLI
Fixed bug 1130: CloseResource doesn't recognize custom close method
Fixed bug 1134: UseStringBufferLength: false positives
Fixed bug 1136: ECMAScript: NullPointerException in getLeft() and getRight()
@@ -1,3 +1,6 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli;
import java.io.File;
@@ -29,20 +32,29 @@ import net.sourceforge.pmd.util.StringUtil;
*/
public class XPathCLI {
private static final Language LANGUAGE = Language.JAVA;
public static void main(String[] args) throws Exception {
if (args.length != 4) {
System.err.println("Wrong arguments.\n");
System.err.println("Example:");
System.err.println("java " + XPathCLI.class.getName() + " -xpath \"//FieldDeclaration\" -filename \"/home/user/Test.java\"");
System.exit(1);
}
String xpath = args[0].equals("-xpath") ? args[1] : args[3];
String filename = args[0].equals("-file") ? args[1] : args[3];
Rule rule = new XPathRule(xpath);
rule.setMessage("Got one!");
rule.setLanguage(LANGUAGE);
RuleSet ruleSet = RuleSet.createFor("", rule);
RuleContext ctx = PMD.newRuleContext(filename, new File(filename));
ctx.setLanguageVersion(Language.JAVA.getDefaultVersion());
ctx.setLanguageVersion(LANGUAGE.getDefaultVersion());
PMDConfiguration config = new PMDConfiguration();
config.setDefaultLanguageVersion(Language.JAVA.getDefaultVersion());
config.setDefaultLanguageVersion(LANGUAGE.getDefaultVersion());
new SourceCodeProcessor(config).processSourceCode(new FileReader(filename), new RuleSets(ruleSet), ctx);
@@ -0,0 +1,35 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.Assert;
import org.junit.Test;
public class XPathCLITest {
@Test
public void runXPath() throws Exception {
PrintStream oldOut = System.out;
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
try {
XPathCLI.main(new String[] {
"-xpath",
"//ClassOrInterfaceDeclaration",
"-filename",
"src/test/java/net/sourceforge/pmd/cli/XPathCLITest.java"
});
System.out.flush();
} finally {
System.setOut(oldOut);
}
Assert.assertTrue(output.toString("UTF-8").startsWith("Match at line "));
}
}