Skip to content

Commit

Permalink
added tests for ModelValidator - Broken validators
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Nov 13, 2023
1 parent dd75183 commit 4e734c7
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public boolean validate() {
if (attributeNamesToConstructorParameterMapping.length > 0) {

// First check if annotation attribute Names are correct
String[] invalidNames = Arrays.stream(attributeNamesToConstructorParameterMapping).filter(e -> !this.validatorAnnotation.getAttribute(e).isPresent()).toArray(String[]::new);
String[] invalidNames = Arrays.stream(attributeNamesToConstructorParameterMapping).filter(e -> !this.validatorAnnotation.hasAttribute(e)).toArray(String[]::new);
if (invalidNames.length > 0) {
this.annotatedElement.compilerMessage(this.validatorAnnotation.unwrap()).asError().write(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_ATTRIBUTE_NAME_MISMATCH, this.validatorAnnotation.asElement().getSimpleName(), invalidNames);
return false;
}


Expand Down Expand Up @@ -73,7 +74,7 @@ public boolean validate() {
return true;
}

annotatedElement.compilerMessage(validatorAnnotation.unwrap()).asError().write(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_MISSING_NOARG_CONSTRUCTOR, validatorImplType.getSimpleName());
annotatedElement.compilerMessage(validatorAnnotation.unwrap()).asError().write(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_CONSTRUCTOR_PARAMETER_MAPPING, validatorImplType.getSimpleName());
return false;
} else {
// must have a noarg constructor or just the default
Expand All @@ -83,6 +84,7 @@ public boolean validate() {

if (!(hasNoargConstructor || hasJustDefaultConstructor)) {
annotatedElement.compilerMessage(validatorAnnotation.unwrap()).asError().write(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_MISSING_NOARG_CONSTRUCTOR, validatorImplTypeElement.getSimpleName());
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,160 @@ public void test_validate() {
.executeTest();
}

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@FluentApiValidator(value = WrongAttributeNameToConstructorParameterMapping.MyValidator.class, attributeNamesToConstructorParameterMapping = {"sadas"})
@interface WrongAttributeNameToConstructorParameterMapping {

String attr();

public class MyValidator implements Validator<String> {

final String attr;

public MyValidator (String attr){
this.attr = attr;
}
@Override
public boolean validate(String obj) {

return false;
}
}

}


interface WrongAttributeNameToConstructorParameterMappingTest {

void method(@PassIn @WrongAttributeNameToConstructorParameterMapping(attr = "abc") String parameter);

}

@Test
public void test_validate_invalidValidatorWrong() {
unitTestBuilder.defineTestWithPassedInElement(WrongAttributeNameToConstructorParameterMappingTest.class, (UnitTest<VariableElement>) (processingEnvironment, element) -> {

try {
ToolingProvider.setTooling(processingEnvironment);

AnnotationMirrorWrapper annotationMirrorWrapper = AnnotationMirrorWrapper.get(element, WrongAttributeNameToConstructorParameterMapping.class).get();

ModelValidator unit = new ModelValidator(VariableElementWrapper.wrap(element), annotationMirrorWrapper);

MatcherAssert.assertThat("Validation must be false", !unit.validate());

} finally {
ToolingProvider.clearTooling();
}

})
.compilationShouldFail()
.expectErrorMessage().thatContains(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_ATTRIBUTE_NAME_MISMATCH.getCode())
.executeTest();
}

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@FluentApiValidator(value = MissingNoargConstructor.MyValidator.class)
@interface MissingNoargConstructor {

class MyValidator implements Validator<String> {

public MyValidator(String attr) {

}

@Override
public boolean validate(String obj) {

return false;
}
}

}


interface MissingNoargConstructorTest {

void method(@PassIn @MissingNoargConstructor String parameter);

}

@Test
public void test_validate_missingNoargConstructor() {
unitTestBuilder.defineTestWithPassedInElement(MissingNoargConstructorTest.class, (UnitTest<VariableElement>) (processingEnvironment, element) -> {

try {
ToolingProvider.setTooling(processingEnvironment);

AnnotationMirrorWrapper annotationMirrorWrapper = AnnotationMirrorWrapper.get(element, MissingNoargConstructor.class).get();

ModelValidator unit = new ModelValidator(VariableElementWrapper.wrap(element), annotationMirrorWrapper);

MatcherAssert.assertThat("Validation must be false", !unit.validate());

} finally {
ToolingProvider.clearTooling();
}

})
.compilationShouldFail()
.expectErrorMessage().thatContains(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_MISSING_NOARG_CONSTRUCTOR.getCode())
.executeTest();
}


@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@FluentApiValidator(value = NoMatchingConstructor.MyValidator.class, attributeNamesToConstructorParameterMapping = {"attr"})
@interface NoMatchingConstructor {

String attr();

class MyValidator implements Validator<String> {

public MyValidator(Class<?> attr) {

}

@Override
public boolean validate(String obj) {

return false;
}
}

}


interface NonMatchingConstructorTest {

void method(@PassIn @NoMatchingConstructor(attr = "TEST") String parameter);

}

@Test
public void test_validate_noMatchingConstructor() {
unitTestBuilder.defineTestWithPassedInElement(NonMatchingConstructorTest.class, (UnitTest<VariableElement>) (processingEnvironment, element) -> {

try {
ToolingProvider.setTooling(processingEnvironment);

AnnotationMirrorWrapper annotationMirrorWrapper = AnnotationMirrorWrapper.get(element, NoMatchingConstructor.class).get();

ModelValidator unit = new ModelValidator(VariableElementWrapper.wrap(element), annotationMirrorWrapper);

MatcherAssert.assertThat("Validation must be false", !unit.validate());

} finally {
ToolingProvider.clearTooling();
}

})
.compilationShouldFail()
.expectErrorMessage().thatContains(FluentApiProcessorCompilerMessages.ERROR_BROKEN_VALIDATOR_CONSTRUCTOR_PARAMETER_MAPPING.getCode())
.executeTest();
}

}

0 comments on commit 4e734c7

Please sign in to comment.