Search in sources :

Example 41 with JavaClass

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

the class ClassFileImporterMembersTest method imports_complex_constructor_with_correct_parameters.

@Test
public void imports_complex_constructor_with_correct_parameters() throws Exception {
    JavaClass clazz = new ClassFileImporter().importUrl(getClass().getResource("testexamples/constructorimport")).get(ClassWithComplexConstructor.class);
    assertThat(clazz.getConstructors()).as("Constructors").hasSize(1);
    assertThat(clazz.getConstructor(String.class, long.class, long.class, Serializable.class, Serializable.class)).isEquivalentTo(ClassWithComplexConstructor.class.getDeclaredConstructor(String.class, long.class, long.class, Serializable.class, Serializable.class));
}
Also used : Serializable(java.io.Serializable) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithComplexConstructor(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithComplexConstructor) Test(org.junit.Test)

Example 42 with JavaClass

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

the class ClassFileImporterMembersTest method imports_members_with_sourceCodeLocation.

@Test
public void imports_members_with_sourceCodeLocation() {
    JavaClasses importedClasses = new ClassFileImporter().importUrl(getClass().getResource("testexamples/methodimport"));
    String sourceFileName = "ClassWithMultipleMethods.java";
    JavaClass javaClass = importedClasses.get(ClassWithMultipleMethods.class);
    assertThat(javaClass.getField("usage").getSourceCodeLocation()).hasToString(// the byte code has no line number associated with a field
    "(" + sourceFileName + ":0)");
    assertThat(javaClass.getConstructor().getSourceCodeLocation()).hasToString(// auto-generated constructor seems to get line of class definition
    "(" + sourceFileName + ":4)");
    assertThat(javaClass.getStaticInitializer().get().getSourceCodeLocation()).hasToString(// auto-generated static initializer seems to get line of first static variable definition
    "(" + sourceFileName + ":5)");
    assertThat(javaClass.getMethod("methodDefinedInLine7").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":7)");
    assertThat(javaClass.getMethod("methodWithBodyStartingInLine10").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":10)");
    assertThat(javaClass.getMethod("emptyMethodDefinedInLine15").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":15)");
    assertThat(javaClass.getMethod("emptyMethodEndingInLine19").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":19)");
    javaClass = importedClasses.get(ClassWithMultipleMethods.InnerClass.class);
    assertThat(javaClass.getMethod("methodWithBodyStartingInLine24").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":24)");
    javaClass = importedClasses.get(ClassWithMultipleMethods.InnerClass.class.getName() + "$1");
    assertThat(javaClass.getMethod("run").getSourceCodeLocation()).hasToString("(" + sourceFileName + ":27)");
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 43 with JavaClass

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

the class ClassFileImporterMembersTest method imports_overridden_methods_correctly.

@Test
public void imports_overridden_methods_correctly() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/classhierarchyimport"));
    JavaClass baseClass = classes.get(BaseClass.class);
    JavaClass subClass = classes.get(Subclass.class);
    assertThat(baseClass.getCodeUnitWithParameterTypes("getSomeField").getModifiers()).containsOnly(PROTECTED);
    assertThat(subClass.getCodeUnitWithParameterTypes("getSomeField").getModifiers()).containsOnly(PUBLIC);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 44 with JavaClass

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

the class ClassFileImporterMembersTest method classes_know_which_instanceof_checks_check_their_type.

@Test
public void classes_know_which_instanceof_checks_check_their_type() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(InstanceofChecked.class).get(InstanceofChecked.class);
    Set<JavaClass> origins = clazz.getInstanceofChecksWithTypeOfSelf().stream().map(instanceofCheck -> instanceofCheck.getOwner().getOwner()).collect(toSet());
    assertThatTypes(origins).matchInAnyOrder(ChecksInstanceofInMethod.class, ChecksInstanceofInConstructor.class, ChecksInstanceofInStaticInitializer.class);
}
Also used : SomeEnum(com.tngtech.archunit.core.importer.testexamples.SomeEnum) OtherClass(com.tngtech.archunit.core.importer.testexamples.OtherClass) SecondCheckedException(com.tngtech.archunit.core.importer.testexamples.SecondCheckedException) Objects(org.assertj.core.util.Objects) ClassWithStringField(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithStringField) JavaField(com.tngtech.archunit.core.domain.JavaField) ReferencedClassObjectsAssertion(com.tngtech.archunit.testutil.assertion.ReferencedClassObjectsAssertion) Iterables.concat(com.google.common.collect.Iterables.concat) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Assertions.assertThat(com.tngtech.archunit.testutil.Assertions.assertThat) Method(java.lang.reflect.Method) Path(java.nio.file.Path) ClassFileImporterTestUtils.getFields(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getFields) SomeClass(com.tngtech.archunit.core.importer.testexamples.SomeClass) Collectors.toSet(java.util.stream.Collectors.toSet) PRIVATE(com.tngtech.archunit.core.domain.JavaModifier.PRIVATE) ThrowsDeclaration(com.tngtech.archunit.core.domain.ThrowsDeclaration) BaseClass(com.tngtech.archunit.core.importer.testexamples.classhierarchyimport.BaseClass) ImmutableSet(com.google.common.collect.ImmutableSet) ClassWithStringStringMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithStringStringMethod) ReferencingClassObjects(com.tngtech.archunit.core.importer.testexamples.referencedclassobjects.ReferencingClassObjects) ClassWithSimpleConstructors(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithSimpleConstructors) ReferencedClassObjectsAssertion.referencedClassObject(com.tngtech.archunit.testutil.assertion.ReferencedClassObjectsAssertion.referencedClassObject) Set(java.util.Set) ChecksInstanceofInStaticInitializer(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInStaticInitializer) ReflectionTestUtils.field(com.tngtech.archunit.testutil.ReflectionTestUtils.field) FileSystem(java.nio.file.FileSystem) Serializable(java.io.Serializable) ClassWithThrowingConstructor(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithThrowingConstructor) Assertions.assertThatThrowsClause(com.tngtech.archunit.testutil.Assertions.assertThatThrowsClause) Optional(java.util.Optional) Assertions.assertThatType(com.tngtech.archunit.testutil.Assertions.assertThatType) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) FirstCheckedException(com.tngtech.archunit.core.importer.testexamples.FirstCheckedException) ChecksInstanceofInConstructor(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInConstructor) PROTECTED(com.tngtech.archunit.core.domain.JavaModifier.PROTECTED) ClassWithComplexMethod(com.tngtech.archunit.core.importer.testexamples.complexmethodimport.ClassWithComplexMethod) ClassWithIntAndObjectFields(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithIntAndObjectFields) Assertions.assertThatReferencedClassObjects(com.tngtech.archunit.testutil.Assertions.assertThatReferencedClassObjects) Constructor(java.lang.reflect.Constructor) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) ChecksInstanceofInMethod(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInMethod) Assertions.assertThatTypes(com.tngtech.archunit.testutil.Assertions.assertThatTypes) FilterInputStream(java.io.FilterInputStream) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Charset(java.nio.charset.Charset) ClassFileImporterTestUtils.getCodeUnits(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getCodeUnits) ClassFileImporterTestUtils.getMethods(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getMethods) Buffer(java.nio.Buffer) InstanceofChecked(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.InstanceofChecked) Utils.namesOf(com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf) ClassWithThrowingMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithThrowingMethod) PUBLIC(com.tngtech.archunit.core.domain.JavaModifier.PUBLIC) ClassWithObjectVoidAndIntIntSerializableMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithObjectVoidAndIntIntSerializableMethod) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ClassWithMultipleMethods(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithMultipleMethods) Test(org.junit.Test) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) File(java.io.File) ClassWithComplexConstructor(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithComplexConstructor) ClassFileImporterTestUtils.findAnyByName(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.findAnyByName) Subclass(com.tngtech.archunit.core.importer.testexamples.classhierarchyimport.Subclass) VOLATILE(com.tngtech.archunit.core.domain.JavaModifier.VOLATILE) TRANSIENT(com.tngtech.archunit.core.domain.JavaModifier.TRANSIENT) JavaClass(com.tngtech.archunit.core.domain.JavaClass) InstanceofChecked(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.InstanceofChecked) Test(org.junit.Test)

Example 45 with JavaClass

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

the class ClassFileImporterMembersTest method imports_simple_constructors_with_correct_parameters.

@Test
public void imports_simple_constructors_with_correct_parameters() throws Exception {
    JavaClass clazz = new ClassFileImporter().importUrl(getClass().getResource("testexamples/constructorimport")).get(ClassWithSimpleConstructors.class);
    assertThat(clazz.getConstructors()).as("Constructors").hasSize(3);
    Constructor<ClassWithSimpleConstructors> expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor();
    assertThat(clazz.getConstructor()).isEquivalentTo(expectedConstructor);
    assertThat(clazz.tryGetConstructor().get()).isEquivalentTo(expectedConstructor);
    Class<?>[] parameterTypes = { Object.class };
    expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
    assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
    assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);
    assertThat(clazz.tryGetConstructor(parameterTypes).get()).isEquivalentTo(expectedConstructor);
    assertThat(clazz.tryGetConstructor(Objects.namesOf(parameterTypes)).get()).isEquivalentTo(expectedConstructor);
    parameterTypes = new Class[] { int.class, int.class };
    expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
    assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
    assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);
    assertThat(clazz.tryGetConstructor(parameterTypes).get()).isEquivalentTo(expectedConstructor);
    assertThat(clazz.tryGetConstructor(Objects.namesOf(parameterTypes)).get()).isEquivalentTo(expectedConstructor);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithSimpleConstructors(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithSimpleConstructors) OtherClass(com.tngtech.archunit.core.importer.testexamples.OtherClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) SomeClass(com.tngtech.archunit.core.importer.testexamples.SomeClass) BaseClass(com.tngtech.archunit.core.importer.testexamples.classhierarchyimport.BaseClass) 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