Search in sources :

Example 1 with Dependency

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

the class ExpectedDependency method correspondsTo.

@Override
public boolean correspondsTo(Object object) {
    if (!(object instanceof Dependency)) {
        return false;
    }
    Dependency dependency = (Dependency) object;
    boolean originMatches = dependency.getOriginClass().isEquivalentTo(origin);
    boolean targetMatches = dependency.getTargetClass().isEquivalentTo(target);
    boolean descriptionMatches = dependency.getDescription().matches(dependencyPattern);
    return originMatches && targetMatches && descriptionMatches;
}
Also used : Dependency(com.tngtech.archunit.core.domain.Dependency)

Example 2 with Dependency

use of com.tngtech.archunit.core.domain.Dependency 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);
}
Also used : SourceCodeLocation(com.tngtech.archunit.core.domain.SourceCodeLocation) Dependency(com.tngtech.archunit.core.domain.Dependency) AccessToSuperAndSubclassField(com.tngtech.archunit.core.importer.testexamples.hierarchicalfieldaccess.AccessToSuperAndSubclassField) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) JavaField(com.tngtech.archunit.core.domain.JavaField) ClassCDependingOnClassB_SuperclassOfX(com.tngtech.archunit.core.importer.testexamples.integration.ClassCDependingOnClassB_SuperclassOfX) ParentInterfaceWithFields(com.tngtech.archunit.core.importer.testexamples.fieldaccesstointerfaces.ParentInterfaceWithFields) Assertions.assertThatCall(com.tngtech.archunit.testutil.Assertions.assertThatCall) JavaCallQuery.methodCallTo(com.tngtech.archunit.testutil.JavaCallQuery.methodCallTo) Map(java.util.Map) ForeignFieldAccessFromConstructor(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ForeignFieldAccessFromConstructor) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) ExternalOverriddenMethodCall(com.tngtech.archunit.core.importer.testexamples.callimport.ExternalOverriddenMethodCall) Set(java.util.Set) Predicates.equivalentTo(com.tngtech.archunit.core.domain.JavaClass.Predicates.equivalentTo) ReflectionTestUtils.field(com.tngtech.archunit.testutil.ReflectionTestUtils.field) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ClassHoldingDependencies(com.tngtech.archunit.core.importer.testexamples.dependents.ClassHoldingDependencies) ChainableFunction(com.tngtech.archunit.base.ChainableFunction) HasOwner(com.tngtech.archunit.core.domain.properties.HasOwner) RunWith(org.junit.runner.RunWith) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) TryCatchBlocksAssertion.tryCatchBlock(com.tngtech.archunit.testutil.assertion.TryCatchBlocksAssertion.tryCatchBlock) Constructor(java.lang.reflect.Constructor) Get(com.tngtech.archunit.core.domain.properties.HasOwner.Functions.Get) ArrayList(java.util.ArrayList) ClassCallingSpecialTarget(com.tngtech.archunit.core.importer.testexamples.specialtargets.ClassCallingSpecialTarget) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) MultipleFieldAccessInSameMethod(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.MultipleFieldAccessInSameMethod) CallsOtherMethod(com.tngtech.archunit.core.importer.testexamples.callimport.CallsOtherMethod) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) FirstClassWithDependency(com.tngtech.archunit.core.importer.testexamples.dependents.FirstClassWithDependency) SuperclassWithAccessedField(com.tngtech.archunit.core.importer.testexamples.hierarchicalfieldaccess.SuperclassWithAccessedField) BRIDGE(com.tngtech.archunit.core.domain.JavaModifier.BRIDGE) IOException(java.io.IOException) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ClassA(com.tngtech.archunit.core.importer.testexamples.integration.ClassA) ClassFileImporterTestUtils.findAnyByName(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.findAnyByName) ParentClass(com.tngtech.archunit.core.importer.testexamples.complexexternal.ParentClass) ClassD(com.tngtech.archunit.core.importer.testexamples.integration.ClassD) InterfaceWithFields(com.tngtech.archunit.core.importer.testexamples.fieldaccesstointerfaces.InterfaceWithFields) ClassWithTryWithResources(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryWithResources) SYNTHETIC(com.tngtech.archunit.core.domain.JavaModifier.SYNTHETIC) CallsExternalMethod(com.tngtech.archunit.core.importer.testexamples.callimport.CallsExternalMethod) STATIC_INITIALIZER_NAME(com.tngtech.archunit.core.domain.JavaStaticInitializer.STATIC_INITIALIZER_NAME) ExternalSubtypeConstructorCall(com.tngtech.archunit.core.importer.testexamples.callimport.ExternalSubtypeConstructorCall) ExternalShadowedFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ExternalShadowedFieldAccess) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) BufferedReader(java.io.BufferedReader) CallsOtherConstructor(com.tngtech.archunit.core.importer.testexamples.callimport.CallsOtherConstructor) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) ExternalFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ExternalFieldAccess) SuperclassWithCalledMethod(com.tngtech.archunit.core.importer.testexamples.hierarchicalmethodcall.SuperclassWithCalledMethod) OwnStaticFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.OwnStaticFieldAccess) CallsMethodReturningArray(com.tngtech.archunit.core.importer.testexamples.callimport.CallsMethodReturningArray) TestUtils.asClasses(com.tngtech.archunit.core.domain.TestUtils.asClasses) SecondClassWithDependency(com.tngtech.archunit.core.importer.testexamples.dependents.SecondClassWithDependency) Collections.singleton(java.util.Collections.singleton) CONSTRUCTOR_NAME(com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassHoldingMethods(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassHoldingMethods) Assertions.assertThat(com.tngtech.archunit.testutil.Assertions.assertThat) ClassWithTryCatchBlockWithoutThrowables(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryCatchBlockWithoutThrowables) ReflectionTestUtils.method(com.tngtech.archunit.testutil.ReflectionTestUtils.method) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) FieldAccessTargetBuilder(com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder) ClassAccessingChildExtendingParentWithSameInterfaceFields(com.tngtech.archunit.core.importer.testexamples.fieldaccesstointerfaces.ambiguous_in_hierarchy.ClassAccessingChildExtendingParentWithSameInterfaceFields) ImmutableSet(com.google.common.collect.ImmutableSet) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) Predicate(java.util.function.Predicate) ChildClass(com.tngtech.archunit.core.importer.testexamples.complexexternal.ChildClass) ClassAccessingInterfaceFields(com.tngtech.archunit.core.importer.testexamples.fieldaccesstointerfaces.unique_in_hierarchy.ClassAccessingInterfaceFields) Assertions.assertThatAccess(com.tngtech.archunit.testutil.Assertions.assertThatAccess) List(java.util.List) ClassXDependingOnClassesABCD(com.tngtech.archunit.core.importer.testexamples.integration.ClassXDependingOnClassesABCD) InterfaceB(com.tngtech.archunit.core.importer.testexamples.diamond.InterfaceB) CallsOwnConstructor(com.tngtech.archunit.core.importer.testexamples.callimport.CallsOwnConstructor) InterfaceD(com.tngtech.archunit.core.importer.testexamples.diamond.InterfaceD) ClassBDependingOnClassA(com.tngtech.archunit.core.importer.testexamples.integration.ClassBDependingOnClassA) DomainBuilders.newMethodCallTargetBuilder(com.tngtech.archunit.core.importer.DomainBuilders.newMethodCallTargetBuilder) Optional(java.util.Optional) ForeignStaticFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ForeignStaticFieldAccess) ForeignFieldAccessFromStaticInitializer(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ForeignFieldAccessFromStaticInitializer) ClassImplementingD(com.tngtech.archunit.core.importer.testexamples.diamond.ClassImplementingD) CallOfSuperAndSubclassMethod(com.tngtech.archunit.core.importer.testexamples.hierarchicalmethodcall.CallOfSuperAndSubclassMethod) ClassCallingDiamond(com.tngtech.archunit.core.importer.testexamples.diamond.ClassCallingDiamond) SubclassWithCalledMethod(com.tngtech.archunit.core.importer.testexamples.hierarchicalmethodcall.SubclassWithCalledMethod) Assertions.assertThatType(com.tngtech.archunit.testutil.Assertions.assertThatType) ClassWithSimpleTryCatchBlocks(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithSimpleTryCatchBlocks) Socket(java.net.Socket) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassWithIntAndObjectFields(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithIntAndObjectFields) HashMap(java.util.HashMap) Function(java.util.function.Function) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) InterfaceOfClassX(com.tngtech.archunit.core.importer.testexamples.integration.InterfaceOfClassX) ExternalInterfaceMethodCall(com.tngtech.archunit.core.importer.testexamples.callimport.ExternalInterfaceMethodCall) Assertions.assertThatTypes(com.tngtech.archunit.testutil.Assertions.assertThatTypes) TestUtils.relativeResourceUri(com.tngtech.archunit.testutil.TestUtils.relativeResourceUri) ClassWithComplexTryCatchBlocks(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks) OtherInterfaceWithFields(com.tngtech.archunit.core.importer.testexamples.fieldaccesstointerfaces.OtherInterfaceWithFields) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) ForeignFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.ForeignFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) SET(com.tngtech.archunit.core.domain.JavaFieldAccess.AccessType.SET) TestUtils.targetFrom(com.tngtech.archunit.core.domain.TestUtils.targetFrom) ClassFileImporterTestUtils.getByName(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getByName) SubclassWithAccessedField(com.tngtech.archunit.core.importer.testexamples.hierarchicalfieldaccess.SubclassWithAccessedField) ReflectionTestUtils.constructor(com.tngtech.archunit.testutil.ReflectionTestUtils.constructor) CallsOwnMethod(com.tngtech.archunit.core.importer.testexamples.callimport.CallsOwnMethod) GET(com.tngtech.archunit.core.domain.JavaFieldAccess.AccessType.GET) OwnFieldAccess(com.tngtech.archunit.core.importer.testexamples.fieldaccessimport.OwnFieldAccess) JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Dependency(com.tngtech.archunit.core.domain.Dependency) FirstClassWithDependency(com.tngtech.archunit.core.importer.testexamples.dependents.FirstClassWithDependency) SecondClassWithDependency(com.tngtech.archunit.core.importer.testexamples.dependents.SecondClassWithDependency) Test(org.junit.Test)

Example 3 with Dependency

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

the class SlicesTest method slices_of_dependencies.

@Test
public void slices_of_dependencies() {
    JavaMethod methodThatCallsJavaUtil = TestUtils.importClassWithContext(Object.class).getMethod("toString");
    JavaMethod methodThatCallsJavaLang = TestUtils.importClassWithContext(Map.class).getMethod("put", Object.class, Object.class);
    simulateCall().from(methodThatCallsJavaUtil, 5).to(methodThatCallsJavaLang);
    simulateCall().from(methodThatCallsJavaLang, 1).to(methodThatCallsJavaUtil);
    Dependency first = dependencyFrom(getOnlyElement(methodThatCallsJavaUtil.getMethodCallsFromSelf()));
    Dependency second = dependencyFrom(getOnlyElement(methodThatCallsJavaLang.getMethodCallsFromSelf()));
    Slices slices = Slices.matching("java.(*)..").transform(ImmutableSet.of(first, second));
    assertThat(slices).extractingResultOf("getDescription").containsOnly("Slice lang", "Slice util");
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Dependency(com.tngtech.archunit.core.domain.Dependency) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Dependency

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

the class ArchConditionsTest method only_have_dependents_where.

@Test
public void only_have_dependents_where() {
    JavaClasses classes = importClasses(CallingClass.class, SomeClass.class);
    JavaClass accessedClass = classes.get(SomeClass.class);
    assertThat(onlyHaveDependentsWhere(DescribedPredicate.<Dependency>alwaysFalse())).checking(accessedClass).haveAtLeastOneViolationMessageMatching(String.format(".*%s.*%s.*", quote(CallingClass.class.getName()), quote(SomeClass.class.getName())));
    assertThat(onlyHaveDependentsWhere(DescribedPredicate.<Dependency>alwaysTrue().as("custom"))).hasDescription("only have dependents where custom").checking(accessedClass).containNoViolation();
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Dependency(com.tngtech.archunit.core.domain.Dependency) Test(org.junit.Test)

Aggregations

Dependency (com.tngtech.archunit.core.domain.Dependency)4 JavaClass (com.tngtech.archunit.core.domain.JavaClass)2 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 Test (org.junit.Test)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 ChainableFunction (com.tngtech.archunit.base.ChainableFunction)1 AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)1 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 JavaAccess (com.tngtech.archunit.core.domain.JavaAccess)1 Predicates.equivalentTo (com.tngtech.archunit.core.domain.JavaClass.Predicates.equivalentTo)1 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 CONSTRUCTOR_NAME (com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaField (com.tngtech.archunit.core.domain.JavaField)1 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)1