use of com.tngtech.archunit.core.domain.JavaAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method classes_know_accesses_to_themselves.
@Test
public void classes_know_accesses_to_themselves() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/dependents"));
JavaClass classHoldingDependencies = classes.get(ClassHoldingDependencies.class);
JavaClass firstClassWithDependency = classes.get(FirstClassWithDependency.class);
JavaClass secondClassWithDependency = classes.get(SecondClassWithDependency.class);
Set<JavaAccess<?>> accesses = classHoldingDependencies.getAccessesToSelf();
Set<JavaAccess<?>> expected = ImmutableSet.<JavaAccess<?>>builder().addAll(getByTargetOwner(classHoldingDependencies.getAccessesFromSelf(), classHoldingDependencies)).addAll(getByTargetOwner(firstClassWithDependency.getAccessesFromSelf(), classHoldingDependencies)).addAll(getByTargetOwner(secondClassWithDependency.getAccessesFromSelf(), classHoldingDependencies)).build();
assertThat(accesses).as("Accesses to ClassWithDependents").isEqualTo(expected);
}
use of com.tngtech.archunit.core.domain.JavaAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method all_accesses_know_which_exceptions_are_handled.
@Test
public void all_accesses_know_which_exceptions_are_handled() {
@SuppressWarnings({ "unused", "WriteOnlyObject" })
class Origin {
void method() {
new ArrayList<>().add(1);
try {
Data_all_accesses_know_which_exceptions_are_handled.Target target = new Data_all_accesses_know_which_exceptions_are_handled.Target();
target.target(target.field);
} catch (IllegalStateException e) {
e.printStackTrace();
}
new Object();
}
}
JavaClasses classes = new ClassFileImporter().importClasses(Origin.class, Data_all_accesses_know_which_exceptions_are_handled.Target.class);
JavaMethod method = classes.get(Origin.class).getMethod("method");
Set<JavaAccess<?>> accesses = method.getAccessesFromSelf();
Function<JavaAccess<?>, Boolean> targetsTarget = GET_TARGET.then(Get.owner()).then(equivalentTo(Data_all_accesses_know_which_exceptions_are_handled.Target.class)::test);
Map<Boolean, List<JavaAccess<?>>> accessesByTargetsTarget = accesses.stream().collect(groupingBy(targetsTarget));
assertThat(accessesByTargetsTarget.get(true)).as("accesses that target target").isNotEmpty();
for (JavaAccess<?> access : accessesByTargetsTarget.get(true)) {
assertThatTryCatchBlock(getOnlyElement(access.getContainingTryBlocks())).isDeclaredIn(method).catches(IllegalStateException.class);
}
assertThat(accessesByTargetsTarget.get(false)).as("accesses that do not target target").isNotEmpty();
for (JavaAccess<?> access : accessesByTargetsTarget.get(false)) {
assertThat(access.getContainingTryBlocks()).as("containing try-catch-blocks").isEmpty();
}
}
use of com.tngtech.archunit.core.domain.JavaAccess in project ArchUnit by TNG.
the class ClassFileImporterLambdaAccessesTest method imports_complex_combination_of_lambda_accesses_to_nested_class.
@Test
public void imports_complex_combination_of_lambda_accesses_to_nested_class() {
@SuppressWarnings("unused")
class Caller {
private Target target;
void call() {
Supplier<Supplier<Supplier<Target>>> quiteNestedConstructorCallSupplier = () -> () -> Target::new;
Function<String, Supplier<Supplier<Supplier<Target>>>> quiteNestedMethodCallSupplier = s -> () -> () -> () -> target.inner.method(s);
Supplier<Supplier<Supplier<String>>> quiteNestedFieldSupplier = () -> () -> () -> target.inner.field;
}
class Target {
final Inner inner = new Inner();
class Inner {
String field;
Target method(String s) {
return Target.this;
}
}
}
}
JavaClasses classes = new ClassFileImporter().importClasses(Caller.class, Caller.Target.class);
Set<JavaAccess<?>> accesses = classes.get(Caller.class).getAccessesFromSelf();
assertThatAccesses(accesses).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.class, CONSTRUCTOR_NAME)).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.Inner.class, "method")).contain(access().fromOrigin(Caller.class, "call").toTarget(Caller.Target.Inner.class, "field"));
}
use of com.tngtech.archunit.core.domain.JavaAccess in project ArchUnit by TNG.
the class JavaAccessConditionTest method assertThatOnlyAccessToSomeClassFor.
private ConditionEventsAssertion assertThatOnlyAccessToSomeClassFor(JavaClass clazz, JavaAccessCondition<JavaAccess<?>> condition) {
Set<JavaAccess<?>> accesses = filterByTarget(clazz.getAccessesFromSelf(), SomeClass.class);
ConditionEvents events = new ConditionEvents();
for (JavaAccess<?> access : accesses) {
condition.check(access, events);
}
return assertThat(events);
}
Aggregations