Skip to content

Commit

Permalink
#571 more CommandReflection tests (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 22, 2018
1 parent 542975c commit 9c8e6b1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6441,10 +6441,11 @@ private static void validateUnmatched(TypedMember member) {
}
}
private static void validateArgSpecField(TypedMember member) {
if (!member.isArgSpec()) { throw new IllegalStateException("Bug: validateArgSpecField() should only be called with an @Option or @Parameters member"); }
if (member.isOption() && member.isParameter()) {
throw new DuplicateOptionAnnotationsException("A member can be either @Option or @Parameters, but '" + member + "' is both.");
}
if (member.isMixin() && member.isArgSpec()) {
if (member.isMixin()) {
throw new DuplicateOptionAnnotationsException("A member cannot be both a @Mixin command and an @Option or @Parameters, but '" + member + "' is both.");
}
if (!(member.accessible instanceof Field)) { return; }
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/picocli/CommandLineModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package picocli;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
Expand Down Expand Up @@ -2542,6 +2543,7 @@ public void testCommandReflection_validateArgSpecField_final() throws Exception
validateArgSpecField.invoke(null, typedMember); // no error
}

@Ignore
@Test
public void testCommandReflection_validateArgSpecField_neither() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Expand Down Expand Up @@ -2669,6 +2671,21 @@ public void testCommandReflection_ValidateInjectSpec_FieldType() throws Exceptio
assertEquals("@picocli.CommandLine.Spec annotation is only supported on fields of type picocli.CommandLine$Model$CommandSpec", ex.getMessage());
}
}
@Test
public void testCommandReflection_ValidateArgSpec() throws Exception {
Class<?> reflection = Class.forName("picocli.CommandLine$Model$CommandReflection");
Method validateArgSpec = reflection.getDeclaredMethod("validateArgSpecField", TypedMember.class);
validateArgSpec.setAccessible(true);

TypedMember typedMember = new TypedMember(ValidateInjectSpec.class.getDeclaredField("notAnnotated"));
try {
validateArgSpec.invoke(null, typedMember);
fail("expected Exception");
} catch (InvocationTargetException ite) {
IllegalStateException ex = (IllegalStateException) ite.getCause();
assertEquals("Bug: validateArgSpecField() should only be called with an @Option or @Parameters member", ex.getMessage());
}
}

@Command(mixinStandardHelpOptions = true)
static class ValidMixin {
Expand Down Expand Up @@ -2702,4 +2719,24 @@ public <K> K create(Class<K> cls) throws Exception {
assertEquals("Could not access or modify mixin member picocli.CommandLineModelTest$ValidMixin picocli.CommandLineModelTest$MixeeUninstantiated.mixin: java.lang.IllegalStateException: boom", ex.getMessage());
}
}

@Command
class MyUnmatched {
@CommandLine.Unmatched
List raw;
}
@Test(expected = InitializationException.class)
public void testCommandReflection_buildUnmatchedForField_raw() throws Exception {
CommandSpec.forAnnotatedObject(new MyUnmatched());
}

@Command
class MyUnmatched2 {
@CommandLine.Unmatched
List<String> raw = new ArrayList<String>();
}
@Test
public void testCommandReflection_buildUnmatchedForField_valid() throws Exception {
CommandSpec.forAnnotatedObject(new MyUnmatched2());
}
}

0 comments on commit 9c8e6b1

Please sign in to comment.