Search in sources :

Example 51 with JavaClass

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

the class ClassFileImporterTest method imports_enclosing_method_of_anonymous_class.

@Test
public void imports_enclosing_method_of_anonymous_class() throws ClassNotFoundException {
    @SuppressWarnings("unused")
    class ClassCreatingAnonymousClassInMethod {

        void someMethod() {
            new Serializable() {
            };
        }
    }
    String anonymousClassName = ClassCreatingAnonymousClassInMethod.class.getName() + "$1";
    JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingAnonymousClassInMethod.class, Class.forName(anonymousClassName));
    JavaClass enclosingClass = classes.get(ClassCreatingAnonymousClassInMethod.class);
    JavaClass anonymousClass = classes.get(anonymousClassName);
    assertThat(anonymousClass.getEnclosingCodeUnit()).contains(enclosingClass.getMethod("someMethod"));
    assertThat(anonymousClass.getEnclosingClass()).contains(enclosingClass);
}
Also used : Serializable(java.io.Serializable) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 52 with JavaClass

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

the class ClassFileImporterTest method reflect_works.

@Test
public void reflect_works() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/innerclassimport"));
    JavaClass calledClass = classes.get(CalledClass.class);
    assertThat(calledClass.reflect()).isEqualTo(CalledClass.class);
    assertThat(calledClass.getField("someString").reflect()).isEqualTo(field(CalledClass.class, "someString"));
    assertThat(calledClass.getConstructor().reflect()).isEqualTo(constructor(CalledClass.class));
    assertThat(calledClass.getConstructor(String.class).reflect()).isEqualTo(constructor(CalledClass.class, String.class));
    assertThat(calledClass.getCodeUnitWithParameterTypes(CONSTRUCTOR_NAME, String.class).reflect()).isEqualTo(constructor(CalledClass.class, String.class));
    JavaClass innerClass = classes.get(ClassWithInnerClass.Inner.class);
    assertThat(innerClass.reflect()).isEqualTo(ClassWithInnerClass.Inner.class);
    assertThat(innerClass.getMethod("call").reflect()).isEqualTo(method(ClassWithInnerClass.Inner.class, "call"));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) CalledClass(com.tngtech.archunit.core.importer.testexamples.innerclassimport.CalledClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithInnerClass(com.tngtech.archunit.core.importer.testexamples.innerclassimport.ClassWithInnerClass) Test(org.junit.Test)

Example 53 with JavaClass

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

the class ClassFileImporterTest method imports_enclosing_constructor_of_local_class.

@Test
public void imports_enclosing_constructor_of_local_class() throws ClassNotFoundException {
    @SuppressWarnings("unused")
    class ClassCreatingLocalClassInConstructor {

        ClassCreatingLocalClassInConstructor() {
            class SomeLocalClass {
            }
        }
    }
    String localClassName = ClassCreatingLocalClassInConstructor.class.getName() + "$1SomeLocalClass";
    JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingLocalClassInConstructor.class, Class.forName(localClassName));
    JavaClass enclosingClass = classes.get(ClassCreatingLocalClassInConstructor.class);
    JavaClass localClass = classes.get(localClassName);
    assertThat(localClass.getEnclosingCodeUnit()).contains(enclosingClass.getConstructor(getClass()));
    assertThat(localClass.getEnclosingClass()).contains(enclosingClass);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 54 with JavaClass

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

the class ClassFileImporterTest method imports_no_enclosing_code_unit_of_anonymous_class_defined_outside_of_method.

@Test
public void imports_no_enclosing_code_unit_of_anonymous_class_defined_outside_of_method() throws ClassNotFoundException {
    @SuppressWarnings("unused")
    class ClassCreatingAnonymousClassInConstructor {

        final Serializable field = new Serializable() {
        };
    }
    String anonymousClassName = ClassCreatingAnonymousClassInConstructor.class.getName() + "$1";
    JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingAnonymousClassInConstructor.class, Class.forName(anonymousClassName));
    JavaClass enclosingClass = classes.get(ClassCreatingAnonymousClassInConstructor.class);
    JavaClass anonymousClass = classes.get(anonymousClassName);
    assertThat(anonymousClass.getEnclosingClass()).contains(enclosingClass);
    assertThat(anonymousClass.getEnclosingCodeUnit()).isEmpty();
}
Also used : Serializable(java.io.Serializable) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 55 with JavaClass

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

the class ClassFileProcessorTest method ClassResolverFromClassPath_resolves_robustly.

@Test
public void ClassResolverFromClassPath_resolves_robustly() {
    Optional<JavaClass> resolved = new ClassResolverFromClasspath().tryResolve("not.There");
    assertThat(resolved).isEmpty();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassResolverFromClasspath(com.tngtech.archunit.core.importer.resolvers.ClassResolverFromClasspath) 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