Skip to content

Commit

Permalink
[#18] Add unit tests for internal fluent validator implementation cla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
tobiasstamann committed Feb 26, 2018
1 parent a04fbff commit 08c8b06
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface FilterTestAnnotation1 {
String value() default "YES";
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.toolisticon.annotationprocessortoolkit.tools;
package io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator;

import com.google.testing.compile.JavaFileObjects;
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.corematcher.CoreMatcherValidationMessages;
import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatchers;
import io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator.FluentElementValidator;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -23,7 +23,7 @@


/**
* Unit test for {@link io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator.FluentElementValidator}.
* Unit test for {@link FluentElementValidator}.
*/
@RunWith(Parameterized.class)
public class FluentElementValidatorTest extends AbstractAnnotationProcessorUnitTest {
Expand Down Expand Up @@ -1097,7 +1097,7 @@ protected void testCase(TypeElement element) {
@Override
protected void testCase(TypeElement element) {

List<? extends Element> methods = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "synchronizedMethod" );
List<? extends Element> methods = ElementUtils.AccessEnclosedElements.getEnclosedElementsByName(element, "synchronizedMethod");
MatcherAssert.assertThat("Precondition : must have found exactly one element", methods.size() == 1);
MatcherAssert.assertThat("Precondition : element must be method", ElementUtils.CheckKindOfElement.isMethod(methods.get(0)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator.impl;

import com.google.testing.compile.JavaFileObjects;
import io.toolisticon.annotationprocessortoolkit.FilterTestAnnotation1;
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.AnnotationUtils;
import io.toolisticon.annotationprocessortoolkit.tools.ElementUtils;
import io.toolisticon.annotationprocessortoolkit.tools.corematcher.CoreMatcherValidationMessages;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.util.Arrays;
import java.util.List;


/**
* Unit test for {@link FluentValidatorMessage}.
*/
@RunWith(Parameterized.class)
public class FluentValidatorMessageTest extends AbstractAnnotationProcessorUnitTest {

public FluentValidatorMessageTest(String message, AnnotationProcessorUnitTestConfiguration configuration) {
super(configuration);
}

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

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

new Object[][]{


{
"Check error element message",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldFail()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

FluentValidatorMessage message = new FluentValidatorMessage(element, Diagnostic.Kind.ERROR, "TEST ${0}", "SUCCESS");
message.issueMessage();

}
}
)
.addMessageValidator()
.setErrorChecks("TEST SUCCESS")
.finishMessageValidator()
.build()


},
{
"Check warning element message",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldSucceed()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

FluentValidatorMessage message = new FluentValidatorMessage(element, Diagnostic.Kind.WARNING, "TEST ${0}", "SUCCESS");
message.issueMessage();

}
}
)
.addMessageValidator()
.setWarningChecks("TEST SUCCESS")
.finishMessageValidator()
.build()


},
{
"Check info element message",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldSucceed()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

FluentValidatorMessage message = new FluentValidatorMessage(element, Diagnostic.Kind.NOTE, "TEST ${0}", "SUCCESS");
message.issueMessage();

}
}
)
.addMessageValidator()
.setNoteChecks("TEST SUCCESS")
.finishMessageValidator()
.build()


},
{
"Check element with AnnotationMirror",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldSucceed()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

List<? extends Element> annotatedElement = ElementUtils.AccessEnclosedElements.getEnclosedElementsWithAllAnnotationsOf(element, FilterTestAnnotation1.class);
MatcherAssert.assertThat("PRECONDITION : Must have found one element", annotatedElement.size() == 1);
AnnotationMirror annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement.get(0), FilterTestAnnotation1.class);
MatcherAssert.assertThat("PRECONDITION : Must have found one annotation", annotationMirror, Matchers.notNullValue());


FluentValidatorMessage message = new FluentValidatorMessage(annotatedElement.get(0),annotationMirror, Diagnostic.Kind.NOTE, "TEST ${0}", "SUCCESS");
message.issueMessage();


}
}
)
.build()


},
{
"Check element with AnnotationMirror and AnnotationValue",
AnnotationProcessorUnitTestConfigurationBuilder.createTestConfig()
.compilationShouldSucceed()
.setProcessor(new AbstractUnitTestAnnotationProcessorClass() {
@Override
protected void testCase(TypeElement element) {

List<? extends Element> annotatedElement = ElementUtils.AccessEnclosedElements.getEnclosedElementsWithAllAnnotationsOf(element, FilterTestAnnotation1.class);
MatcherAssert.assertThat("PRECONDITION : Must have found one element", annotatedElement.size() == 1);
AnnotationMirror annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement.get(0), FilterTestAnnotation1.class);
MatcherAssert.assertThat("PRECONDITION : Must have found one annotation", annotationMirror, Matchers.notNullValue());
MatcherAssert.assertThat("PRECONDITION : Must have found one annotation", annotationMirror.getElementValues().size() == 1);

FluentValidatorMessage message = new FluentValidatorMessage(element, annotatedElement.get(0).getAnnotationMirrors().get(0), annotationMirror.getElementValues().get(0), Diagnostic.Kind.NOTE, "TEST ${0}", "SUCCESS");
message.issueMessage();

}
}
)
.build()


},


}

);


}


@Override
protected JavaFileObject getSourceFileForCompilation() {
return JavaFileObjects.forResource("AnnotationProcessorTestClass.java");
}

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

public static <T> List<T> convertToList(T... element) {

return Arrays.asList(element);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.toolisticon.annotationprocessortoolkit.tools.fluentvalidator.impl;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit test for {@link FluentValidatorState}
*/
public class FluentValidatorStateTest {


@Test
public void testSetFailingResult() {

FluentValidatorState unit = new FluentValidatorState();

MatcherAssert.assertThat(unit.getValidationResult(), Matchers.is(true));

unit.setAsFailedValidation();
MatcherAssert.assertThat(unit.getValidationResult(), Matchers.is(false));

}

@Test
public void testAddAndIssueMessages() {
FluentValidatorMessage fluentValidatorMessage1 = Mockito.mock(FluentValidatorMessage.class);
FluentValidatorMessage fluentValidatorMessage2 = Mockito.mock(FluentValidatorMessage.class);

FluentValidatorState unit = new FluentValidatorState();
unit.addMessage(fluentValidatorMessage1);
unit.addMessage(fluentValidatorMessage2);

unit.issueMessages();
Mockito.verify(fluentValidatorMessage1, Mockito.times(1)).issueMessage();
Mockito.verify(fluentValidatorMessage2, Mockito.times(1)).issueMessage();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public AnnotationProcessorTestClass(String withParameter) {

}

@FilterTestAnnotation1
@FilterTestAnnotation1(value="YES")
@FilterTestAnnotation2
public synchronized void synchronizedMethod() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static class MessageEvaluation {
private final BaseConfigurationBuilder baseConfigurationBuilder;
private String[] warningChecks;
private String[] errorChecks;
private String[] infoChecks;
private String[] noteChecks;


MessageEvaluation(BaseConfigurationBuilder baseConfigurationBuilder) {
Expand All @@ -84,16 +84,16 @@ public MessageEvaluation setErrorChecks(String... errorChecksToSet) {
return this;
}

public MessageEvaluation setInfoChecks(String... infoChecksToSet) {
this.infoChecks = infoChecksToSet;
public MessageEvaluation setNoteChecks(String... noteChecksToSet) {
this.noteChecks = noteChecksToSet;
return this;
}

public BaseConfigurationBuilder finishMessageValidator() {
baseConfigurationBuilder.testMessageValidator = new TestMessageValidator(
errorChecks != null ? errorChecks : new String[0],
warningChecks != null ? warningChecks : new String[0],
infoChecks != null ? infoChecks : new String[0]
noteChecks != null ? noteChecks : new String[0]
);
return baseConfigurationBuilder;
}
Expand Down

0 comments on commit 08c8b06

Please sign in to comment.