Search in sources :

Example 1 with JavaParameter

use of com.tngtech.archunit.core.domain.JavaParameter 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]));
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 2 with JavaParameter

use of com.tngtech.archunit.core.domain.JavaParameter in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_empty_parameter_annotations_for_method_without_annotated_parameters.

@Test
public void imports_empty_parameter_annotations_for_method_without_annotated_parameters() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithTwoUnannotatedParameters, String.class, int.class);
    for (JavaParameter parameter : method.getParameters()) {
        assertThat(parameter.getAnnotations()).isEmpty();
    }
    assertThat(method.getParameterAnnotations()).containsExactly(Collections.<JavaAnnotation<JavaParameter>>emptySet(), Collections.<JavaAnnotation<JavaParameter>>emptySet());
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) Test(org.junit.Test)

Example 3 with JavaParameter

use of com.tngtech.archunit.core.domain.JavaParameter in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_methods_with_multiple_parameters_with_annotations.

@Test
public void imports_methods_with_multiple_parameters_with_annotations() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithTwoAnnotatedParameters, String.class, int.class);
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
    List<JavaParameter> parameters = method.getParameters();
    for (int i = 0; i < 2; i++) {
        assertThat(parameterAnnotations.get(i)).isEqualTo(parameters.get(i).getAnnotations());
        assertThatAnnotations(parameterAnnotations.get(i)).match(ImmutableSet.copyOf(method.reflect().getParameterAnnotations()[i]));
    }
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) Test(org.junit.Test)

Example 4 with JavaParameter

use of com.tngtech.archunit.core.domain.JavaParameter in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_method_with_one_parameter_with_one_annotation.

@Test
public void imports_method_with_one_parameter_with_one_annotation() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithOneAnnotation, String.class);
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
    JavaParameter parameter = getOnlyElement(method.getParameters());
    JavaAnnotation<JavaParameter> annotation = getOnlyElement(getOnlyElement(parameterAnnotations));
    assertThat((JavaEnumConstant) annotation.get("value").get()).isEquivalentTo(OTHER_VALUE);
    assertThat((JavaEnumConstant) annotation.get("valueWithDefault").get()).isEquivalentTo(SOME_VALUE);
    assertThat(((JavaEnumConstant[]) annotation.get("enumArray").get())).matches(SOME_VALUE, OTHER_VALUE);
    assertThat(((JavaEnumConstant[]) annotation.get("enumArrayWithDefault").get())).matches(OTHER_VALUE);
    JavaAnnotation<?> subAnnotation = (JavaAnnotation<?>) annotation.get("subAnnotation").get();
    assertThat(subAnnotation.get("value").get()).isEqualTo("changed");
    assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
    assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(parameter);
    assertThat(((JavaAnnotation<?>) annotation.get("subAnnotationWithDefault").get()).get("value").get()).isEqualTo("default");
    JavaAnnotation<?>[] subAnnotationArray = (JavaAnnotation<?>[]) annotation.get("subAnnotationArray").get();
    assertThat(subAnnotationArray[0].get("value").get()).isEqualTo("one");
    assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[0].getAnnotatedElement()).isEqualTo(parameter);
    assertThat(subAnnotationArray[1].get("value").get()).isEqualTo("two");
    assertThat(subAnnotationArray[1].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotationArray[1].getAnnotatedElement()).isEqualTo(parameter);
    assertThat(((JavaAnnotation<?>[]) annotation.get("subAnnotationArrayWithDefault").get())[0].get("value").get()).isEqualTo("first");
    assertThatType((JavaClass) annotation.get("clazz").get()).matches(Map.class);
    assertThatType((JavaClass) annotation.get("clazzWithDefault").get()).matches(String.class);
    assertThat((JavaClass[]) annotation.get("classes").get()).matchExactly(Object.class, Serializable.class);
    assertThat((JavaClass[]) annotation.get("classesWithDefault").get()).matchExactly(Serializable.class, List.class);
    assertThatAnnotation(getOnlyElement(parameter.getAnnotations())).matches(method.reflect().getParameterAnnotations()[0][0]);
    assertThatAnnotation(annotation).matches(method.reflect().getParameterAnnotations()[0][0]);
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 5 with JavaParameter

use of com.tngtech.archunit.core.domain.JavaParameter in project ArchUnit by TNG.

the class ClassFileImporterAnnotationsTest method imports_parameter_annotations_correctly_for_synthetic_constructor_parameter.

@Test
public void imports_parameter_annotations_correctly_for_synthetic_constructor_parameter() {
    @SuppressWarnings("unused")
    class LocalClassThusSyntheticFirstConstructorParameter {

        LocalClassThusSyntheticFirstConstructorParameter(Object notAnnotated, @SimpleAnnotation("test") List<String> annotated) {
        }
    }
    JavaConstructor constructor = getOnlyElement(new ClassFileImporter().importClasses(LocalClassThusSyntheticFirstConstructorParameter.class, String.class).get(LocalClassThusSyntheticFirstConstructorParameter.class).getConstructors());
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = constructor.getParameterAnnotations();
    assertThat(parameterAnnotations).as("parameter annotations").hasSize(2);
    assertThatAnnotations(parameterAnnotations.get(0)).isEmpty();
    assertThatAnnotations(parameterAnnotations.get(1)).isNotEmpty();
    List<JavaParameter> parameters = constructor.getParameters();
    for (int i = 0; i < parameterAnnotations.size(); i++) {
        assertThat(parameterAnnotations.get(i)).isEqualTo(parameters.get(i).getAnnotations());
        assertThatAnnotations(parameterAnnotations.get(i)).match(ImmutableSet.copyOf(constructor.reflect().getParameterAnnotations()[i]));
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SimpleAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) List(java.util.List) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Aggregations

JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)6 Test (org.junit.Test)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)5 ClassWithMethodWithAnnotatedParameters (com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters)5 Set (java.util.Set)5 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)2 JavaClass (com.tngtech.archunit.core.domain.JavaClass)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 JavaEnumConstant (com.tngtech.archunit.core.domain.JavaEnumConstant)1 SimpleAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation)1 List (java.util.List)1