forked from phoedos/pmd
Refactored command line option processing into a separate class; added some tests
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2166 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -0,0 +1,56 @@
|
||||
package test.net.sourceforge.pmd;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import net.sourceforge.pmd.CommandLineOptions;
|
||||
|
||||
public class CommandLineOptionsTest extends TestCase {
|
||||
|
||||
public void testDebug() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset", "-debug"});
|
||||
assertTrue(opt.debugEnabled());
|
||||
}
|
||||
|
||||
public void testShortNames() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset", "-shortnames"});
|
||||
assertTrue(opt.shortNamesEnabled());
|
||||
}
|
||||
|
||||
public void testInputFileName() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset"});
|
||||
assertEquals("file", opt.getInputFileName());
|
||||
}
|
||||
|
||||
public void testReportFormat() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset"});
|
||||
assertEquals("format", opt.getReportFormat());
|
||||
}
|
||||
|
||||
public void testRulesets() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset"});
|
||||
assertEquals("ruleset", opt.getRulesets());
|
||||
}
|
||||
|
||||
public void testCommaSeparatedFiles() {
|
||||
CommandLineOptions opt = new CommandLineOptions(new String[] {"file1,file2,file3", "format", "ruleset"});
|
||||
assertTrue(opt.containsCommaSeparatedFileList());
|
||||
}
|
||||
|
||||
public void testNotEnoughArgs() {
|
||||
try {
|
||||
new CommandLineOptions(new String[] {"file1", "format"});
|
||||
fail("Should have thrown an exception when only array contained < 3 args");
|
||||
} catch (RuntimeException re) {
|
||||
// cool
|
||||
}
|
||||
}
|
||||
|
||||
public void testNullArgs() {
|
||||
try {
|
||||
new CommandLineOptions(null);
|
||||
fail("Should have thrown an exception when null passed to constructor");
|
||||
} catch (RuntimeException re) {
|
||||
// cool
|
||||
}
|
||||
}
|
||||
}
|
65
pmd/src/net/sourceforge/pmd/CommandLineOptions.java
Normal file
65
pmd/src/net/sourceforge/pmd/CommandLineOptions.java
Normal file
@ -0,0 +1,65 @@
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
public class CommandLineOptions {
|
||||
|
||||
private boolean debugEnabled;
|
||||
private boolean shortNamesEnabled;
|
||||
|
||||
private String inputFileName;
|
||||
private String reportFormat;
|
||||
private String ruleSets;
|
||||
|
||||
public CommandLineOptions(String[] args) {
|
||||
|
||||
if (args == null || args.length < 3) {
|
||||
throw new RuntimeException(usage());
|
||||
}
|
||||
|
||||
inputFileName = args[0];
|
||||
reportFormat = args[1];
|
||||
ruleSets = args[2];
|
||||
|
||||
for (int i=0; i<args.length; i++) {
|
||||
if (args[i].equals("-debug")) {
|
||||
debugEnabled = true;
|
||||
}
|
||||
if (args[i].equals("-shortnames")) {
|
||||
shortNamesEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsCommaSeparatedFileList() {
|
||||
return inputFileName.indexOf(',') != -1;
|
||||
}
|
||||
|
||||
public String getInputFileName() {
|
||||
return this.inputFileName;
|
||||
}
|
||||
|
||||
public String getReportFormat() {
|
||||
return this.reportFormat;
|
||||
}
|
||||
|
||||
public String getRulesets() {
|
||||
return this.ruleSets;
|
||||
}
|
||||
|
||||
public boolean debugEnabled() {
|
||||
return debugEnabled;
|
||||
}
|
||||
|
||||
public boolean shortNamesEnabled() {
|
||||
return shortNamesEnabled;
|
||||
}
|
||||
|
||||
private String usage() {
|
||||
return PMD.EOL +
|
||||
"Please pass in a java source code filename or directory, a report format, " + PMD.EOL +
|
||||
"and a ruleset filename or a comma-delimited string of ruleset filenames." + PMD.EOL +
|
||||
"For example: " + PMD.EOL +
|
||||
"c:\\> java -jar pmd-1.2.1.jar c:\\my\\source\\code html rulesets/unusedcode.xml,rulesets/imports.xml" + PMD.EOL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class PMD {
|
||||
|
||||
public static final String EOL = System.getProperty("line.separator", "\n");
|
||||
|
||||
/**
|
||||
@ -61,31 +62,13 @@ public class PMD {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 3) {
|
||||
usage();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
String inputFileName = args[0];
|
||||
String reportFormat = args[1];
|
||||
String ruleSets = args[2];
|
||||
|
||||
boolean shortNames = false;
|
||||
boolean debug = false;
|
||||
for (int i=0; i<args.length; i++) {
|
||||
if (args[i].equals("-shortnames")) {
|
||||
shortNames = true;
|
||||
}
|
||||
if (args[i].equals("-debug")) {
|
||||
debug = true;
|
||||
}
|
||||
}
|
||||
CommandLineOptions opts = new CommandLineOptions(args);
|
||||
|
||||
List files;
|
||||
if (inputFileName.indexOf(',') != -1) {
|
||||
files = collectFromCommaDelimitedString(inputFileName);
|
||||
if (opts.containsCommaSeparatedFileList()) {
|
||||
files = collectFromCommaDelimitedString(opts.getInputFileName());
|
||||
} else {
|
||||
files = collectFilesFromOneName(inputFileName);
|
||||
files = collectFilesFromOneName(opts.getInputFileName());
|
||||
}
|
||||
|
||||
PMD pmd = new PMD();
|
||||
@ -95,17 +78,17 @@ public class PMD {
|
||||
|
||||
try {
|
||||
RuleSetFactory ruleSetFactory = new RuleSetFactory();
|
||||
RuleSet rules = ruleSetFactory.createRuleSet(ruleSets);
|
||||
RuleSet rules = ruleSetFactory.createRuleSet(opts.getRulesets());
|
||||
for (Iterator i = files.iterator(); i.hasNext();) {
|
||||
File file = (File) i.next();
|
||||
ctx.setSourceCodeFilename(glomName(shortNames, inputFileName, file));
|
||||
ctx.setSourceCodeFilename(glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file));
|
||||
try {
|
||||
pmd.processFile(new FileInputStream(file), rules, ctx);
|
||||
} catch (PMDException pmde) {
|
||||
if (debug) {
|
||||
if (opts.debugEnabled()) {
|
||||
pmde.getReason().printStackTrace();
|
||||
}
|
||||
ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), glomName(shortNames, inputFileName, file)));
|
||||
ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file)));
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
@ -115,29 +98,25 @@ public class PMD {
|
||||
}
|
||||
|
||||
Renderer renderer = null;
|
||||
if (reportFormat.equals("xml")) {
|
||||
if (opts.getReportFormat().equals("xml")) {
|
||||
renderer = new XMLRenderer();
|
||||
} else if (reportFormat.equals("ideaj")) {
|
||||
} else if (opts.getReportFormat().equals("ideaj")) {
|
||||
renderer = new IDEAJRenderer(args);
|
||||
} else if (reportFormat.equals("text")) {
|
||||
} else if (opts.getReportFormat().equals("text")) {
|
||||
renderer = new TextRenderer();
|
||||
} else if (reportFormat.equals("emacs")) {
|
||||
} else if (opts.getReportFormat().equals("emacs")) {
|
||||
renderer = new EmacsRenderer();
|
||||
} else if (reportFormat.equals("csv")) {
|
||||
} else if (opts.getReportFormat().equals("csv")) {
|
||||
renderer = new CSVRenderer();
|
||||
} else if (reportFormat.equals("html")) {
|
||||
} else if (opts.getReportFormat().equals("html")) {
|
||||
renderer = new HTMLRenderer();
|
||||
} else if (!reportFormat.equals("")) {
|
||||
} else if (!opts.getReportFormat().equals("")) {
|
||||
try {
|
||||
renderer = (Renderer)Class.forName(reportFormat).newInstance();
|
||||
renderer = (Renderer)Class.forName(opts.getReportFormat()).newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
System.out.println("Please supply a renderer name");
|
||||
usage();
|
||||
return;
|
||||
}
|
||||
System.out.println(renderer.render(ctx.getReport()));
|
||||
}
|
||||
@ -182,13 +161,4 @@ public class PMD {
|
||||
return files;
|
||||
}
|
||||
|
||||
private static void usage() {
|
||||
final String EOL = System.getProperty("line.separator");
|
||||
System.err.println(EOL +
|
||||
"Please pass in a java source code filename or directory, a report format, " + EOL +
|
||||
"and a ruleset filename or a comma-delimited string of ruleset filenames." + EOL +
|
||||
"For example: " + EOL +
|
||||
"c:\\> java -jar pmd-1.2.1.jar c:\\my\\source\\code html rulesets/unusedcode.xml," +
|
||||
"rulesets/imports.xml" + EOL);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user