Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
added proper testing of invalid command lines
Browse files Browse the repository at this point in the history
  • Loading branch information
tenorviol committed Jan 1, 2012
1 parent e307c58 commit f8e8eac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Binary file modified lib/gitsearch-0.1.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions src/CommandLine.java
Expand Up @@ -15,15 +15,15 @@ public CommandLine(String[] args) throws IllegalArgumentException {


if ("index".equals(command)) { if ("index".equals(command)) {
parseIndexArgs(); parseIndexArgs();
} else if ("search".equals(command)) { } else if ("query".equals(command)) {
parseSearchArgs(); parseQueryArgs();
} else { } else {
throw new IllegalArgumentException("Unknown command, '" + command + "'"); throw new IllegalArgumentException("Unknown command, '" + command + "'");
} }
} }


private void parseCommand(List<String> args) throws IllegalArgumentException { private void parseCommand(List<String> args) throws IllegalArgumentException {
if (!"--index".equals(args.get(0)) || args.size() < 2) { if (args.size() < 2 || !"--index".equals(args.get(0))) {
throw new IllegalArgumentException("--index <path> required"); throw new IllegalArgumentException("--index <path> required");
} }
indexPath = args.get(1); indexPath = args.get(1);
Expand All @@ -41,7 +41,7 @@ private void parseIndexArgs() throws IllegalArgumentException {
} }
} }


private void parseSearchArgs() throws IllegalArgumentException { private void parseQueryArgs() throws IllegalArgumentException {
if (commandArgs.size() == 0) { if (commandArgs.size() == 0) {
throw new IllegalArgumentException("No search query given"); throw new IllegalArgumentException("No search query given");
} }
Expand Down
27 changes: 18 additions & 9 deletions test/CommandLineTest.java
Expand Up @@ -5,14 +5,23 @@


public class CommandLineTest { public class CommandLineTest {


@Test @DataProvider(name = "invalid-command-lines")
public void testFoo() { public Object[][] illegalCommandLineProvider() {
String[] args = {"--index", "./foo/index", "index"}; return new Object[][] {
try { { null },
CommandLine cl = new CommandLine(args); { "--index" },
} catch (Exception e) { { "--index ./.index" },
System.out.println(e); { "--index ./.index index" },
} { "--index ./.index query" },
Assert.assertEquals(1,1); { "--index ./.index non-command" },
{ "query --index ./.index foo" },
};
}

@Test(dataProvider = "invalid-command-lines",
expectedExceptions = IllegalArgumentException.class)
public void testInvalidCommandLineThrowsIllegalArgumentException(String commandLine) {
String[] args = (null == commandLine) ? new String[] {} : commandLine.split(" ");
CommandLine cl = new CommandLine(args);
} }
} }

0 comments on commit f8e8eac

Please sign in to comment.