Search in sources :

Example 76 with JavaClass

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

the class ClassFileImporterAccessesTest method imports_method_calls_on_external_class.

@Test
public void imports_method_calls_on_external_class() {
    JavaClass classThatCallsExternalMethod = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsExternalMethod.class);
    JavaMethodCall call = getOnlyElement(classThatCallsExternalMethod.getMethodCallsFromSelf());
    assertThatCall(call).isFrom(classThatCallsExternalMethod.getCodeUnitWithParameterTypes("getString")).inLineNumber(7);
    MethodCallTarget target = call.getTarget();
    assertThat(target.getOwner().reflect()).isEqualTo(ArrayList.class);
    assertThat(target.getFullName()).isEqualTo(ArrayList.class.getName() + ".toString()");
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 77 with JavaClass

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

the class ClassFileImporterAccessesTest method imports_shadowed_and_superclass_method_calls.

@Test
public void imports_shadowed_and_superclass_method_calls() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/hierarchicalmethodcall"));
    JavaClass classThatCallsMethodOfSuperclass = classes.get(CallOfSuperAndSubclassMethod.class);
    JavaClass superclassWithCalledMethod = classes.get(SuperclassWithCalledMethod.class);
    JavaClass subClassWithCalledMethod = classes.get(SubclassWithCalledMethod.class);
    Set<JavaMethodCall> calls = classThatCallsMethodOfSuperclass.getMethodCallsFromSelf();
    assertThat(calls).hasSize(2);
    JavaCodeUnit callSuperclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSuperclassMethod);
    JavaMethod expectedSuperclassMethod = superclassWithCalledMethod.getMethod(SuperclassWithCalledMethod.method);
    MethodCallTarget expectedSuperclassCall = newMethodCallTargetBuilder().withOwner(subClassWithCalledMethod).withName(expectedSuperclassMethod.getName()).withParameters(expectedSuperclassMethod.getRawParameterTypes()).withReturnType(expectedSuperclassMethod.getRawReturnType()).withMember(() -> Optional.of(expectedSuperclassMethod)).build();
    assertThatCall(getOnlyByCaller(calls, callSuperclassMethod)).isFrom(callSuperclassMethod).isTo(expectedSuperclassCall).inLineNumber(CallOfSuperAndSubclassMethod.callSuperclassLineNumber);
    JavaCodeUnit callSubclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSubclassMethod);
    assertThatCall(getOnlyByCaller(calls, callSubclassMethod)).isFrom(callSubclassMethod).isTo(subClassWithCalledMethod.getMethod(SubclassWithCalledMethod.maskedMethod)).inLineNumber(CallOfSuperAndSubclassMethod.callSubclassLineNumber);
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 78 with JavaClass

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

the class ClassFileImporterAnnotationsTest method class_handles_optional_annotation_correctly.

@Test
public void class_handles_optional_annotation_correctly() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(ClassWithOneAnnotation.class).get(ClassWithOneAnnotation.class);
    assertThat(clazz.tryGetAnnotationOfType(SimpleAnnotation.class)).isPresent();
    assertThat(clazz.tryGetAnnotationOfType(Deprecated.class)).isEmpty();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithOneAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithOneAnnotation) Test(org.junit.Test)

Example 79 with JavaClass

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

the class ClassFileImporterAnnotationsTest method imports_class_with_one_annotation_correctly.

@Test
public void imports_class_with_one_annotation_correctly() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(ClassWithOneAnnotation.class).get(ClassWithOneAnnotation.class);
    JavaAnnotation<JavaClass> annotation = clazz.getAnnotationOfType(SimpleAnnotation.class.getName());
    assertThatType(annotation.getRawType()).matches(SimpleAnnotation.class);
    assertThatType(annotation.getOwner()).isEqualTo(clazz);
    JavaAnnotation<?> annotationByName = clazz.getAnnotationOfType(SimpleAnnotation.class.getName());
    assertThat(annotationByName).isEqualTo(annotation);
    assertThat(annotation.get("value").get()).isEqualTo("test");
    assertThatType(clazz).matches(ClassWithOneAnnotation.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithOneAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithOneAnnotation) SimpleAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation) Test(org.junit.Test)

Example 80 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass 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);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) TypeAnnotationWithEnumAndArrayValue(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.TypeAnnotationWithEnumAndArrayValue) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9