Search in sources :

Example 91 with JavaClasses

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

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

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

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

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

the class ClassesShouldEvaluator method on.

Set<JavaClass> on(Class<?>... toCheck) {
    JavaClasses classes = importClasses(toCheck);
    List<String> relevantFailures = getRelevantFailures(classes);
    return classes.stream().filter(clazz -> anyLineMatches(relevantFailures, clazz)).collect(toSet());
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArchConfiguration(com.tngtech.archunit.ArchConfiguration) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ArchRule(com.tngtech.archunit.lang.ArchRule) Pattern.quote(java.util.regex.Pattern.quote) Annotation(java.lang.annotation.Annotation) JavaClass(com.tngtech.archunit.core.domain.JavaClass) FailureReport(com.tngtech.archunit.lang.FailureReport) Splitter(com.google.common.base.Splitter) Collectors.toSet(java.util.stream.Collectors.toSet) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses)

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