Search in sources :

Example 26 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project opentelemetry-java by open-telemetry.

the class SdkDesignTest method implementOrOverride.

static DescribedPredicate<? super JavaMethod> implementOrOverride() {
    return new DescribedPredicate<>("implement or override a method") {

        @Override
        public boolean apply(JavaMethod input) {
            List<JavaClass> params = input.getRawParameterTypes();
            Class<?>[] paramsType = new Class<?>[params.size()];
            for (int i = 0, n = params.size(); i < n; i++) {
                paramsType[i] = params.get(i).reflect();
            }
            String name = input.getName();
            List<JavaClass> parents = new ArrayList<>(input.getOwner().getAllRawSuperclasses());
            parents.addAll(input.getOwner().getAllRawInterfaces());
            for (JavaClass parent : parents) {
                Optional<JavaMethod> found = parent.tryGetMethod(name, paramsType);
                if (found.isPresent()) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArrayList(java.util.ArrayList) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaClass(com.tngtech.archunit.core.domain.JavaClass)

Example 27 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project flink by splunk.

the class Conditions method haveLeafExceptionTypes.

/**
 * Tests leaf exception types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafExceptionTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf exception types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getExceptionTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Exception leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 28 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project SORMAS-Project by hzi-braunschweig.

the class DatabaseExportServiceTest method test_all_entities_have_export_configuration.

@Test
public void test_all_entities_have_export_configuration() {
    Collection<String> exportableTables = DatabaseExportService.EXPORT_CONFIGS.values();
    Set<String> missingEntities = new HashSet<>();
    Set<String> exportedButNotWanted = new HashSet<>();
    JavaClasses classes = new ClassFileImporter().importPackages("de.symeda.sormas.backend");
    for (JavaClass clazz : classes) {
        if (clazz.isAnnotatedWith(Entity.class)) {
            Entity entityAnnotation = clazz.getAnnotationOfType(Entity.class);
            String tableName = entityAnnotation.name();
            if (StringUtils.isBlank(tableName)) {
                tableName = clazz.getSimpleName().toLowerCase();
            }
            if (!exportableTables.contains(tableName)) {
                missingEntities.add(clazz.getSimpleName());
            } else if (NOT_EXPORTED_ENTITIES.contains(clazz.reflect())) {
                exportedButNotWanted.add(clazz.getSimpleName());
            }
        }
    }
    // remove not exported entities from the list of missing ones
    NOT_EXPORTED_ENTITIES.forEach(e -> missingEntities.remove(e.getSimpleName()));
    assertThat("Missing export configuration for entities [" + String.join(", ", missingEntities) + "]", missingEntities, hasSize(0));
    assertThat("Export configuration not wanted for entities [" + String.join(", ", exportedButNotWanted) + "]", exportedButNotWanted, hasSize(0));
}
Also used : Entity(javax.persistence.Entity) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) HashSet(java.util.HashSet) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 29 with JavaClass

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

the class GivenThatIsTestedConsistentlyTest method classes_that_tests_all_relevant_methods.

@Test
public void classes_that_tests_all_relevant_methods() {
    JavaClasses classes = importClassesWithContext(GivenClassesThatTest.class, ClassesThat.class);
    JavaClass test = classes.get(GivenClassesThatTest.class);
    for (JavaMethod method : classes.get(ClassesThat.class).getMethods()) {
        assertAccessFrom(test, method);
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Test(org.junit.Test)

Example 30 with JavaClass

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

the class ArchitecturesTest method onion_architecture_with_overwritten_description_retains_ignored_dependencies.

@Test
public void onion_architecture_with_overwritten_description_retains_ignored_dependencies() {
    ArchRule onionIgnoringOriginApplicationLayerClass = getTestOnionArchitecture().ignoreDependency(equivalentTo(ApplicationLayerClass.class), DescribedPredicate.<JavaClass>alwaysTrue()).because("some reason causing description to be overwritten");
    JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));
    EvaluationResult result = onionIgnoringOriginApplicationLayerClass.evaluate(classes);
    ExpectedOnionViolations expectedViolations = getExpectedOnionViolations().withoutViolationsWithOrigin(ApplicationLayerClass.class);
    assertPatternMatches(result.getFailureReport().getDetails(), expectedViolations.toPatterns());
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9