Skip to content

Commit

Permalink
Stop Jacaoco's synthetic methods breaking a test
Browse files Browse the repository at this point in the history
  • Loading branch information
wjsrobertson committed Jul 5, 2016
1 parent dd51201 commit ba924bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ public class TestMethodArgumentValidator {

private static final Set<Class<?>> ALLOWED_TYPES = setOf(ReadCell.class, ReadColumn.class);

public boolean hasValidArguments(Method m){
public boolean hasValidArguments(Method m) {
boolean valid = true;

for (Parameter parameter : m.getParameters()) {
for (Class<?> allowedType : ALLOWED_TYPES) {
Annotation[] annotations = parameter.getAnnotations();
boolean hasValid = hasValidAnnotation(annotations);
if (! hasValid) {
return false;
}
}
Annotation[] annotations = parameter.getAnnotations();
boolean hasValid = hasValidAnnotation(annotations);
valid &= hasValid;
}

return true;
return valid;
}

private boolean hasValidAnnotation(Annotation[] annotations) {
boolean hasValid = false;

for (Annotation annotation : annotations) {
if (ALLOWED_TYPES.contains(annotation.annotationType())) {
hasValid = true;
}
}

return hasValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,45 @@ public void validTestMethod(@ReadColumn(from="A1", to="B1") List<Integer> x) {

static class HasInNoAnnotationArg {
@Test
public void validTestMethod(int x) {
public void invalidTestMethod(int x) {
}
}

@Test
public void checkHasValidArgumentsForCellArgs() throws NoSuchMethodException {
Method validTestMethod = HasValidCellArgs.class.getDeclaredMethods()[0];
Method validTestMethod = firstNonSyntheticMethod(HasValidCellArgs.class);

boolean valid = underTest.hasValidArguments(validTestMethod);

assertThat(valid).isTrue();
}

@Test
public void checkHasValidArgumentsForColumnArgs() throws NoSuchMethodException {
Method validTestMethod = HasValidColumnArg.class.getDeclaredMethods()[0];
Method validTestMethod = firstNonSyntheticMethod(HasValidColumnArg.class);

boolean valid = underTest.hasValidArguments(validTestMethod);

assertThat(valid).isTrue();
}

@Test
public void checkHasInvalidArgumentsForNonAnnotatedArg() throws NoSuchMethodException {
Method validTestMethod = HasInNoAnnotationArg.class.getDeclaredMethods()[0];
Method inValidTestMethod = firstNonSyntheticMethod(HasInNoAnnotationArg.class);

boolean valid = underTest.hasValidArguments(inValidTestMethod);

boolean valid = underTest.hasValidArguments(validTestMethod);
assertThat(valid).isFalse();
}

private Method firstNonSyntheticMethod(Class<?> aClass) {
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if (! declaredMethod.isSynthetic()) {
return declaredMethod;
}
}

return null;
}
}

0 comments on commit ba924bb

Please sign in to comment.