Search in sources :

Example 36 with JavaMethod

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

the class ClassFileImporterAccessesTest method resolve.

// only temporary to make sure resolveMember() and resolve() are in sync. Inline again when we throw out resolve()
private JavaMethod resolve(MethodCallTarget target) {
    JavaMethod method = target.resolveMember().get();
    assertThat(target.resolve()).containsExactly(method);
    return method;
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod)

Example 37 with JavaMethod

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

the class ClassFileImporterAccessesTest method imports_own_set_field_access.

@Test
public void imports_own_set_field_access() {
    JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
    JavaMethod setStringValue = classWithOwnFieldAccess.getMethod("setStringValue", String.class);
    JavaFieldAccess access = getOnlyElement(setStringValue.getFieldAccesses());
    assertThatAccess(access).isOfType(SET).isFrom(setStringValue).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(12);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Example 38 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod 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 39 with JavaMethod

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

the class ClassFileImporterAnnotationsTest method methods_handle_optional_annotation_correctly.

@Test
public void methods_handle_optional_annotation_correctly() {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAnnotatedMethod);
    assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class)).isPresent();
    assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class)).isEmpty();
    Optional<JavaAnnotation<JavaMethod>> optionalAnnotation = method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
    assertThat(optionalAnnotation.get().getOwner()).as("owner of optional annotation").isEqualTo(method);
    assertThat(method.tryGetAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithEnumAndArrayValue.class.getName())).isEmpty();
}
Also used : ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 40 with JavaMethod

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

the class ClassFileImporterAnnotationsTest method imports_methods_with_one_annotation_correctly.

@Test
public void imports_methods_with_one_annotation_correctly() throws Exception {
    JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAnnotatedMethod);
    JavaAnnotation<JavaMethod> annotation = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
    assertThatType(annotation.getRawType()).matches(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class);
    assertThat(annotation.getOwner()).isEqualTo(method);
    assertThat(annotation.get("value").get()).isEqualTo("something");
    assertThat(method).isEquivalentTo(ClassWithAnnotatedMethods.class.getMethod(stringAnnotatedMethod));
}
Also used : ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Aggregations

JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)56 JavaClass (com.tngtech.archunit.core.domain.JavaClass)36 Test (org.junit.Test)32 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)22 ArchCondition (com.tngtech.archunit.lang.ArchCondition)21 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 List (java.util.List)12 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)10 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)9 Collectors (java.util.stream.Collectors)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 Stream (java.util.stream.Stream)8 Collections (java.util.Collections)7 JavaField (com.tngtech.archunit.core.domain.JavaField)6 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)5 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)5 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)5