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));
}
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)");
}
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);
}
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);
}
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);
}
Aggregations