use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class JavaAnnotationsAssertion method match.
public JavaAnnotationsAssertion match(Collection<Annotation> annotations) {
assertThat(actual).hasSameSizeAs(annotations);
Map<String, JavaAnnotation<?>> actualByClassName = annotationsByClassName(actual);
Map<String, Annotation> reflectionByClassName = reflectionByClassName(annotations);
assertThat(actualByClassName.keySet()).as("annotation type names").containsExactlyInAnyOrderElementsOf(reflectionByClassName.keySet());
for (Map.Entry<String, JavaAnnotation<?>> entry : actualByClassName.entrySet()) {
Annotation reflectionAnnotation = reflectionByClassName.get(entry.getKey());
assertThatAnnotation(entry.getValue()).matches(reflectionAnnotation);
}
return myself;
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_methods_with_two_annotations_correctly.
@Test
public void imports_methods_with_two_annotations_correctly() throws Exception {
JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAndIntAnnotatedMethod);
Set<JavaAnnotation<JavaMethod>> annotations = method.getAnnotations();
assertThat(annotations).hasSize(2);
assertThat(annotations).extractingResultOf("getOwner").containsOnly(method);
assertThat(annotations).extractingResultOf("getAnnotatedElement").containsOnly(method);
JavaAnnotation<?> annotationWithString = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
assertThat(annotationWithString.get("value").get()).isEqualTo("otherThing");
JavaAnnotation<?> annotationWithInt = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithIntValue.class.getName());
assertThat(annotationWithInt.get("otherValue").get()).isEqualTo("overridden");
assertThat(method).isEquivalentTo(ClassWithAnnotatedMethods.class.getMethod(stringAndIntAnnotatedMethod));
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_annotation_defaults.
@Test
public void imports_annotation_defaults() {
JavaClass annotationType = new ClassFileImporter().importPackagesOf(TypeAnnotationWithEnumAndArrayValue.class).get(TypeAnnotationWithEnumAndArrayValue.class);
assertThat((JavaEnumConstant) annotationType.getMethod("valueWithDefault").getDefaultValue().get()).as("default of valueWithDefault()").isEquivalentTo(SOME_VALUE);
assertThat(((JavaEnumConstant[]) annotationType.getMethod("enumArrayWithDefault").getDefaultValue().get())).as("default of enumArrayWithDefault()").matches(OTHER_VALUE);
assertThat(((JavaAnnotation<?>) annotationType.getMethod("subAnnotationWithDefault").getDefaultValue().get()).get("value").get()).as("default of subAnnotationWithDefault()").isEqualTo("default");
assertThat(((JavaAnnotation<?>[]) annotationType.getMethod("subAnnotationArrayWithDefault").getDefaultValue().get())[0].get("value").get()).as("default of subAnnotationArrayWithDefault()").isEqualTo("first");
assertThatType((JavaClass) annotationType.getMethod("clazzWithDefault").getDefaultValue().get()).as("default of clazzWithDefault()").matches(String.class);
assertThat((JavaClass[]) annotationType.getMethod("classesWithDefault").getDefaultValue().get()).as("default of clazzWithDefault()").matchExactly(Serializable.class, List.class);
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method classes_know_which_annotation_members_have_their_type.
@Test
public void classes_know_which_annotation_members_have_their_type() {
@SuppressWarnings("unused")
@ParameterAnnotation(value = String.class)
class Dependent {
@ParameterAnnotation(value = String.class)
String field;
@ParameterAnnotation(value = String.class)
Dependent() {
}
@ParameterAnnotation(value = String.class)
void method() {
}
@ParameterAnnotation(value = List.class)
void notToFind() {
}
}
JavaClasses classes = new ClassFileImporter().importClasses(Dependent.class, ParameterAnnotation.class, String.class);
Set<JavaAnnotation<?>> annotations = classes.get(String.class).getAnnotationsWithParameterTypeOfSelf();
for (JavaAnnotation<?> annotation : annotations) {
assertThatAnnotation(annotation).hasType(ParameterAnnotation.class);
}
Set<JavaAnnotation<?>> expected = ImmutableSet.<JavaAnnotation<?>>of(classes.get(Dependent.class).getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getField("field").getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getConstructor(getClass()).getAnnotationOfType(ParameterAnnotation.class.getName()), classes.get(Dependent.class).getMethod("method").getAnnotationOfType(ParameterAnnotation.class.getName()));
assertThat(annotations).as("annotations with parameter type " + String.class.getSimpleName()).containsOnlyElementsOf(expected);
}
use of com.tngtech.archunit.core.domain.JavaAnnotation in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_method_with_one_parameter_with_two_annotations.
@Test
public void imports_method_with_one_parameter_with_two_annotations() {
JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithTwoAnnotations, String.class);
List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
Set<JavaAnnotation<JavaParameter>> annotations = getOnlyElement(parameterAnnotations);
assertThat(annotations).isEqualTo(getOnlyElement(method.getParameters()).getAnnotations());
assertThatAnnotations(annotations).match(ImmutableSet.copyOf(method.reflect().getParameterAnnotations()[0]));
}
Aggregations