Skip to content

Commit

Permalink
#571 add tests to improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 19, 2018
1 parent b25a106 commit 558404c
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 7 deletions.
98 changes: 94 additions & 4 deletions src/test/java/picocli/CommandLineCommandMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import picocli.CommandLine.DefaultExceptionHandler;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.RunLast;
import picocli.CommandLine.Spec;
import picocli.CommandLineTest.CompactFields;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -879,4 +877,96 @@ public String methodCommand(@Parameters(arity="0..*") List<String> methodValues)
// fails, still "[arg0, arg1]"
assertEquals("null", methodSecondExecutionResultWithoutParameters.get(0));
}

@Command(addMethodSubcommands = false)
static class StaticMethodCommand {
@Spec static CommandSpec spec;

public StaticMethodCommand(int constructorParam) {}

@Command
public static int staticCommand(@Option(names = "-x") int x) {
return x * 3;
}

@Command
public void cannotBeCalled(@Option(names = "-v") boolean v) {
}

@Command
public static void throwsExecutionException() {
throw new ExecutionException(new CommandLine(new StaticMethodCommand(8)), "abc");
}

@Command
public static void throwsOtherException() {
throw new IndexOutOfBoundsException();
}
}

@Test
public void testStaticCommandMethod() {
assertEquals(9, CommandLine.invoke("staticCommand", StaticMethodCommand.class, "-x", "3"));
}

@Test
public void testCommandMethodsRequireNonArgConstructor() {
try {
CommandLine.invoke("cannotBeCalled", StaticMethodCommand.class);
} catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof UnsupportedOperationException);
}
}

@Test
public void testCommandMethodsThatThrowsExecutionException() {
try {
CommandLine.invoke("throwsExecutionException", StaticMethodCommand.class);
} catch (ExecutionException ex) {
assertEquals("abc", ex.getMessage());
}
}

@Test
public void testCommandMethodsThatThrowsException() {
try {
CommandLine.invoke("throwsOtherException", StaticMethodCommand.class);
} catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IndexOutOfBoundsException);
}
}

@Command(addMethodSubcommands = false)
static class ErroringCommand {
public ErroringCommand() { // InvocationTargetException when invoking constructor
throw new IllegalStateException("boom");
}
@Command
public void cannotBeCalled() { }
}

@Test
public void testCommandMethodsWhereConstructorThrowsException() {
try {
CommandLine.invoke("cannotBeCalled", ErroringCommand.class);
} catch (ExecutionException ex) { // InvocationTargetException when invoking constructor
assertTrue(ex.getCause() instanceof IllegalStateException);
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while calling command ("));
}
}

@Test
public void testCommandMethodsUnexpectedError() throws Exception {
Method method = CommandMethod1.class.getDeclaredMethod("times", int.class, int.class);
CommandLine cmd = new CommandLine(method);

Method execute = CommandLine.class.getDeclaredMethod("execute", CommandLine.class, List.class);
execute.setAccessible(true);
try {
execute.invoke(null, cmd, null);
} catch (InvocationTargetException ex) {
ExecutionException actual = (ExecutionException) ex.getCause();
assertTrue(actual.getMessage(), actual.getMessage().startsWith("Unhandled error while calling command ("));
}
}
}
56 changes: 53 additions & 3 deletions src/test/java/picocli/CommandLineHelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.SystemOutRule;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.*;
import picocli.CommandLine.Help.Ansi.IStyle;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class CommandLineHelpTest {

@Rule
public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false");
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@After
public void after() {
Expand Down Expand Up @@ -3172,21 +3175,51 @@ public void test200NPEWithEmptyCommandName() throws UnsupportedEncodingException
}

@Test
public void testPrintHelpIfRequestedReturnsTrueForUsageHelp() throws IOException {
public void testPrintHelpIfRequested1ReturnsTrueForUsageHelp() throws IOException {
class App {
@Option(names = "-h", usageHelp = true) boolean usageRequested;
}
List<CommandLine> list = new CommandLine(new App()).parse("-h");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(baos);
assertTrue(CommandLine.printHelpIfRequested(list, out, out, Help.Ansi.OFF));
assertTrue(CommandLine.printHelpIfRequested(list, out, Help.Ansi.OFF));

String expected = String.format("" +
"Usage: <main class> [-h]%n" +
" -h%n");
assertEquals(expected, baos.toString());
}


@Test
public void testPrintHelpIfRequestedWithParseResultReturnsTrueForUsageHelp() throws IOException {
class App {
@Option(names = "-h", usageHelp = true) boolean usageRequested;
}
ParseResult parseResult = new CommandLine(new App()).parseArgs("-h");
assertTrue(CommandLine.printHelpIfRequested(parseResult));

String expected = String.format("" +
"Usage: <main class> [-h]%n" +
" -h%n");
assertEquals(expected, systemOutRule.getLog());
}

@Test
public void testPrintHelpIfRequested2ReturnsTrueForUsageHelp() throws IOException {
class App {
@Option(names = "-h", usageHelp = true) boolean usageRequested;
}
List<CommandLine> list = new CommandLine(new App()).parse("-h");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(baos);
assertTrue(CommandLine.printHelpIfRequested(list, out, out, Help.Ansi.OFF));

String expected = String.format("" +
"Usage: <main class> [-h]%n" +
" -h%n");
assertEquals(expected, baos.toString());
}

@Test
public void testPrintHelpIfRequestedWithCustomColorScheme() {
ColorScheme customColorScheme = new Help.ColorScheme(Help.Ansi.ON)
Expand Down Expand Up @@ -3243,6 +3276,23 @@ class App {
assertEquals(expected, baos.toString());
}

@Test
public void testPrintHelpIfRequestedForHelpCommandThatDoesNotImplementIHelpCommandInitializable() {
@Command(name = "help", helpCommand = true)
class MyHelp implements Runnable {
public void run() {
}
}
@Command(subcommands = MyHelp.class)
class App implements Runnable {
public void run() {
}
}
CommandLine cmd = new CommandLine(new App(), new InnerClassFactory(this));
ParseResult result = cmd.parseArgs("help");
assertTrue(CommandLine.printHelpIfRequested(result));
}

@Command(name = "top", subcommands = {Sub.class})
static class Top {
@Option(names = "-o", required = true) String mandatory;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/picocli/CommandLineParseResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ class App {
assertNull(parseResult.matchedPositionalValue(2, null));
}

@Test
public void testIsUsageHelpRequested_initiallyFalse() {
@Command(mixinStandardHelpOptions = true)
class App {
@Option(names = "-x") String x;
}
CommandLine cmd = new CommandLine(new App());
assertFalse(cmd.isUsageHelpRequested());
}

@Test
public void testIsUsageHelpRequested() {
@Command(mixinStandardHelpOptions = true)
Expand All @@ -282,6 +292,16 @@ class App {
assertTrue(parseResult.matchedPositionals().isEmpty());
}

@Test
public void testIsVersionHelpRequested_initiallyFalse() {
@Command(mixinStandardHelpOptions = true)
class App {
@Option(names = "-x") String x;
}
CommandLine cmd = new CommandLine(new App());
assertFalse(cmd.isVersionHelpRequested());
}

@Test
public void testIsVersionHelpRequested() {
@Command(mixinStandardHelpOptions = true)
Expand All @@ -296,6 +316,16 @@ class App {
assertSame(cmd.getCommandSpec().optionsMap().get("--version"), parseResult.matchedOption('V'));
}

@Test
public void testGetParseResult_initiallyNull() {
@Command(mixinStandardHelpOptions = true)
class App {
@Option(names = "-x") String x;
}
CommandLine cmd = new CommandLine(new App());
assertNull(cmd.getParseResult());
}

@Test
public void testMatchedOptions_ReturnsOnlyMatchedOptions() {
class App {
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/picocli/CommandLineParseWithHandlersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import picocli.CommandLine.Model.CommandSpec;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -751,4 +752,58 @@ public void testRunWithFactoryInvalidInput() {
assertEquals("", systemOutRule.getLog());
assertEquals("", systemErrRule.getLog());
}

@Test
public void testExecutionExceptionIfRunnableThrowsExecutionException() {
@Command
class App implements Runnable {
@Spec CommandSpec spec;
public void run() {
throw new ExecutionException(spec.commandLine(), "abc");
}
}
try {
CommandLine.run(new App());
} catch (ExecutionException ex) {
assertEquals("abc", ex.getMessage());
}
}

@Test
public void testExecutionExceptionIfCallableThrowsExecutionException() {
@Command
class App implements Callable<Void> {
@Spec CommandSpec spec;
public Void call() {
throw new ExecutionException(spec.commandLine(), "abc");
}
}
try {
CommandLine.call(new App());
} catch (ExecutionException ex) {
assertEquals("abc", ex.getMessage());
}
}

@Test
public void testParameterExceptionIfCallableThrowsParameterException() {
@Command
class App implements Callable<Void> {
@Spec CommandSpec spec;
public Void call() {
throw new ParameterException(spec.commandLine(), "xxx");
}
}
try {
CommandLine.call(new App());
} catch (ParameterException ex) {
assertEquals("xxx", ex.getMessage());
}
}

@Test
public void testRunAllSelf() {
RunAll runAll = new RunAll();
assertSame(runAll, runAll.self());
}
}
13 changes: 13 additions & 0 deletions src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,19 @@ class SingleValue {
assertEquals(Arrays.asList("val3"), cmd.getUnmatchedArguments());
}

@Test
public void testUnmatchedArgsInitiallyEmpty() throws Exception {
class SingleValue {
@Parameters(index = "0..2") String[] str;
}
setTraceLevel("OFF");
CommandLine cmd = new CommandLine(new SingleValue());
assertTrue(cmd.getUnmatchedArguments().isEmpty());

CommandLine cmd2 = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true);
assertTrue(cmd2.getUnmatchedArguments().isEmpty());
}

@Test
public void testPositionalParamSingleValueButWithoutIndex() throws Exception {
class SingleValue {
Expand Down
Loading

0 comments on commit 558404c

Please sign in to comment.