Skip to content

Commit

Permalink
[#2] Added testcases to enhance test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Jun 30, 2017
1 parent 35491db commit 1944a86
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,8 @@ public boolean hasNoneOf(Element element, T... characteristicsToCheck) {
return true;
}

public GenericElementCharacteristicMatcher<T> getMatcher() {
return matcher;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public GenericElementCharacteristicValidator<T> getValidator() {
return validator;
}




/**
* Convenience method for getting and using a Modifier matching validator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));

}


Expand Down Expand Up @@ -166,6 +175,11 @@ public void castClassTest_MustThrowErrorForVariableElement() {

}

@Test
public void castClassTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castClass(null), Matchers.nullValue());
}

// ---------------------------------------
// castInterface -------------------------
// ---------------------------------------
Expand Down Expand Up @@ -194,6 +208,11 @@ public void castInterfaceTest_MustThrowErrorForVariableElement() {

}

@Test
public void castInterfaceTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castInterface(null), Matchers.nullValue());
}

// ---------------------------------------
// castEnum -----------------------------
// ---------------------------------------
Expand Down Expand Up @@ -222,6 +241,11 @@ public void castEnumTest_MustThrowErrorForVariableElement() {

}

@Test
public void castEnumTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castEnum(null), Matchers.nullValue());
}

// ---------------------------------------
// castParameter ------------------------
// ---------------------------------------
Expand Down Expand Up @@ -250,6 +274,11 @@ public void castParameterTest_MustThrowErrorForTypeElement() {

}

@Test
public void castParameterTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castParameter(null), Matchers.nullValue());
}

// ---------------------------------------
// castField ----------------------------
// ---------------------------------------
Expand Down Expand Up @@ -278,6 +307,11 @@ public void castFieldTest_MustThrowErrorForTypeElement() {

}

@Test
public void castFieldTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castField(null), Matchers.nullValue());
}

// ---------------------------------------
// castConstructor ----------------------
// ---------------------------------------
Expand Down Expand Up @@ -306,6 +340,11 @@ public void castConstructorTest_MustThrowErrorForTypeElement() {

}

@Test
public void castConstructorTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castConstructor(null), Matchers.nullValue());
}

// ---------------------------------------
// castMethod ---------------------------
// ---------------------------------------
Expand Down Expand Up @@ -334,4 +373,9 @@ public void castMethodTest_MustThrowErrorForTypeElement() {

}

@Test
public void castMethodTest_shouldHandleNullValuedParameterCorrectly() {
MatcherAssert.assertThat(ElementUtils.CastElement.castMethod(null), Matchers.nullValue());
}

}
Original file line number Diff line number Diff line change
@@ -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<Modifier>) 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<Class<? extends Annotation>>) 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<String>) 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<String>) 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<ElementKind>) 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));
}


}

0 comments on commit 1944a86

Please sign in to comment.