From 1944a86902616323acc8197a19a3e70120f82b17 Mon Sep 17 00:00:00 2001 From: Tobias Stamann Date: Fri, 30 Jun 2017 16:34:10 +0200 Subject: [PATCH] [#2] Added testcases to enhance test coverage --- ...GenericElementCharacteristicValidator.java | 3 + .../characteristicsvalidator/Validator.java | 3 + .../tools/ElementUtils_CastElementTest.java | 44 +++++ .../ValidatorTest.java | 164 ++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/ValidatorTest.java diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/GenericElementCharacteristicValidator.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/GenericElementCharacteristicValidator.java index 8176efd9..db3b55af 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/GenericElementCharacteristicValidator.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/GenericElementCharacteristicValidator.java @@ -165,5 +165,8 @@ public boolean hasNoneOf(Element element, T... characteristicsToCheck) { return true; } + public GenericElementCharacteristicMatcher getMatcher() { + return matcher; + } } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/Validator.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/Validator.java index f47fc4f6..3745ba87 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/Validator.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/Validator.java @@ -46,6 +46,9 @@ public GenericElementCharacteristicValidator getValidator() { return validator; } + + + /** * Convenience method for getting and using a Modifier matching validator. * diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/ElementUtils_CastElementTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/ElementUtils_CastElementTest.java index c1444ac4..250ffab0 100644 --- a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/ElementUtils_CastElementTest.java +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/ElementUtils_CastElementTest.java @@ -54,6 +54,9 @@ public void isTypeElement_testIfAllElementKindsWillBeDetectedCorrectly() { Mockito.when(element.getKind()).thenReturn(ElementKind.PARAMETER); MatcherAssert.assertThat("Should not be detected as TypeElement", !ElementUtils.CastElement.isTypeElement(element)); + // test null safety + MatcherAssert.assertThat("Should return false for null valued parameter", !ElementUtils.CastElement.isTypeElement(null)); + } @@ -95,6 +98,9 @@ public void isVariableElement_testIfAllElementKindsWillBeDetectedCorrectly() { Mockito.when(element.getKind()).thenReturn(ElementKind.CLASS); MatcherAssert.assertThat("Should not be detected as VariableElement", !ElementUtils.CastElement.isVariableElement(element)); + // test null safety + MatcherAssert.assertThat("Should return false for null valued parameter", !ElementUtils.CastElement.isVariableElement(null)); + } @Test @@ -135,6 +141,9 @@ public void isExecutableElement_testIfAllElementKindsWillBeDetectedCorrectly() { Mockito.when(element.getKind()).thenReturn(ElementKind.CLASS); MatcherAssert.assertThat("Should not be detected as ExecutableElement", !ElementUtils.CastElement.isExecutableElement(element)); + // test null safety + MatcherAssert.assertThat("Should return false for null valued parameter", !ElementUtils.CastElement.isExecutableElement(null)); + } @@ -166,6 +175,11 @@ public void castClassTest_MustThrowErrorForVariableElement() { } + @Test + public void castClassTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castClass(null), Matchers.nullValue()); + } + // --------------------------------------- // castInterface ------------------------- // --------------------------------------- @@ -194,6 +208,11 @@ public void castInterfaceTest_MustThrowErrorForVariableElement() { } + @Test + public void castInterfaceTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castInterface(null), Matchers.nullValue()); + } + // --------------------------------------- // castEnum ----------------------------- // --------------------------------------- @@ -222,6 +241,11 @@ public void castEnumTest_MustThrowErrorForVariableElement() { } + @Test + public void castEnumTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castEnum(null), Matchers.nullValue()); + } + // --------------------------------------- // castParameter ------------------------ // --------------------------------------- @@ -250,6 +274,11 @@ public void castParameterTest_MustThrowErrorForTypeElement() { } + @Test + public void castParameterTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castParameter(null), Matchers.nullValue()); + } + // --------------------------------------- // castField ---------------------------- // --------------------------------------- @@ -278,6 +307,11 @@ public void castFieldTest_MustThrowErrorForTypeElement() { } + @Test + public void castFieldTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castField(null), Matchers.nullValue()); + } + // --------------------------------------- // castConstructor ---------------------- // --------------------------------------- @@ -306,6 +340,11 @@ public void castConstructorTest_MustThrowErrorForTypeElement() { } + @Test + public void castConstructorTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castConstructor(null), Matchers.nullValue()); + } + // --------------------------------------- // castMethod --------------------------- // --------------------------------------- @@ -334,4 +373,9 @@ public void castMethodTest_MustThrowErrorForTypeElement() { } + @Test + public void castMethodTest_shouldHandleNullValuedParameterCorrectly() { + MatcherAssert.assertThat(ElementUtils.CastElement.castMethod(null), Matchers.nullValue()); + } + } diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/ValidatorTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/ValidatorTest.java new file mode 100644 index 00000000..88a9cdac --- /dev/null +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsvalidator/ValidatorTest.java @@ -0,0 +1,164 @@ +package de.holisticon.annotationprocessortoolkit.tools.characteristicsvalidator; + +import de.holisticon.annotationprocessortoolkit.tools.characteristicsmatcher.Matcher; +import de.holisticon.annotationprocessortoolkit.tools.characteristicsmatcher.ParameterExecutableElementCharacteristicMatcher; +import de.holisticon.annotationprocessortoolkit.tools.characteristicsmatcher.ParameterFQNExecutableElementCharacteristicMatcher; +import de.holisticon.annotationprocessortoolkit.tools.characteristicsmatcher.TypeElementCharacteristicMatcher; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.Modifier; +import java.lang.annotation.Annotation; + +/** + * unit test for {@link Validator} class. + */ +public class ValidatorTest { + + + // ------------------------------------ + // -- MODIFIER_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testModifierValidator() { + + MatcherAssert.assertThat(Validator.MODIFIER_VALIDATOR.getValidator().getMatcher(), Matchers.is(Matcher.MODIFIER_MATCHER.getMatcher())); + + } + + @Test + public void testModifierValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(((GenericElementCharacteristicValidator) Validator.getModifierValidator()).getMatcher(), Matchers.is(Matcher.MODIFIER_MATCHER.getMatcher())); + + } + + // ------------------------------------ + // -- ANNOTATION_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testAnnotationValidator() { + + MatcherAssert.assertThat(Validator.ANNOTATION_VALIDATOR.getValidator().getMatcher(), Matchers.is(Matcher.ANNOTATION_MATCHER.getMatcher())); + + } + + @Test + public void testAnnotationValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(((GenericElementCharacteristicValidator>) Validator.getAnnotationValidator()).getMatcher(), Matchers.is(Matcher.ANNOTATION_MATCHER.getMatcher())); + + } + + // ------------------------------------ + // -- NAME_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testNameValidator() { + + MatcherAssert.assertThat(Validator.NAME_VALIDATOR.getValidator().getMatcher(), Matchers.is(Matcher.NAME_MATCHER.getMatcher())); + + } + + @Test + public void testNameValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(((GenericElementCharacteristicValidator) Validator.getNameValidator()).getMatcher(), Matchers.is(Matcher.NAME_MATCHER.getMatcher())); + + } + + // ------------------------------------ + // -- REGEX_NAME_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testRegexNameValidator() { + + MatcherAssert.assertThat(Validator.REGEX_NAME_VALIDATOR.getValidator().getMatcher(), Matchers.is(Matcher.REGEX_NAME_MATCHER.getMatcher())); + + } + + @Test + public void testRegexNameValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(((GenericElementCharacteristicValidator) Validator.getRegexNameValidator()).getMatcher(), Matchers.is(Matcher.REGEX_NAME_MATCHER.getMatcher())); + + } + + // ------------------------------------ + // -- ELEMENT_KIND_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testElementKindValidator() { + + MatcherAssert.assertThat(Validator.ELEMENT_KIND_VALIDATOR.getValidator().getMatcher(), Matchers.is(Matcher.ELEMENT_KIND_MATCHER.getMatcher())); + + } + + @Test + public void testElementKindValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(((GenericElementCharacteristicValidator) Validator.getElementKindValidator()).getMatcher(), Matchers.is(Matcher.ELEMENT_KIND_MATCHER.getMatcher())); + + } + + // ------------------------------------ + // -- PARAMETER_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testParameterValidator() { + + MatcherAssert.assertThat(Validator.PARAMETER_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(ParameterExecutableElementCharacteristicMatcher.class)); + + } + + @Test + public void testParameterValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(Validator.PARAMETER_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(ParameterExecutableElementCharacteristicMatcher.class)); + } + + // ------------------------------------ + // -- PARAMETER_FQN_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testParameterFQNValidator() { + + MatcherAssert.assertThat(Validator.PARAMETER_FQN_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(ParameterFQNExecutableElementCharacteristicMatcher.class)); + + } + + @Test + public void testParameterFQNValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(Validator.PARAMETER_FQN_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(ParameterFQNExecutableElementCharacteristicMatcher.class)); + } + + // ------------------------------------ + // -- TYPE_VALIDATOR Validator tests + // ------------------------------------ + + @Test + public void testTypeValidator() { + + MatcherAssert.assertThat(Validator.TYPE_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(TypeElementCharacteristicMatcher.class)); + + } + + + @Test + public void testTypeValidator_GetValidatorByMethod() { + + MatcherAssert.assertThat(Validator.TYPE_VALIDATOR(null).getValidator().getMatcher(), Matchers.instanceOf(TypeElementCharacteristicMatcher.class)); + } + + +}