Search in sources :

Example 56 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class JavaClassTransitiveDependenciesTest method findsTransitiveDependenciesInAcyclicGraph.

@Test
public void findsTransitiveDependenciesInAcyclicGraph() {
    Class<?> a = AcyclicGraph.A.class;
    Class<?> b = AcyclicGraph.B.class;
    Class<?> c = AcyclicGraph.C.class;
    Class<?> d = AcyclicGraph.D.class;
    JavaClasses classes = new ClassFileImporter().importClasses(a, b, c, d);
    Class<?> cArray = AcyclicGraph.C[][].class;
    // @formatter:off
    assertThatDependencies(classes.get(a).getTransitiveDependenciesFromSelf()).contain(a, Object.class).contain(a, b).contain(b, Object.class).contain(b, Integer.class).contain(a, cArray).contain(c, Object.class).contain(c, d).contain(d, Object.class).contain(d, String.class);
    assertThatDependencies(classes.get(b).getTransitiveDependenciesFromSelf()).contain(b, Object.class).contain(b, Integer.class);
    assertThatDependencies(classes.get(c).getTransitiveDependenciesFromSelf()).contain(c, Object.class).contain(c, d).contain(d, Object.class).contain(d, String.class);
// @formatter:on
}
Also used : ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 57 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class JavaParameterizedTypeTest method createTestInput.

@SuppressWarnings("OptionalGetWithoutIsPresent")
private static Object[] createTestInput(Class<?> testClass) {
    Type reflectionType = Arrays.stream(testClass.getTypeParameters()).filter(v -> v.getName().equals("TEST")).map(v -> v.getBounds()[0]).findFirst().get();
    JavaType javaType = new ClassFileImporter().importClass(testClass).getTypeParameters().stream().filter(v -> v.getName().equals("TEST")).map(v -> v.getBounds().get(0)).findFirst().get();
    return $(javaType, reflectionType);
}
Also used : Arrays(java.util.Arrays) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) DataProviders.$(com.tngtech.java.junit.dataprovider.DataProviders.$) Test(org.junit.Test) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) Map(java.util.Map) Type(java.lang.reflect.Type) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter)

Example 58 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class JavaTypeTest method function_TO_ERASURE.

@Test
public void function_TO_ERASURE() {
    JavaType javaType = mock(JavaType.class);
    JavaClass erasure = new ClassFileImporter().importClass(getClass());
    when(javaType.toErasure()).thenReturn(erasure);
    assertThat(TO_ERASURE.apply(javaType)).isEqualTo(erasure);
}
Also used : ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 59 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class AnnotationProxyTest method wrong_annotation_type_is_rejected.

@Test
public void wrong_annotation_type_is_rejected() {
    JavaAnnotation<?> mismatch = new ClassFileImporter().importClasses(TestAnnotation.class, Retention.class).get(TestAnnotation.class).getAnnotationOfType(Retention.class.getName());
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(Retention.class.getSimpleName());
    thrown.expectMessage(TestAnnotation.class.getSimpleName());
    thrown.expectMessage("incompatible");
    AnnotationProxy.of(TestAnnotation.class, mismatch);
}
Also used : Retention(java.lang.annotation.Retention) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 60 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class DependencyTest method Dependency_from_access_with_component_type.

@Test
@UseDataProvider("method_calls_to_array_types")
public void Dependency_from_access_with_component_type(Class<?> classDependingOnArray, String nameOfMethodWithArrayMethodCall, Class<?> arrayType, int expectedLineNumber) throws NoSuchMethodException {
    Method reflectionMethodWithArrayMethodCall = classDependingOnArray.getDeclaredMethod(nameOfMethodWithArrayMethodCall);
    Class<?> reflectionDeclaringClass = reflectionMethodWithArrayMethodCall.getDeclaringClass();
    JavaMethod method = new ClassFileImporter().importClasses(reflectionDeclaringClass).get(reflectionDeclaringClass).getMethod(reflectionMethodWithArrayMethodCall.getName());
    JavaMethodCall call = getOnlyElement(method.getMethodCallsFromSelf());
    Set<Dependency> dependencies = Dependency.tryCreateFromAccess(call);
    DependenciesAssertion.ExpectedDependencies expectedDependencies = from(reflectionDeclaringClass).to(arrayType).withDescriptionContaining("Method <%s> calls method <%s>", method.getFullName(), arrayType.getName() + ".clone()").inLocation(classDependingOnArray, expectedLineNumber);
    Class<?> expectedComponentType = arrayType.getComponentType();
    while (expectedComponentType != null) {
        expectedDependencies.from(reflectionDeclaringClass).to(expectedComponentType).withDescriptionContaining("Method <%s> depends on component type <%s>", method.getFullName(), expectedComponentType.getName()).inLocation(classDependingOnArray, expectedLineNumber);
        expectedComponentType = expectedComponentType.getComponentType();
    }
    assertThatDependencies(dependencies).containOnly(expectedDependencies);
}
Also used : Method(java.lang.reflect.Method) DependenciesAssertion(com.tngtech.archunit.testutil.assertion.DependenciesAssertion) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)136 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)98 Test (org.junit.Test)76 Test (org.junit.jupiter.api.Test)52 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 JavaClass (com.tngtech.archunit.core.domain.JavaClass)14 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)13 Workspace (com.structurizr.Workspace)12 ArchRule (com.tngtech.archunit.lang.ArchRule)12 Application (org.archifacts.core.model.Application)12 Component (com.structurizr.model.Component)10 Container (com.structurizr.model.Container)10 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)10 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10 JavaPackage (com.tngtech.archunit.core.domain.JavaPackage)5 LayeredArchitecture (com.tngtech.archunit.library.Architectures.LayeredArchitecture)5 OnionArchitecture (com.tngtech.archunit.library.Architectures.OnionArchitecture)5 Optional (java.util.Optional)5 Artifact (org.archifacts.core.model.Artifact)5 MiscArtifact (org.archifacts.core.model.MiscArtifact)5