Search in sources :

Example 11 with JavaConstructor

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

the class ClassFileImporterCodeUnitReferencesTest method creates_correct_description_for_references.

@Test
public void creates_correct_description_for_references() {
    JavaClasses javaClasses = new ClassFileImporter().importClasses(Origin.class, Target.class);
    JavaClass originClass = javaClasses.get(Origin.class);
    JavaClass targetClass = javaClasses.get(Target.class);
    JavaMethod constructorReferenceOrigin = originClass.getMethod("referencesConstructor");
    JavaConstructor targetConstructor = targetClass.getConstructor();
    JavaMethod methodReferenceOrigin = originClass.getMethod("referencesMethod");
    JavaMethod targetMethod = targetClass.getMethod("call");
    JavaConstructorReference constructorReference = getOnlyElement(constructorReferenceOrigin.getConstructorReferencesFromSelf());
    assertThat(constructorReference.getDescription()).isEqualTo(String.format("Method <%s> references constructor <%s> in (%s.java:%d)", constructorReferenceOrigin.getFullName(), targetConstructor.getFullName(), Origin.class.getSimpleName(), 9));
    JavaMethodReference methodReference = getOnlyElement(originClass.getMethod("referencesMethod").getMethodReferencesFromSelf());
    assertThat(methodReference.getDescription()).isEqualTo(String.format("Method <%s> references method <%s> in (%s.java:%d)", methodReferenceOrigin.getFullName(), targetMethod.getFullName(), Origin.class.getSimpleName(), 13));
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 12 with JavaConstructor

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

the class ClassFileImporterMembersTest method imports_constructor_with_correct_throws_declarations.

@Test
public void imports_constructor_with_correct_throws_declarations() {
    JavaClass clazz = new ClassFileImporter().importUrl(getClass().getResource("testexamples/constructorimport")).get(ClassWithThrowingConstructor.class);
    JavaConstructor constructor = getOnlyElement(clazz.getConstructors());
    assertThatThrowsClause(constructor.getThrowsClause()).as("Throws types of sole constructor").matches(FirstCheckedException.class, SecondCheckedException.class);
    assertThatTypes(constructor.getExceptionTypes()).matchExactly(FirstCheckedException.class, SecondCheckedException.class);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 13 with JavaConstructor

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

the class ClassFileImporterGenericCodeUnitParameterTypesTest method imports_raw_parameter_types_and_generic_parameter_types_of_inner_class_constructor_like_Reflection_API.

@Test
public void imports_raw_parameter_types_and_generic_parameter_types_of_inner_class_constructor_like_Reflection_API() {
    @SuppressWarnings("unused")
    class LocalClassThatWillHaveEnclosingClassAsFirstRawConstructorParameter {

        public LocalClassThatWillHaveEnclosingClassAsFirstRawConstructorParameter(ClassParameterWithSingleTypeParameter<String> param) {
        }
    }
    JavaConstructor constructor = new ClassFileImporter().importClass(LocalClassThatWillHaveEnclosingClassAsFirstRawConstructorParameter.class).getConstructor(getClass(), ClassParameterWithSingleTypeParameter.class);
    assertThatTypes(constructor.getRawParameterTypes()).matchExactly(getClass(), ClassParameterWithSingleTypeParameter.class);
    assertThat(constructor.getParameterTypes()).hasSize(1);
    assertThatType(constructor.getParameterTypes().get(0)).matches(parameterizedType(ClassParameterWithSingleTypeParameter.class).withTypeArguments(String.class));
}
Also used : JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Aggregations

JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)13 Test (org.junit.Test)11 JavaClass (com.tngtech.archunit.core.domain.JavaClass)7 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 ArchCondition (com.tngtech.archunit.lang.ArchCondition)2 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)2 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)1 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)1 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)1 SimpleAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation)1 ClassWithAnnotatedMethods (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods)1 ClassWithComplexTryCatchBlocks (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks)1