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()");
}
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);
}
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();
}
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);
}
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);
}
Aggregations