Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify SelfValidatingValidator (#2413)
Remove use of Javassist from SelfValidatingValidator in favor of plain old Java.
- Loading branch information
1 parent
4cb87b0
commit bfa2e3e
Showing
2 changed files
with
101 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 73 additions & 40 deletions
113
...on/src/test/java/io/dropwizard/validation/selfvalidating/SelfValidatingValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,93 +1,126 @@ | |||
package io.dropwizard.validation.selfvalidating; | package io.dropwizard.validation.selfvalidating; | ||
|
|
||
import static org.assertj.core.api.Assertions.assertThat; | |||
|
|||
import java.util.Arrays; | |||
|
|||
import org.junit.Test; | |||
|
|||
import com.fasterxml.classmate.AnnotationConfiguration; | import com.fasterxml.classmate.AnnotationConfiguration; | ||
import com.fasterxml.classmate.AnnotationInclusion; | import com.fasterxml.classmate.AnnotationInclusion; | ||
import com.fasterxml.classmate.MemberResolver; | import com.fasterxml.classmate.MemberResolver; | ||
import com.fasterxml.classmate.ResolvedTypeWithMembers; | import com.fasterxml.classmate.ResolvedTypeWithMembers; | ||
import com.fasterxml.classmate.TypeResolver; | import com.fasterxml.classmate.TypeResolver; | ||
import com.fasterxml.classmate.members.ResolvedMethod; | import com.fasterxml.classmate.members.ResolvedMethod; | ||
import io.dropwizard.validation.BaseValidator; | |||
import org.junit.Test; | |||
|
|
||
public class SelfValidatingValidatorTest { | import javax.validation.ConstraintViolation; | ||
|
import javax.validation.Validator; | ||
@SelfValidating | import java.util.Arrays; | ||
public static class InvalidExample { | import java.util.Set; | ||
|
|
||
@SelfValidation | import static org.assertj.core.api.Assertions.assertThat; | ||
public void validateCorrect(ViolationCollector col) { | |||
} | |||
|
|
||
@SelfValidation | public class SelfValidatingValidatorTest { | ||
public void validateFailAdditionalParameters(ViolationCollector col, int a) { | private SelfValidatingValidator selfValidatingValidator = new SelfValidatingValidator(); | ||
} | |||
|
|
||
@SelfValidation | @Test | ||
public boolean validateFailReturn(ViolationCollector col) { | public void validObjectHasNoViolations() throws Exception { | ||
return true; | final Validator validator = BaseValidator.newValidator(); | ||
} | final Set<ConstraintViolation<ValidExample>> violations = validator.validate(new ValidExample(1)); | ||
assertThat(violations).isEmpty(); | |||
} | |||
|
|
||
@SelfValidation | @Test | ||
private void validateFailPrivate(ViolationCollector col) { | public void invalidObjectHasViolations() throws Exception { | ||
} | final Validator validator = BaseValidator.newValidator(); | ||
final Set<ConstraintViolation<ValidExample>> violations = validator.validate(new ValidExample(-1)); | |||
assertThat(violations) | |||
.isNotEmpty() | |||
.allSatisfy(violation -> assertThat(violation.getMessage()).isEqualTo("n must be positive!")); | |||
} | } | ||
|
|||
private SelfValidatingValidator selfValidatingValidator = new SelfValidatingValidator(); | |||
|
|
||
@Test | @Test | ||
public void correctMethod() throws Exception { | public void correctMethod() throws Exception { | ||
assertThat(selfValidatingValidator.isMethodCorrect( | assertThat(selfValidatingValidator.isMethodCorrect( | ||
getMethod("validateCorrect", ViolationCollector.class))) | getMethod("validateCorrect", ViolationCollector.class))) | ||
.isTrue(); | .isTrue(); | ||
} | } | ||
|
|
||
@Test | @Test | ||
public void voidIsNotAccepted() throws Exception { | public void voidIsNotAccepted() throws Exception { | ||
assertThat(selfValidatingValidator.isMethodCorrect( | assertThat(selfValidatingValidator.isMethodCorrect( | ||
getMethod("validateFailReturn", ViolationCollector.class))) | getMethod("validateFailReturn", ViolationCollector.class))) | ||
.isFalse(); | .isFalse(); | ||
} | } | ||
|
|
||
@Test | @Test | ||
public void privateIsNotAccepted() throws Exception { | public void privateIsNotAccepted() throws Exception { | ||
assertThat(selfValidatingValidator.isMethodCorrect( | assertThat(selfValidatingValidator.isMethodCorrect( | ||
getMethod("validateFailPrivate", ViolationCollector.class))) | getMethod("validateFailPrivate", ViolationCollector.class))) | ||
.isFalse(); | .isFalse(); | ||
} | } | ||
|
|
||
@Test | @Test | ||
public void additionalParametersAreNotAccepted() throws Exception { | public void additionalParametersAreNotAccepted() throws Exception { | ||
assertThat(selfValidatingValidator.isMethodCorrect( | assertThat(selfValidatingValidator.isMethodCorrect( | ||
getMethod("validateFailAdditionalParameters", ViolationCollector.class, int.class))) | getMethod("validateFailAdditionalParameters", ViolationCollector.class, int.class))) | ||
.isFalse(); | .isFalse(); | ||
} | } | ||
|
|||
private ResolvedMethod getMethod(String name, Class<?>... params) { | private ResolvedMethod getMethod(String name, Class<?>... params) { | ||
AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration.StdConfiguration(AnnotationInclusion.INCLUDE_AND_INHERIT_IF_INHERITED); | AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration.StdConfiguration(AnnotationInclusion.INCLUDE_AND_INHERIT_IF_INHERITED); | ||
TypeResolver typeResolver = new TypeResolver(); | TypeResolver typeResolver = new TypeResolver(); | ||
MemberResolver memberResolver = new MemberResolver(typeResolver); | MemberResolver memberResolver = new MemberResolver(typeResolver); | ||
ResolvedTypeWithMembers annotatedType = memberResolver.resolve(typeResolver.resolve(InvalidExample.class), annotationConfiguration, null); | ResolvedTypeWithMembers annotatedType = memberResolver.resolve(typeResolver.resolve(InvalidExample.class), annotationConfiguration, null); | ||
for(ResolvedMethod m : annotatedType.getMemberMethods()) { | for (ResolvedMethod m : annotatedType.getMemberMethods()) { | ||
if(hasSignature(m, name, params)) { | if (hasSignature(m, name, params)) { | ||
return m; | return m; | ||
} | } | ||
} | } | ||
throw new IllegalStateException("Could not resolve method "+name+Arrays.toString(params)+" in "+InvalidExample.class); | throw new IllegalStateException("Could not resolve method " + name + Arrays.toString(params) + " in " + InvalidExample.class); | ||
} | } | ||
|
|
||
private boolean hasSignature(ResolvedMethod m, String name, Class<?>[] params) { | private boolean hasSignature(ResolvedMethod m, String name, Class<?>[] params) { | ||
if(!m.getName().equals(name) || m.getArgumentCount() != params.length) { | if (!m.getName().equals(name) || m.getArgumentCount() != params.length) { | ||
return false; | return false; | ||
} | } | ||
for(int i=0 ; i < params.length ; i++) { | for (int i = 0; i < params.length; i++) { | ||
if(!m.getArgumentType(i).getErasedType().equals(params[i])) | if (!m.getArgumentType(i).getErasedType().equals(params[i])) | ||
return false; | return false; | ||
} | } | ||
return true; | return true; | ||
} | } | ||
|
|
||
|
|||
@SelfValidating | |||
static class InvalidExample { | |||
@SelfValidation | |||
public void validateCorrect(ViolationCollector col) { | |||
} | |||
|
|||
@SelfValidation | |||
public void validateFailAdditionalParameters(ViolationCollector col, int a) { | |||
} | |||
|
|||
@SelfValidation | |||
public boolean validateFailReturn(ViolationCollector col) { | |||
return true; | |||
} | |||
|
|||
@SelfValidation | |||
private void validateFailPrivate(ViolationCollector col) { | |||
} | |||
} | |||
|
|||
@SelfValidating | |||
static class ValidExample { | |||
final int n; | |||
|
|||
ValidExample(int n) { | |||
this.n = n; | |||
} | |||
|
|||
@SelfValidation | |||
public void validate(ViolationCollector col) { | |||
if (n < 0) { | |||
col.addViolation("n must be positive!"); | |||
} | |||
} | |||
} | |||
} | } |