Search in sources :

Example 1 with DeleteProvider

use of org.apache.ibatis.annotations.DeleteProvider 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<JavaClass>("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 2 with DeleteProvider

use of org.apache.ibatis.annotations.DeleteProvider in project javaBook-src by huifer.

the class SqlProviderTest method differentTypeAndValue.

@Test
void differentTypeAndValue() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("differentTypeAndValue");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(DeleteProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Cannot specify different class on 'value' and 'type' attribute of @DeleteProvider at the 'public abstract void org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorMapper.differentTypeAndValue()'."));
    }
}
Also used : DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) BuilderException(org.apache.ibatis.builder.BuilderException) Configuration(org.apache.ibatis.session.Configuration) MapperMethod(org.apache.ibatis.binding.MapperMethod) Method(java.lang.reflect.Method) ProviderSqlSource(org.apache.ibatis.builder.annotation.ProviderSqlSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 3 with DeleteProvider

use of org.apache.ibatis.annotations.DeleteProvider in project mybatis-3 by mybatis.

the class SqlProviderTest method differentTypeAndValue.

@Test
void differentTypeAndValue() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("differentTypeAndValue");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(DeleteProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Cannot specify different class on 'value' and 'type' attribute of @DeleteProvider at the 'public abstract void org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorMapper.differentTypeAndValue()'."));
    }
}
Also used : DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) BuilderException(org.apache.ibatis.builder.BuilderException) Configuration(org.apache.ibatis.session.Configuration) Method(java.lang.reflect.Method) MapperMethod(org.apache.ibatis.binding.MapperMethod) ProviderSqlSource(org.apache.ibatis.builder.annotation.ProviderSqlSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteProvider (org.apache.ibatis.annotations.DeleteProvider)3 Method (java.lang.reflect.Method)2 BaseDataTest (org.apache.ibatis.BaseDataTest)2 MapperMethod (org.apache.ibatis.binding.MapperMethod)2 BuilderException (org.apache.ibatis.builder.BuilderException)2 ProviderSqlSource (org.apache.ibatis.builder.annotation.ProviderSqlSource)2 Configuration (org.apache.ibatis.session.Configuration)2 Test (org.junit.jupiter.api.Test)2 JavaClass (com.tngtech.archunit.core.domain.JavaClass)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 ArchCondition (com.tngtech.archunit.lang.ArchCondition)1 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Delete (org.apache.ibatis.annotations.Delete)1 Insert (org.apache.ibatis.annotations.Insert)1 InsertProvider (org.apache.ibatis.annotations.InsertProvider)1 Select (org.apache.ibatis.annotations.Select)1 SelectProvider (org.apache.ibatis.annotations.SelectProvider)1 Update (org.apache.ibatis.annotations.Update)1