use of com.tngtech.archunit.core.domain.JavaConstructor in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks.
@Test
public void method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks() {
JavaClasses classes = new ClassFileImporter().importClasses(ClassWithComplexTryCatchBlocks.class, ClassHoldingMethods.class);
JavaClass classHoldingMethods = classes.get(ClassHoldingMethods.class);
JavaClass classWithTryCatchBlocks = classes.get(ClassWithComplexTryCatchBlocks.class);
JavaMethod setSomeInt = classHoldingMethods.getMethod("setSomeInt", int.class);
JavaMethod setSomeString = classHoldingMethods.getMethod("setSomeString", String.class);
JavaMethod doSomething = classHoldingMethods.getMethod("doSomething");
JavaConstructor constructor = classWithTryCatchBlocks.getConstructor();
assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(12)).isNotWrappedInTryCatch();
assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(14)).isWrappedWithTryCatchFor(Exception.class);
assertThatCall(methodCallTo(doSomething).from(constructor).inLineNumber(17)).isWrappedWithTryCatchFor(Exception.class).isWrappedWithTryCatchFor(IllegalArgumentException.class).isWrappedWithTryCatchFor(UnsupportedOperationException.class);
assertThatCall(methodCallTo(setSomeString).from(constructor).inLineNumber(26)).isWrappedWithTryCatchFor(Throwable.class);
}
use of com.tngtech.archunit.core.domain.JavaConstructor in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method constructors_know_callers.
@Test
public void constructors_know_callers() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/dependents"));
JavaClass classHoldingDependencies = classes.get(ClassHoldingDependencies.class);
JavaClass firstClassWithDependency = classes.get(FirstClassWithDependency.class);
JavaClass secondClassWithDependency = classes.get(SecondClassWithDependency.class);
JavaConstructor targetConstructur = classHoldingDependencies.getConstructor();
Set<JavaConstructorCall> calls = targetConstructur.getCallsOfSelf();
Set<JavaConstructorCall> expected = ImmutableSet.<JavaConstructorCall>builder().addAll(getByTarget(classHoldingDependencies.getConstructorCallsFromSelf(), targetConstructur)).addAll(getByTarget(firstClassWithDependency.getConstructorCallsFromSelf(), targetConstructur)).addAll(getByTarget(secondClassWithDependency.getConstructorCallsFromSelf(), targetConstructur)).build();
assertThat(calls).as("Default Constructor calls to ClassWithDependents").isEqualTo(expected);
}
use of com.tngtech.archunit.core.domain.JavaConstructor in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_parameter_annotations_correctly_for_synthetic_constructor_parameter.
@Test
public void imports_parameter_annotations_correctly_for_synthetic_constructor_parameter() {
@SuppressWarnings("unused")
class LocalClassThusSyntheticFirstConstructorParameter {
LocalClassThusSyntheticFirstConstructorParameter(Object notAnnotated, @SimpleAnnotation("test") List<String> annotated) {
}
}
JavaConstructor constructor = getOnlyElement(new ClassFileImporter().importClasses(LocalClassThusSyntheticFirstConstructorParameter.class, String.class).get(LocalClassThusSyntheticFirstConstructorParameter.class).getConstructors());
List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = constructor.getParameterAnnotations();
assertThat(parameterAnnotations).as("parameter annotations").hasSize(2);
assertThatAnnotations(parameterAnnotations.get(0)).isEmpty();
assertThatAnnotations(parameterAnnotations.get(1)).isNotEmpty();
List<JavaParameter> parameters = constructor.getParameters();
for (int i = 0; i < parameterAnnotations.size(); i++) {
assertThat(parameterAnnotations.get(i)).isEqualTo(parameters.get(i).getAnnotations());
assertThatAnnotations(parameterAnnotations.get(i)).match(ImmutableSet.copyOf(constructor.reflect().getParameterAnnotations()[i]));
}
}
use of com.tngtech.archunit.core.domain.JavaConstructor in project ArchUnit by TNG.
the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_parameter_types.
@Test
public void automatically_resolves_constructor_parameter_types() {
@SuppressWarnings("unused")
class ConstructorParameterTypesWithoutAnyFurtherReference {
ConstructorParameterTypesWithoutAnyFurtherReference(FileSystem constructorParam1, Buffer constructorParam2) {
}
}
JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME).importClass(ConstructorParameterTypesWithoutAnyFurtherReference.class);
JavaConstructor constructor = javaClass.getConstructor(getClass(), FileSystem.class, Buffer.class);
assertThat(constructor.getRawParameterTypes().get(0)).as("constructor parameter type").isFullyImported(true);
assertThat(constructor.getRawParameterTypes().get(1)).as("constructor parameter type").isFullyImported(true);
}
use of com.tngtech.archunit.core.domain.JavaConstructor in project ArchUnit by TNG.
the class ClassFileImporterAutomaticResolutionTest method importFirstTypeArgumentConstructorParameterBound.
private static JavaType importFirstTypeArgumentConstructorParameterBound(Class<?> clazz) {
JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_PROPERTY_NAME, MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_DEFAULT_VALUE).importClass(clazz);
JavaConstructor constructor = getOnlyElement(javaClass.getConstructors());
return getFirstTypeArgumentUpperBound(constructor.getParameterTypes().get(0));
}
Aggregations