Skip to content

Commit

Permalink
[#18] Added unit and integration tests for example
Browse files Browse the repository at this point in the history
Did some refactorings like renaming too.
  • Loading branch information
tobiasstamann committed Apr 20, 2018
1 parent 10d38fb commit 844a796
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.toolisticon.annotationprocessortoolkit.tools.TypeUtils;
import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers;
import io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator.FluentElementValidator;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SomeInterface;
import io.toolisticon.example.annotationprocessortoolkit.annotations.TypeThatIsAssignableToInterfaceAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SpecificInterface;
import io.toolisticon.spiap.api.Service;

import javax.annotation.processing.Processor;
Expand All @@ -19,14 +19,14 @@
/**
* Test annotation processor which demonstrates the usage of the annotation processor toolkit.
*/
@SupportedAnnotationTypes("io.toolisticon.example.annotationprocessortoolkit.annotations.TypeThatIsAssignableToInterfaceAnnotation")
@SupportedAnnotationTypes("io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation")
@Service(Processor.class)
public class TypeThatIsAssignableToInterfaceAnnotationProcessor extends AbstractAnnotationProcessor {
public class ImplementsSpecificInterfaceCheckAnnotationProcessor extends AbstractAnnotationProcessor {

// Overriding the getSupportedAnnotationTypes instead of using the SupportedAnnotationTypes annotation
// might be an option this is especially useful if you have inheritance
// private final static Set<String> SUPPORTED_ANNOTATION_TYPES =
// createSupportedAnnotationSet(TypeThatIsAssignableToInterfaceAnnotation.class);
// createSupportedAnnotationSet(ImplementsSpecificInterfaceCheckAnnotation.class);
// @Override
// public Set<String> getSupportedAnnotationTypes() {
// return SUPPORTED_ANNOTATION_TYPES;
Expand All @@ -35,12 +35,12 @@ public class TypeThatIsAssignableToInterfaceAnnotationProcessor extends Abstract
@Override
public boolean processAnnotations(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

for (Element element : roundEnv.getElementsAnnotatedWith(TypeThatIsAssignableToInterfaceAnnotation.class)) {
for (Element element : roundEnv.getElementsAnnotatedWith(ImplementsSpecificInterfaceCheckAnnotation.class)) {


// validator already will print output so additional actions are not necessary
FluentElementValidator.createFluentElementValidator(ElementUtils.CastElement.castToTypeElement(element))
.applyValidator(CoreMatchers.IS_ASSIGNABLE_TO).hasOneOf(SomeInterface.class)
.applyValidator(CoreMatchers.IS_ASSIGNABLE_TO).hasOneOf(SpecificInterface.class)
.validateAndIssueMessages();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@SupportedAnnotationTypes(
"io.toolisticon.example.annotationprocessortoolkit.annotations.MethodWithOneStringParameterAndVoidReturnTypeAnnotation")
@Service(Processor.class)
public class MethodWithOneStringParameterAndVoidReturnTypeAnnotationProcessor extends AbstractAnnotationProcessor {
public class MethodHasStringParameterAndVoidReturnTypeCheckAnnotationProcessor extends AbstractAnnotationProcessor {


// Overriding the getSupportedAnnotationTypes instead of using the SupportedAnnotationTypes annotation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.toolisticon.example.annotationprocessortoolkit.annotationprocessor;

import io.toolisticon.annotationprocessortoolkit.testhelper.AbstractAnnotationProcessorIntegrationTest;
import io.toolisticon.annotationprocessortoolkit.testhelper.integrationtest.AnnotationProcessorIntegrationTestConfiguration;
import io.toolisticon.annotationprocessortoolkit.testhelper.integrationtest.AnnotationProcessorIntegrationTestConfigurationBuilder;
import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatcherValidationMessages;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;

/**
* Integration test for {@link ImplementsSpecificInterfaceCheckAnnotationProcessor}.
*/
@RunWith(Parameterized.class)
public class ImplementsSpecificInterfaceCheckAnnotationProcessorTest extends AbstractAnnotationProcessorIntegrationTest<ImplementsSpecificInterfaceCheckAnnotationProcessor> {

public ImplementsSpecificInterfaceCheckAnnotationProcessorTest(String description, AnnotationProcessorIntegrationTestConfiguration annotationProcessorIntegrationTestConfiguration) {
super(annotationProcessorIntegrationTestConfiguration);
}

@Override
protected ImplementsSpecificInterfaceCheckAnnotationProcessor getAnnotationProcessor() {
return new ImplementsSpecificInterfaceCheckAnnotationProcessor();
}

@Before
public void init() {
CoreMatcherValidationMessages.setPrintMessageCodes(true);
}

@Parameterized.Parameters(name = "{index}: \"{0}\"")
public static List<Object[]> data() {

return Arrays.asList(new Object[][]{
{
"Test valid usage : implements",
AnnotationProcessorIntegrationTestConfigurationBuilder.createTestConfig()
.setSourceFileToCompile("testcases/implementsSpecificInterfaceCheckAnnotationProcessor/ValidUsageTest.java")
.compilationShouldSucceed()
.build()
},
{
"Test invalid usage : extends ",
AnnotationProcessorIntegrationTestConfigurationBuilder.createTestConfig()
.setSourceFileToCompile("testcases/implementsSpecificInterfaceCheckAnnotationProcessor/ValidUsageTestExtendsCase.java")
.compilationShouldSucceed()
.build()
},
{
"Test invalid usage : non String parameter",
AnnotationProcessorIntegrationTestConfigurationBuilder.createTestConfig()
.setSourceFileToCompile("testcases/implementsSpecificInterfaceCheckAnnotationProcessor/InvalidUsageTest.java")
.compilationShouldFail()
.addMessageValidator()
.setErrorChecks(CoreMatcherValidationMessages.IS_ASSIGNABLE_TO.getCode())
.finishMessageValidator()
.build()
},


});

}


@Test
public void test() {
super.test();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.List;

@RunWith(Parameterized.class)
public class TypeThatIsAssignableToInterfaceAnnotationProcessorUnitTest extends AbstractAnnotationProcessorUnitTest {
public class ImplementsSpecificInterfaceCheckAnnotationProcessorUnitTest extends AbstractAnnotationProcessorUnitTest {

public TypeThatIsAssignableToInterfaceAnnotationProcessorUnitTest(String description, AnnotationProcessorUnitTestConfiguration configuration) {
public ImplementsSpecificInterfaceCheckAnnotationProcessorUnitTest(String description, AnnotationProcessorUnitTestConfiguration configuration) {
super(configuration);
}

Expand All @@ -32,7 +32,7 @@ public static List<Object[]> data() {
@Override
protected void testCase(TypeElement element) {

TypeThatIsAssignableToInterfaceAnnotationProcessor unit = new TypeThatIsAssignableToInterfaceAnnotationProcessor();
ImplementsSpecificInterfaceCheckAnnotationProcessor unit = new ImplementsSpecificInterfaceCheckAnnotationProcessor();
unit.init(this.processingEnv);

MatcherAssert.assertThat("Should be assignable to Object", unit.isAssignableTo(element, Object.class.getCanonicalName()));
Expand All @@ -49,7 +49,7 @@ protected void testCase(TypeElement element) {
@Override
protected void testCase(TypeElement element) {

TypeThatIsAssignableToInterfaceAnnotationProcessor unit = new TypeThatIsAssignableToInterfaceAnnotationProcessor();
ImplementsSpecificInterfaceCheckAnnotationProcessor unit = new ImplementsSpecificInterfaceCheckAnnotationProcessor();
unit.init(this.processingEnv);

MatcherAssert.assertThat("Should not be assignable to String", !unit.isAssignableTo(element, String.class.getCanonicalName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@


@RunWith(Parameterized.class)
public class MethodWithOneStringParameterAndVoidReturnTypeProcessorTest extends AbstractAnnotationProcessorIntegrationTest<MethodWithOneStringParameterAndVoidReturnTypeAnnotationProcessor> {
public class MethodWithOneStringParameterAndVoidReturnTypeProcessorTest extends AbstractAnnotationProcessorIntegrationTest<MethodHasStringParameterAndVoidReturnTypeCheckAnnotationProcessor> {


public MethodWithOneStringParameterAndVoidReturnTypeProcessorTest(String description, AnnotationProcessorIntegrationTestConfiguration annotationProcessorIntegrationTestConfiguration) {
super(annotationProcessorIntegrationTestConfiguration);
}

@Override
protected MethodWithOneStringParameterAndVoidReturnTypeAnnotationProcessor getAnnotationProcessor() {
return new MethodWithOneStringParameterAndVoidReturnTypeAnnotationProcessor();
protected MethodHasStringParameterAndVoidReturnTypeCheckAnnotationProcessor getAnnotationProcessor() {
return new MethodHasStringParameterAndVoidReturnTypeCheckAnnotationProcessor();
}

@Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.toolisticon.example.annotationprocessortoolkit;

import io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.MethodWithOneStringParameterAndVoidReturnTypeAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SpecificInterface;

@ImplementsSpecificInterfaceCheckAnnotation
public class InvalidUsageTest {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.toolisticon.example.annotationprocessortoolkit;

import io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.MethodWithOneStringParameterAndVoidReturnTypeAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SpecificInterface;

@ImplementsSpecificInterfaceCheckAnnotation
public class ValidUsageTest implements SpecificInterface{


public String testMethod() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.toolisticon.example.annotationprocessortoolkit;

import io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SpecificInterface;


public class ValidUsageTestExtendsCase {

public static class SuperClass implements SpecificInterface {

public String testMethod() {
return null;
}

}


@ImplementsSpecificInterfaceCheckAnnotation
public static class SubClass extends SuperClass {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.lang.annotation.Target;

/**
* Test annotation for annotating classes that must implement a the {@link SomeInterface} interface.
* Test annotation for annotating classes that must implement a the {@link SpecificInterface} interface.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeThatIsAssignableToInterfaceAnnotation {
public @interface ImplementsSpecificInterfaceCheckAnnotation {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.toolisticon.example.annotationprocessortoolkit.annotations;


public interface SomeInterface {
public interface SpecificInterface {
String testMethod();
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.toolisticon.example.annotationprocessortoolkit.usecase;

import io.toolisticon.example.annotationprocessortoolkit.annotations.MethodWithOneStringParameterAndVoidReturnTypeAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SomeInterface;
import io.toolisticon.example.annotationprocessortoolkit.annotations.TypeThatIsAssignableToInterfaceAnnotation;
import io.toolisticon.example.annotationprocessortoolkit.annotations.SpecificInterface;
import io.toolisticon.example.annotationprocessortoolkit.annotations.ImplementsSpecificInterfaceCheckAnnotation;

/**
* Test class to show that the annotation processors are working correctly.
*/


@TypeThatIsAssignableToInterfaceAnnotation
public class TestClass implements SomeInterface {
@ImplementsSpecificInterfaceCheckAnnotation
public class TestClass implements SpecificInterface {

@MethodWithOneStringParameterAndVoidReturnTypeAnnotation
public void testMethod(String a) {
Expand Down

0 comments on commit 844a796

Please sign in to comment.