use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_try_catch_block_without_caught_throwables.
@Test
public void imports_try_catch_block_without_caught_throwables() {
JavaClass javaClass = new ClassFileImporter().importClass(ClassWithTryCatchBlockWithoutThrowables.class);
JavaMethod method = javaClass.getMethod("method");
Set<TryCatchBlock> tryCatchBlocks = method.getTryCatchBlocks();
assertThat(tryCatchBlocks).hasSize(1).areExactly(1, tryCatchBlock().declaredIn(method).catchingNoThrowables().atLocation(ClassWithTryCatchBlockWithoutThrowables.class, 7));
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_on_self.
@Test
public void imports_method_calls_on_self() {
JavaClass classThatCallsOwnMethod = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnMethod.class);
JavaMethodCall call = getOnlyElement(classThatCallsOwnMethod.getMethodCallsFromSelf());
assertThatCall(call).isFrom(classThatCallsOwnMethod.getCodeUnitWithParameterTypes("getString")).isTo(classThatCallsOwnMethod.getMethod("string")).inLineNumber(6);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_other_static_field_accesses.
@Test
public void imports_other_static_field_accesses() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
JavaClass classWithOwnFieldAccess = classes.get(OwnStaticFieldAccess.class);
JavaClass classWithForeignFieldAccess = classes.get(ForeignStaticFieldAccess.class);
Set<JavaFieldAccess> accesses = classWithForeignFieldAccess.getFieldAccessesFromSelf();
assertThat(accesses).as("Number of field accesses from " + classWithForeignFieldAccess.getName()).hasSize(2);
assertThatAccess(getOnly(accesses, "staticStringValue", GET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getStaticStringFromOther")).isTo(classWithOwnFieldAccess.getField("staticStringValue")).inLineNumber(5);
assertThatAccess(getOnly(accesses, "staticStringValue", SET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setStaticStringFromOther")).isTo(classWithOwnFieldAccess.getField("staticStringValue")).inLineNumber(9);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method getDirectDependencies_does_not_return_transitive_dependencies.
@Test
public void getDirectDependencies_does_not_return_transitive_dependencies() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/integration"));
JavaClass javaClass = classes.get(ClassCDependingOnClassB_SuperclassOfX.class);
JavaClass expectedTargetClass = classes.get(ClassBDependingOnClassA.class);
Set<JavaClass> targetClasses = javaClass.getDirectDependenciesFromSelf().stream().map(Dependency::getTargetClass).filter(targetClass -> targetClass.getPackageName().contains("testexamples")).collect(toSet());
assertThat(targetClasses).containsOnly(expectedTargetClass);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_other_field_accesses_from_static_initializer.
@Test
public void imports_other_field_accesses_from_static_initializer() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
JavaClass fieldAccessFromInitializer = classes.get(ForeignFieldAccessFromStaticInitializer.class);
Set<JavaFieldAccess> accesses = fieldAccessFromInitializer.getFieldAccessesFromSelf();
assertThat(accesses).as("Number of field accesses from " + fieldAccessFromInitializer.getName()).hasSize(2);
assertThatAccess(getOnly(accesses, "stringValue", GET)).isFrom(fieldAccessFromInitializer.getCodeUnitWithParameterTypes(STATIC_INITIALIZER_NAME)).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(5);
assertThatAccess(getOnly(accesses, "intValue", SET)).isFrom(fieldAccessFromInitializer.getCodeUnitWithParameterTypes(STATIC_INITIALIZER_NAME)).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(6);
}
Aggregations