Skip to content

Commit

Permalink
#571 fixed CommandReflection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 21, 2018
1 parent a9bfa43 commit 319a32d
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/test/java/picocli/CommandLineModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2464,8 +2464,8 @@ public void testCommandReflection_initSubcommands() throws Exception {
@Command(subcommands = InvalidSub2.class)
static class InvalidTop2 {}

@Command(name = "<main class>")
static class InvalidSub2 {
@Option(names = "-x") int x;
}

@Test
Expand Down Expand Up @@ -2541,10 +2541,16 @@ public void testCommandReflection_validateArgSpecField_final() throws Exception
}

static class ValidateInjectSpec {
int notAnnotated;

@Spec
@Option(names = "-x")
int x;

@Spec
@Parameters
int y;

@Spec
@Unmatched
List<String> unmatched;
Expand All @@ -2556,9 +2562,24 @@ static class ValidateInjectSpec {
@Spec
Object invalidType;
}
@Test
public void testCommandReflection_ValidateInjectSpec() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Method validateInjectSpec = reflection.getDeclaredMethod("validateInjectSpec", TypedMember.class);
validateInjectSpec.setAccessible(true);

TypedMember typedMember = new TypedMember(ValidateInjectSpec.class.getDeclaredField("notAnnotated"));
try {
validateInjectSpec.invoke(null, typedMember);
fail("expected Exception");
} catch (InvocationTargetException ite) {
IllegalStateException ex = (IllegalStateException) ite.getCause();
assertEquals("Bug: validateInjectSpec() should only be called with @Spec members", ex.getMessage());
}
}

@Test
public void testCommandReflection_ValidateInjectSpec_Arg() throws Exception {
public void testCommandReflection_ValidateInjectSpec_Option() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Method validateInjectSpec = reflection.getDeclaredMethod("validateInjectSpec", TypedMember.class);
validateInjectSpec.setAccessible(true);
Expand All @@ -2573,6 +2594,22 @@ public void testCommandReflection_ValidateInjectSpec_Arg() throws Exception {
}
}

@Test
public void testCommandReflection_ValidateInjectSpec_Positional() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Method validateInjectSpec = reflection.getDeclaredMethod("validateInjectSpec", TypedMember.class);
validateInjectSpec.setAccessible(true);

TypedMember typedMember = new TypedMember(ValidateInjectSpec.class.getDeclaredField("y"));
try {
validateInjectSpec.invoke(null, typedMember);
fail("expected Exception");
} catch (InvocationTargetException ite) {
DuplicateOptionAnnotationsException ex = (DuplicateOptionAnnotationsException) ite.getCause();
assertEquals("A member cannot have both @Spec and @Option or @Parameters annotations, but 'int picocli.CommandLineModelTest$ValidateInjectSpec.y' has both.", ex.getMessage());
}
}

@Test
public void testCommandReflection_ValidateInjectSpec_Unmatched() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Expand Down

0 comments on commit 319a32d

Please sign in to comment.