diff --git a/fluapigen-processor/src/main/java/io/toolisticon/fluapigen/processor/ModelValidator.java b/fluapigen-processor/src/main/java/io/toolisticon/fluapigen/processor/ModelValidator.java index 65ebbc7..4145fe3 100644 --- a/fluapigen-processor/src/main/java/io/toolisticon/fluapigen/processor/ModelValidator.java +++ b/fluapigen-processor/src/main/java/io/toolisticon/fluapigen/processor/ModelValidator.java @@ -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; } @@ -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 @@ -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; } } diff --git a/fluapigen-processor/src/test/java/io/toolisticon/fluapigen/processor/ValidatorTest.java b/fluapigen-processor/src/test/java/io/toolisticon/fluapigen/processor/ValidatorTest.java index b11a1f9..2533e07 100644 --- a/fluapigen-processor/src/test/java/io/toolisticon/fluapigen/processor/ValidatorTest.java +++ b/fluapigen-processor/src/test/java/io/toolisticon/fluapigen/processor/ValidatorTest.java @@ -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 { + + 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) (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 { + + 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) (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 { + + 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) (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(); + } }