Skip to content

Commit

Permalink
[#18] Add unit tests for CoreMatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Feb 23, 2018
1 parent 8fd9b8e commit cfb342e
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Object[]> data() {
return Arrays.asList(

new Object[][]{


{
"ByParameterTypeMatcher match",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldSucceed()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

// find field
List<? extends Element> 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<? extends Element> 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<? extends Element> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends Element> 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()


},

}

);
Expand Down
Loading

0 comments on commit cfb342e

Please sign in to comment.