Search in sources :

Example 11 with JavaFieldAccess

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

the class ClassFileImporterAccessesTest method imports_multiple_own_accesses.

@Test
public void imports_multiple_own_accesses() {
    JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
    Set<JavaFieldAccess> fieldAccesses = classWithOwnFieldAccess.getFieldAccessesFromSelf();
    assertThat(fieldAccesses).hasSize(4);
    assertThat(getOnly(fieldAccesses, "stringValue", GET).getLineNumber()).as("Line number of get stringValue").isEqualTo(8);
    assertThat(getOnly(fieldAccesses, "stringValue", SET).getLineNumber()).as("Line number of set stringValue").isEqualTo(12);
    assertThat(getOnly(fieldAccesses, "intValue", GET).getLineNumber()).as("Line number of get intValue").isEqualTo(16);
    assertThat(getOnly(fieldAccesses, "intValue", SET).getLineNumber()).as("Line number of set intValue").isEqualTo(20);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 12 with JavaFieldAccess

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

the class ClassFileImporterAccessesTest method imports_own_get_field_access.

@Test
public void imports_own_get_field_access() {
    JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
    JavaMethod getStringValue = classWithOwnFieldAccess.getMethod("getStringValue");
    JavaFieldAccess access = getOnlyElement(getStringValue.getFieldAccesses());
    assertThatAccess(access).isOfType(GET).isFrom(getStringValue).isTo("stringValue").inLineNumber(8);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Example 13 with JavaFieldAccess

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

the class ClassFileImporterAccessesTest method imports_other_field_accesses_from_constructor.

@Test
public void imports_other_field_accesses_from_constructor() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
    JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
    JavaClass fieldAccessFromConstructor = classes.get(ForeignFieldAccessFromConstructor.class);
    Set<JavaFieldAccess> accesses = fieldAccessFromConstructor.getFieldAccessesFromSelf();
    assertThat(accesses).as("Number of field accesses from " + fieldAccessFromConstructor.getName()).hasSize(2);
    assertThatAccess(getOnly(accesses, "stringValue", GET)).isFrom(fieldAccessFromConstructor.getCodeUnitWithParameterTypes(CONSTRUCTOR_NAME)).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(5);
    assertThatAccess(getOnly(accesses, "intValue", SET)).isFrom(fieldAccessFromConstructor.getCodeUnitWithParameterTypes(CONSTRUCTOR_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)

Example 14 with JavaFieldAccess

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

the class ClassFileImporterAccessesTest method imports_other_field_accesses.

@Test
public void imports_other_field_accesses() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
    JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
    JavaClass classWithForeignFieldAccess = classes.get(ForeignFieldAccess.class);
    Set<JavaFieldAccess> accesses = classWithForeignFieldAccess.getFieldAccessesFromSelf();
    assertThat(accesses).hasSize(4);
    assertThatAccess(getOnly(accesses, "stringValue", GET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getStringFromOther")).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(5);
    assertThatAccess(getOnly(accesses, "stringValue", SET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setStringFromOther")).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(9);
    assertThatAccess(getOnly(accesses, "intValue", GET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getIntFromOther")).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(13);
    assertThatAccess(getOnly(accesses, "intValue", SET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setIntFromOther")).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(17);
}
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 15 with JavaFieldAccess

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

the class ClassFileImporterAccessesTest method imports_own_set_field_access.

@Test
public void imports_own_set_field_access() {
    JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
    JavaMethod setStringValue = classWithOwnFieldAccess.getMethod("setStringValue", String.class);
    JavaFieldAccess access = getOnlyElement(setStringValue.getFieldAccesses());
    assertThatAccess(access).isOfType(SET).isFrom(setStringValue).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(12);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Aggregations

JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)22 Test (org.junit.Test)21 JavaClass (com.tngtech.archunit.core.domain.JavaClass)16 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)10 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)3 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 Formatters.formatNamesOf (com.tngtech.archunit.core.domain.Formatters.formatNamesOf)1 JavaAccess (com.tngtech.archunit.core.domain.JavaAccess)1 CONSTRUCTOR_NAME (com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaField (com.tngtech.archunit.core.domain.JavaField)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 Utils.namesOf (com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf)1 FieldAccessRecord (com.tngtech.archunit.core.importer.AccessRecord.FieldAccessRecord)1 FieldAccessTargetBuilder (com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder)1