Search in sources :

Example 71 with JavaClass

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));
}
Also used : ClassWithTryCatchBlockWithoutThrowables(com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithTryCatchBlockWithoutThrowables) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) Test(org.junit.Test)

Example 72 with JavaClass

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);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 73 with JavaClass

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);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 74 with JavaClass

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);
}
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 75 with JavaClass

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);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) 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