Search in sources :

Example 46 with JavaClass

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

the class ClassFileImporterTest method imports_simple_local_class.

@Test
public void imports_simple_local_class() throws Exception {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/innerclassimport"));
    JavaClass localClass = classes.get(ClassWithInnerClass.class.getName() + "$1LocalCaller");
    assertThat(localClass).matches(Class.forName(localClass.getName())).isTopLevelClass(false).isNestedClass(true).isMemberClass(false).isInnerClass(true).isLocalClass(true).isAnonymousClass(false).isRecord(false);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 47 with JavaClass

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

the class ClassFileImporterTest method imports_interfaces_and_classes.

@Test
public void imports_interfaces_and_classes() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/classhierarchyimport"));
    JavaClass baseClass = classes.get(BaseClass.class);
    JavaClass parentInterface = classes.get(ParentInterface.class);
    assertThat(baseClass).isInterface(false);
    assertThat(parentInterface).isInterface(true);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 48 with JavaClass

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

the class ClassFileImporterTest method imports_interfaces.

@Test
public void imports_interfaces() {
    JavaClass simpleInterface = new ClassFileImporter().importUrl(getClass().getResource("testexamples/simpleimport")).get(InterfaceToImport.class);
    assertThat(simpleInterface).matches(InterfaceToImport.class).hasNoSuperclass().hasNoInterfaces().isInterface(true).isEnum(false).isRecord(false);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) InterfaceToImport(com.tngtech.archunit.core.importer.testexamples.simpleimport.InterfaceToImport) Test(org.junit.Test)

Example 49 with JavaClass

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

the class ClassFileImporterTest method creates_relations_between_classes_and_interfaces.

@Test
public void creates_relations_between_classes_and_interfaces() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/classhierarchyimport"));
    JavaClass baseClass = classes.get(BaseClass.class);
    JavaClass otherInterface = classes.get(OtherInterface.class);
    JavaClass subclass = classes.get(Subclass.class);
    JavaClass subinterface = classes.get(Subinterface.class);
    JavaClass otherSubclass = classes.get(OtherSubclass.class);
    JavaClass parentInterface = classes.get(ParentInterface.class);
    JavaClass grandParentInterface = classes.get(GrandParentInterface.class);
    JavaClass someCollection = classes.get(SomeCollection.class);
    JavaClass collectionInterface = classes.get(CollectionInterface.class);
    assertThat(baseClass.getRawInterfaces()).containsOnly(otherInterface);
    assertThat(baseClass.getAllRawInterfaces()).containsOnly(otherInterface, grandParentInterface);
    assertThat(subclass.getRawInterfaces()).containsOnly(subinterface);
    assertThat(subclass.getAllRawInterfaces()).containsOnly(subinterface, otherInterface, parentInterface, grandParentInterface);
    assertThat(otherSubclass.getRawInterfaces()).containsOnly(parentInterface);
    assertThat(otherSubclass.getAllRawInterfaces()).containsOnly(parentInterface, grandParentInterface, otherInterface);
    assertThat(someCollection.getRawInterfaces()).containsOnly(collectionInterface, otherInterface, subinterface);
    assertThat(someCollection.getAllRawInterfaces()).extractingResultOf("reflect").containsOnly(CollectionInterface.class, OtherInterface.class, Subinterface.class, ParentInterface.class, GrandParentInterface.class, Collection.class, Iterable.class);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 50 with JavaClass

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

the class ClassFileImporterTest method adds_package_of_component_type_to_arrays.

// we want to diverge from the Reflection API in this place, because it is way more useful for dependency checks,
// if com.some.SomeArray[].getPackageName() reports 'com.some' instead of '' (which would be ArchUnit's equivalent of null)
@Test
@UseDataProvider("array_types")
public void adds_package_of_component_type_to_arrays(Class<?> classAccessingArray) {
    JavaClass javaClass = new ClassFileImporter().importPackagesOf(classAccessingArray).get(classAccessingArray);
    JavaClass arrayType = getOnlyElement(javaClass.getFieldAccessesFromSelf()).getTarget().getRawType();
    assertThat(arrayType.getPackageName()).isEqualTo(ClassUsedInArray.class.getPackage().getName());
    assertThat(arrayType.getPackage().getName()).isEqualTo(ClassUsedInArray.class.getPackage().getName());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

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