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