diff --git a/pmd/regress/test/net/sourceforge/pmd/CommandLineOptionsTest.java b/pmd/regress/test/net/sourceforge/pmd/CommandLineOptionsTest.java new file mode 100644 index 0000000000..11866c3ee5 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/CommandLineOptionsTest.java @@ -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 + } + } +} diff --git a/pmd/src/net/sourceforge/pmd/CommandLineOptions.java b/pmd/src/net/sourceforge/pmd/CommandLineOptions.java new file mode 100644 index 0000000000..b9b7e39cbe --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/CommandLineOptions.java @@ -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 java -jar pmd-1.2.1.jar c:\\my\\source\\code html rulesets/unusedcode.xml,rulesets/imports.xml" + PMD.EOL; + } +} + + diff --git a/pmd/src/net/sourceforge/pmd/PMD.java b/pmd/src/net/sourceforge/pmd/PMD.java index cadcf69351..0149a3fa7c 100644 --- a/pmd/src/net/sourceforge/pmd/PMD.java +++ b/pmd/src/net/sourceforge/pmd/PMD.java @@ -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 java -jar pmd-1.2.1.jar c:\\my\\source\\code html rulesets/unusedcode.xml," + - "rulesets/imports.xml" + EOL); - } }