From cfb342e6df026dd7fdb6cfe8ca89755558151fca Mon Sep 17 00:00:00 2001 From: Tobias Stamann Date: Fri, 23 Feb 2018 03:44:32 +0100 Subject: [PATCH] [#18] Add unit tests for CoreMatchers --- .../impl/ByParameterTypeFqnMatcher.java | 32 ++-- .../matcher/impl/ByParameterTypeMatcher.java | 17 +- .../impl/ByParameterTypeFqnMatcherTest.java | 172 ++++++++++++++++++ .../impl/ByParameterTypeMatcherTest.java | 66 +++++++ .../impl/HasVoidReturnTypeMatcherTest.java | 49 +++++ .../impl/IsAssignableToMatcherTest.java | 39 ++++ 6 files changed, 363 insertions(+), 12 deletions(-) create mode 100644 annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcherTest.java create mode 100644 annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/HasVoidReturnTypeMatcherTest.java diff --git a/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcher.java b/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcher.java index 9ff663d6..647c96fe 100644 --- a/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcher.java +++ b/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcher.java @@ -27,28 +27,21 @@ public boolean checkForMatchingCharacteristic(ExecutableElement element, String[ return false; } - // check if element is ExecutableElement - if (!ElementUtils.CastElement.isExecutableElement(element)) { - return false; - } - - // cast to executable element for further checks - ExecutableElement executableElement = ElementUtils.CastElement.castToExecutableElement(element); // check if number of parameters is the same - if (executableElement.getParameters().size() != toCheckFor.length) { + if (element.getParameters().size() != toCheckFor.length) { return false; } - for (int i = 0; i < executableElement.getParameters().size(); i++) { + for (int i = 0; i < element.getParameters().size(); i++) { TypeMirror parameterTypeMirror = TypeUtils.TypeRetrieval.getTypeMirror(toCheckFor[i]); if (parameterTypeMirror == null) { return false; } - if (!executableElement.getParameters().get(i).asType().equals(parameterTypeMirror)) { + if (!element.getParameters().get(i).asType().equals(parameterTypeMirror)) { return false; } } @@ -62,7 +55,24 @@ public boolean checkForMatchingCharacteristic(ExecutableElement element, String[ */ @Override public String getStringRepresentationOfPassedCharacteristic(String[] toGetStringRepresentationFor) { - return toGetStringRepresentationFor != null ? "" : null; + + if (toGetStringRepresentationFor != null) { + StringBuilder stringBuilder = new StringBuilder("["); + boolean isFirst = true; + for (String element : toGetStringRepresentationFor) { + if (isFirst) { + isFirst = false; + } else { + stringBuilder.append(", "); + } + stringBuilder.append(element); + } + stringBuilder.append("]"); + return stringBuilder.toString(); + } else { + return null; + } + } } diff --git a/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcher.java b/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcher.java index e34306fc..0e256118 100644 --- a/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcher.java +++ b/annotationprocessor/src/main/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcher.java @@ -56,7 +56,22 @@ public boolean checkForMatchingCharacteristic(ExecutableElement element, Class[] */ @Override public String getStringRepresentationOfPassedCharacteristic(Class[] toGetStringRepresentationFor) { - return toGetStringRepresentationFor != null ? "" : null; + if (toGetStringRepresentationFor != null) { + StringBuilder stringBuilder = new StringBuilder("["); + boolean isFirst = true; + for (Class element : toGetStringRepresentationFor) { + if (isFirst) { + isFirst = false; + } else { + stringBuilder.append(", "); + } + stringBuilder.append(element.getCanonicalName()); + } + stringBuilder.append("]"); + return stringBuilder.toString(); + } else { + return null; + } } } diff --git a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcherTest.java b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcherTest.java new file mode 100644 index 00000000..5e83e294 --- /dev/null +++ b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeFqnMatcherTest.java @@ -0,0 +1,172 @@ +package io.toolisticon.annotationprocessortoolkit.tools.matcher.impl; + +import io.toolisticon.annotationprocessortoolkit.testhelper.AbstractAnnotationProcessorUnitTest; +import io.toolisticon.annotationprocessortoolkit.testhelper.unittest.AbstractUnitTestAnnotationProcessorClass; +import io.toolisticon.annotationprocessortoolkit.testhelper.unittest.AnnotationProcessorUnitTestConfiguration; +import io.toolisticon.annotationprocessortoolkit.testhelper.unittest.AnnotationProcessorUnitTestConfigurationBuilder; +import io.toolisticon.annotationprocessortoolkit.tools.ElementUtils; +import io.toolisticon.annotationprocessortoolkit.tools.Utilities; +import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; +import java.util.Arrays; +import java.util.List; + + + + +/** + * Unit test for {@link ByParameterTypeFqnMatcher}. + */ +@RunWith(Parameterized.class) +public class ByParameterTypeFqnMatcherTest extends AbstractAnnotationProcessorUnitTest { + + public ByParameterTypeFqnMatcherTest(String message, AnnotationProcessorUnitTestConfiguration configuration) { + super(configuration); + } + + @Parameterized.Parameters(name = "{index}: {0}") + public static List data() { + return Arrays.asList( + + new Object[][]{ + + + { + "ByParameterTypeMatcher match", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + // find field + List result = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "methodWithReturnTypeAndParameters"); + MatcherAssert.assertThat("Precondition: should have found one method", result.size() == 1); + MatcherAssert.assertThat("Precondition: dound method has to be of zype ExecutableElement", result.get(0) instanceof ExecutableElement); + + ExecutableElement executableElement = ElementUtils.CastElement.castElementList(result, ExecutableElement.class).get(0); + MatcherAssert.assertThat("Precondition: method must have 2 parameters", executableElement.getParameters().size() == 2); + MatcherAssert.assertThat("Precondition: first parameter must be of type Boolean but is " + executableElement.getParameters().get(0).asType().toString(), executableElement.getParameters().get(0).asType().toString().equals(Boolean.class.getCanonicalName())); + MatcherAssert.assertThat("Precondition: second parameter must be of type String but is " + executableElement.getParameters().get(1).asType().toString(), executableElement.getParameters().get(1).asType().toString().equals(String.class.getCanonicalName())); + + + MatcherAssert.assertThat("Should have found matching parameters", CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(executableElement, Utilities.convertVarargsToArray(Boolean.class.getCanonicalName(), String.class.getCanonicalName()))); + + } + } + ) + .build() + + }, + { + "ByParameterTypeMatcher no match", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + List result = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "methodWithReturnTypeAndParameters"); + MatcherAssert.assertThat("Precondition: should have found one method", result.size() == 1); + MatcherAssert.assertThat("Precondition: dound method has to be of zype ExecutableElement", result.get(0) instanceof ExecutableElement); + + ExecutableElement executableElement = ElementUtils.CastElement.castElementList(result, ExecutableElement.class).get(0); + MatcherAssert.assertThat("Precondition: method must have 2 parameters", executableElement.getParameters().size() == 2); + + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(executableElement, Utilities.convertVarargsToArray(String.class.getCanonicalName(), Boolean.class.getCanonicalName()))); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(executableElement, Utilities.convertVarargsToArray(Boolean.class.getCanonicalName()))); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(executableElement, Utilities.convertVarargsToArray(Boolean.class.getCanonicalName(), String.class.getCanonicalName(), String.class.getCanonicalName()))); + + } + } + ) + .build() + + + }, + + { + "ByParameterTypeMatcher null values", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + List result = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "methodWithReturnTypeAndParameters"); + MatcherAssert.assertThat("Precondition: should have found one method", result.size() == 1); + MatcherAssert.assertThat("Precondition: dound method has to be of zype ExecutableElement", result.get(0) instanceof ExecutableElement); + + ExecutableElement executableElement = ElementUtils.CastElement.castElementList(result, ExecutableElement.class).get(0); + MatcherAssert.assertThat("Precondition: method must have 2 parameters", executableElement.getParameters().size() == 2); + + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(null, Utilities.convertVarargsToArray(String.class.getCanonicalName(), Boolean.class.getCanonicalName()))); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(executableElement, null)); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().checkForMatchingCharacteristic(null, null)); + + } + } + ) + .build() + + + }, + + { + "getStringRepresentationOfPassedCharacteristic null", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + + MatcherAssert.assertThat("Should not have found matching parameters", CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().getStringRepresentationOfPassedCharacteristic(null), Matchers.nullValue()); + + } + } + ) + .build() + + + }, + + { + "getStringRepresentationOfPassedCharacteristic get String representation", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + MatcherAssert.assertThat("Should have created valid string representation", CoreMatchers.BY_PARAMETER_TYPE_FQN.getMatcher().getStringRepresentationOfPassedCharacteristic(Utilities.convertVarargsToArray(String.class.getCanonicalName(), Boolean.class.getCanonicalName())), Matchers.is("[java.lang.String, java.lang.Boolean]")); + + + } + } + ) + .build() + + + }, + + } + + ); + + + } + + @Test + public void test() { + super.test(); + } +} diff --git a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcherTest.java b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcherTest.java index 2d1be580..73289928 100644 --- a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcherTest.java +++ b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/ByParameterTypeMatcherTest.java @@ -8,6 +8,7 @@ import io.toolisticon.annotationprocessortoolkit.tools.Utilities; import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -89,6 +90,71 @@ protected void testCase(TypeElement element) { }, + { + "ByParameterTypeMatcher null values", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + List result = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "methodWithReturnTypeAndParameters"); + MatcherAssert.assertThat("Precondition: should have found one method", result.size() == 1); + MatcherAssert.assertThat("Precondition: dound method has to be of zype ExecutableElement", result.get(0) instanceof ExecutableElement); + + ExecutableElement executableElement = ElementUtils.CastElement.castElementList(result, ExecutableElement.class).get(0); + MatcherAssert.assertThat("Precondition: method must have 2 parameters", executableElement.getParameters().size() == 2); + + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE.getMatcher().checkForMatchingCharacteristic(null, Utilities.convertVarargsToArray(String.class, Boolean.class))); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE.getMatcher().checkForMatchingCharacteristic(executableElement, null)); + MatcherAssert.assertThat("Should not have found matching parameters", !CoreMatchers.BY_PARAMETER_TYPE.getMatcher().checkForMatchingCharacteristic(null, null)); + + } + } + ) + .build() + + + }, + + { + "getStringRepresentationOfPassedCharacteristic null", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + + MatcherAssert.assertThat("Should not have found matching parameters", CoreMatchers.BY_PARAMETER_TYPE.getMatcher().getStringRepresentationOfPassedCharacteristic(null), Matchers.nullValue()); + + } + } + ) + .build() + + + }, + + { + "getStringRepresentationOfPassedCharacteristic get String representation", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + MatcherAssert.assertThat("Should have created valid string representation", CoreMatchers.BY_PARAMETER_TYPE.getMatcher().getStringRepresentationOfPassedCharacteristic(Utilities.convertVarargsToArray(String.class, Boolean.class)), Matchers.is("[java.lang.String, java.lang.Boolean]")); + + + } + } + ) + .build() + + + }, + } ); diff --git a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/HasVoidReturnTypeMatcherTest.java b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/HasVoidReturnTypeMatcherTest.java new file mode 100644 index 00000000..5682c6ab --- /dev/null +++ b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/HasVoidReturnTypeMatcherTest.java @@ -0,0 +1,49 @@ +package io.toolisticon.annotationprocessortoolkit.tools.matcher.impl; + +import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; + +/** + * Unit Test for {@link HasVoidReturnTypeMatcher}. + */ +public class HasVoidReturnTypeMatcherTest { + + @Test + public void test_check_withNullValue() { + + MatcherAssert.assertThat(CoreMatchers.HAS_VOID_RETURN_TYPE.getMatcher().check(null), Matchers.is(false)); + + } + + @Test + public void test_check_withVoidReturnType() { + + ExecutableElement element = Mockito.mock(ExecutableElement.class); + TypeMirror typeMirror = Mockito.mock(TypeMirror.class); + Mockito.when(element.getReturnType()).thenReturn(typeMirror); + Mockito.when(typeMirror.getKind()).thenReturn(TypeKind.VOID); + + MatcherAssert.assertThat(CoreMatchers.HAS_VOID_RETURN_TYPE.getMatcher().check(element), Matchers.is(true)); + + } + + @Test + public void test_check_withNonVoidReturnType() { + + ExecutableElement element = Mockito.mock(ExecutableElement.class); + TypeMirror typeMirror = Mockito.mock(TypeMirror.class); + Mockito.when(element.getReturnType()).thenReturn(typeMirror); + Mockito.when(typeMirror.getKind()).thenReturn(TypeKind.ARRAY); + + MatcherAssert.assertThat(CoreMatchers.HAS_VOID_RETURN_TYPE.getMatcher().check(element), Matchers.is(false)); + + } + +} diff --git a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/IsAssignableToMatcherTest.java b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/IsAssignableToMatcherTest.java index dc62d0bc..bcaf2985 100644 --- a/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/IsAssignableToMatcherTest.java +++ b/annotationprocessor/src/test/java/io/toolisticon/annotationprocessortoolkit/tools/matcher/impl/IsAssignableToMatcherTest.java @@ -7,6 +7,7 @@ import io.toolisticon.annotationprocessortoolkit.tools.TypeUtils; import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -97,6 +98,44 @@ protected void testCase(TypeElement element) { }, + { + "getStringRepresentationOfPassedCharacteristic null", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + + MatcherAssert.assertThat("Should not have found matching parameters", CoreMatchers.IS_ASSIGNABLE_TO.getMatcher().getStringRepresentationOfPassedCharacteristic(null), Matchers.nullValue()); + + } + } + ) + .build() + + + }, + + { + "getStringRepresentationOfPassedCharacteristic get String representation", + AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig() + .compilationShouldSucceed() + .setProcessor(new AbstractUnitTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + MatcherAssert.assertThat("Should have created valid string representation", CoreMatchers.IS_ASSIGNABLE_TO.getMatcher().getStringRepresentationOfPassedCharacteristic(String.class), Matchers.is("java.lang.String")); + + + } + } + ) + .build() + + + }, + }