Search in sources :

Example 71 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses 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 72 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses 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 73 with JavaClasses

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

the class ClassFileImporterMembersTest method classes_know_which_methods_have_their_type_as_parameter.

@Test
public void classes_know_which_methods_have_their_type_as_parameter() {
    JavaClasses classes = new ClassFileImporter().importClasses(SomeClass.class, OtherClass.class, SomeEnum.class);
    assertThat(classes.get(SomeEnum.class).getMethodsWithParameterTypeOfSelf()).extracting("name").contains("methodWithSomeEnumParameter", "otherMethodWithSomeEnumParameter");
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Test(org.junit.Test)

Example 74 with JavaClasses

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

the class ClassFileImporterMembersTest method classes_know_which_methods_have_their_type_as_return_type.

@Test
public void classes_know_which_methods_have_their_type_as_return_type() {
    JavaClasses classes = new ClassFileImporter().importClasses(SomeClass.class, OtherClass.class, SomeEnum.class);
    assertThat(classes.get(SomeEnum.class).getMethodsWithReturnTypeOfSelf()).extracting("name").contains("methodWithSomeEnumReturnType", "otherMethodWithSomeEnumReturnType");
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Test(org.junit.Test)

Example 75 with JavaClasses

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

the class ClassFileImporterMembersTest method classes_know_which_fields_have_their_type.

@Test
public void classes_know_which_fields_have_their_type() {
    JavaClasses classes = new ClassFileImporter().importClasses(SomeClass.class, OtherClass.class, SomeEnum.class);
    assertThat(classes.get(SomeEnum.class).getFieldsWithTypeOfSelf()).extracting("name").contains("other", "someEnum");
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Test(org.junit.Test)

Aggregations

JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)248 Test (org.junit.Test)184 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)98 JavaClass (com.tngtech.archunit.core.domain.JavaClass)72 Test (org.junit.jupiter.api.Test)54 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 ArchRule (com.tngtech.archunit.lang.ArchRule)19 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)15 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)14 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)13 Workspace (com.structurizr.Workspace)12 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)12 Set (java.util.Set)12 Application (org.archifacts.core.model.Application)12 Component (com.structurizr.model.Component)10 Container (com.structurizr.model.Container)10 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10