Search in sources :

Example 21 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project taskana by Taskana.

the class ArchitectureTest method notUseCurrentTimestampSqlFunction.

private static ArchCondition<JavaClass> notUseCurrentTimestampSqlFunction() {
    Function<JavaMethod, List<String>> getSqlStringsFromMethod = CheckedFunction.wrap((method) -> {
        List<String> values = new ArrayList<>();
        final Optional<Select> selectAnnotation = method.tryGetAnnotationOfType(Select.class);
        final Optional<Update> updateAnnotation = method.tryGetAnnotationOfType(Update.class);
        final Optional<Insert> insertAnnotation = method.tryGetAnnotationOfType(Insert.class);
        final Optional<Delete> deleteAnnotation = method.tryGetAnnotationOfType(Delete.class);
        final Optional<SelectProvider> selectProviderAnnotation = method.tryGetAnnotationOfType(SelectProvider.class);
        final Optional<UpdateProvider> updateProviderAnnotation = method.tryGetAnnotationOfType(UpdateProvider.class);
        final Optional<InsertProvider> insertProviderAnnotation = method.tryGetAnnotationOfType(InsertProvider.class);
        final Optional<DeleteProvider> deleteProviderAnnotation = method.tryGetAnnotationOfType(DeleteProvider.class);
        if (selectAnnotation.isPresent()) {
            values.addAll(Arrays.asList(selectAnnotation.get().value()));
        }
        if (updateAnnotation.isPresent()) {
            values.addAll(Arrays.asList(updateAnnotation.get().value()));
        }
        if (insertAnnotation.isPresent()) {
            values.addAll(Arrays.asList(insertAnnotation.get().value()));
        }
        if (deleteAnnotation.isPresent()) {
            values.addAll(Arrays.asList(deleteAnnotation.get().value()));
        }
        if (selectProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(selectProviderAnnotation.get().type(), selectProviderAnnotation.get().method()));
        }
        if (updateProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(updateProviderAnnotation.get().type(), updateProviderAnnotation.get().method()));
        }
        if (insertProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(insertProviderAnnotation.get().type(), insertProviderAnnotation.get().method()));
        }
        if (deleteProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(deleteProviderAnnotation.get().type(), deleteProviderAnnotation.get().method()));
        }
        return values;
    });
    return new ArchCondition<>("not use the SQL function 'CURRENT_TIMESTAMP'") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            for (JavaMethod method : javaClass.getAllMethods()) {
                List<String> sqlStrings = getSqlStringsFromMethod.apply(method);
                if (sqlStrings.isEmpty() && !method.tryGetAnnotationOfType(SelectProvider.class).isPresent()) {
                    String message = String.format("Method '%s#%s' does not contain any MyBatis SQL annotation", javaClass.getName(), method.getName());
                    events.add(SimpleConditionEvent.violated(javaClass, message));
                }
                if (sqlStrings.stream().anyMatch(s -> s.contains("CURRENT_TIMESTAMP"))) {
                    String message = String.format("Method '%s#%s' uses 'CURRENT_TIMESTAMP' SQL function", javaClass.getName(), method.getName());
                    events.add(SimpleConditionEvent.violated(javaClass, message));
                }
            }
        }
    };
}
Also used : Delete(org.apache.ibatis.annotations.Delete) SelectProvider(org.apache.ibatis.annotations.SelectProvider) ArchCondition(com.tngtech.archunit.lang.ArchCondition) ArrayList(java.util.ArrayList) Update(org.apache.ibatis.annotations.Update) Insert(org.apache.ibatis.annotations.Insert) DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) List(java.util.List) ArrayList(java.util.ArrayList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) InsertProvider(org.apache.ibatis.annotations.InsertProvider) UpdateProvider(org.apache.ibatis.annotations.UpdateProvider) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Select(org.apache.ibatis.annotations.Select)

Example 22 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project solarthing by wildmountainfarms.

the class ArchTest method classes_contain_both_equals_and_hash_code_or_contain_neither.

@Test
void classes_contain_both_equals_and_hash_code_or_contain_neither() {
    // TODO Note this only checks the method name, so someone could overload equals or hashCode and cause errors
    JavaClasses importedClasses = new ClassFileImporter().importPackages("me.retrodaredevil");
    for (JavaClass javaClass : importedClasses) {
        boolean overridesEquals = javaClass.getMethods().stream().anyMatch(method -> "equals".equals(method.getName()));
        boolean overridesHashCode = javaClass.getMethods().stream().anyMatch(method -> "hashCode".equals(method.getName()));
        if (overridesEquals != overridesHashCode) {
            if (overridesEquals) {
                fail(javaClass + " overrides equals but not hashCode()!");
            } else {
                fail(javaClass + " overrides hashCode() but not equals()!");
            }
        }
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test)

Example 23 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project solarthing by wildmountainfarms.

the class ArchTest method testClassesThatMustImplementEquals.

@Test
void testClassesThatMustImplementEquals() {
    JavaClasses importedClasses = new ClassFileImporter().importPackages("me.retrodaredevil.solarthing");
    Collection<JavaClass> classesToCheck = new ArrayList<>();
    // we want to be able to compare all execution reasons
    classesToCheck.addAll(importedClasses.get(ExecutionReason.class).getAllSubclasses());
    // required for OpenSourceExecutionReason
    classesToCheck.addAll(importedClasses.get(OpenSourcePacket.class).getAllSubclasses());
    classesToCheck.addAll(importedClasses.get(ActivePeriod.class).getAllSubclasses());
    // DeleteAlterPacket uses this, and DeleteAlterPacket must implement equals correctly
    classesToCheck.addAll(importedClasses.get(UpdateToken.class).getAllSubclasses());
    for (JavaClass javaClass : classesToCheck) {
        if (javaClass.isInterface()) {
            continue;
        }
        boolean doesClassOverrideEquals = javaClass.getMethods().stream().anyMatch(method -> "equals".equals(method.getName()));
        if (!doesClassOverrideEquals) {
            fail("equals is not implemented for " + javaClass);
        }
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArrayList(java.util.ArrayList) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test)

Example 24 with JavaClass

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

the class ArchUnitFixtureTest method checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes.

@Nonnull
private ConditionEvent checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes(String methodName, Class<?>... arguments) {
    ArchCondition<JavaMethod> archCondition = haveOnlyArgumentsOrReturnTypesThatAre(resideInAnyPackage("java.lang").or(primitive).or(resideInAnyPackage("java.util")).as("allowed"));
    JavaClass javaClass = new ClassFileImporter().importClass(AllowedMethodTypesClass.class);
    JavaMethod validMethod = javaClass.getMethod(methodName, arguments);
    ConditionEvents events = new ConditionEvents();
    archCondition.check(validMethod, events);
    assertThat(events).hasSize(1);
    return events.iterator().next();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Nonnull(javax.annotation.Nonnull)

Example 25 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project teammates by TEAMMATES.

the class ArchitectureTest method testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb.

@Test
public void testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb() {
    for (JavaClass coreLogicClass : forClasses(LOGIC_CORE_PACKAGE)) {
        String logicClassName = coreLogicClass.getSimpleName();
        if ("DataBundleLogic".equals(logicClassName)) {
            continue;
        }
        if (logicClassName.endsWith(TEST_FILE_SUFFIX)) {
            continue;
        }
        String dbClassName = logicClassName.replace("Logic", "Db");
        noClasses().that().resideInAPackage(includeSubpackages(LOGIC_CORE_PACKAGE)).and().doNotHaveSimpleName(logicClassName).and().doNotHaveSimpleName(logicClassName + TEST_FILE_SUFFIX).and().doNotHaveSimpleName("DataBundleLogic").should().accessClassesThat(new DescribedPredicate<>("") {

            @Override
            public boolean apply(JavaClass input) {
                return input.getPackageName().startsWith(STORAGE_API_PACKAGE) && input.getSimpleName().equals(dbClassName);
            }
        }).check(forClasses(LOGIC_CORE_PACKAGE, STORAGE_API_PACKAGE));
    }
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) Test(org.testng.annotations.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