Search in sources :

Example 1 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method notSupportParameterObjectOnMultipleArguments.

@Test
void notSupportParameterObjectOnMultipleArguments() throws NoSuchMethodException {
    try {
        Class<?> mapperType = Mapper.class;
        Method mapperMethod = mapperType.getMethod("getUsersByName", String.class, String.class);
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod).getBoundSql(new Object());
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error invoking SqlProvider method 'public java.lang.String org.apache.ibatis.submitted.sqlprovider.OurSqlBuilder.buildGetUsersByNameQuery(java.lang.String,java.lang.String)' with specify parameter 'class java.lang.Object'.  Cause: java.lang.IllegalArgumentException: wrong number of arguments"));
    }
}
Also used : 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)

Example 2 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method methodOverload.

@Test
void methodOverload() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("methodOverload", String.class);
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. Method 'overload' is found multiple in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorSqlBuilder'. Sql provider method can not overload."));
    }
}
Also used : SelectProvider(org.apache.ibatis.annotations.SelectProvider) 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)

Example 3 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method omitTypeWhenSpecifyDefaultType.

@Test
void omitTypeWhenSpecifyDefaultType() throws NoSuchMethodException {
    Class<?> mapperType = DefaultSqlProviderMapper.class;
    Configuration configuration = new Configuration();
    configuration.setDefaultSqlProviderType(DefaultSqlProviderMapper.SqlProvider.class);
    {
        Method mapperMethod = mapperType.getMethod("select", int.class);
        String sql = new ProviderSqlSource(configuration, mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod).getBoundSql(1).getSql();
        assertEquals("select name from foo where id = ?", sql);
    }
    {
        Method mapperMethod = mapperType.getMethod("insert", String.class);
        String sql = new ProviderSqlSource(configuration, mapperMethod.getAnnotation(InsertProvider.class), mapperType, mapperMethod).getBoundSql("Taro").getSql();
        assertEquals("insert into foo (name) values(?)", sql);
    }
    {
        Method mapperMethod = mapperType.getMethod("update", int.class, String.class);
        String sql = new ProviderSqlSource(configuration, mapperMethod.getAnnotation(UpdateProvider.class), mapperType, mapperMethod).getBoundSql(Collections.emptyMap()).getSql();
        assertEquals("update foo set name = ? where id = ?", sql);
    }
    {
        Method mapperMethod = mapperType.getMethod("delete", int.class);
        String sql = new ProviderSqlSource(configuration, mapperMethod.getAnnotation(DeleteProvider.class), mapperType, mapperMethod).getBoundSql(Collections.emptyMap()).getSql();
        assertEquals("delete from foo where id = ?", sql);
    }
}
Also used : 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)

Example 4 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method invokeError.

@Test
void invokeError() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("invokeError");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod).getBoundSql(new Object());
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error invoking SqlProvider method 'public java.lang.String org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorSqlBuilder.invokeError()' with specify parameter 'class java.lang.Object'.  Cause: java.lang.UnsupportedOperationException: invokeError"));
    }
}
Also used : 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)

Example 5 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method multipleProviderContext.

@Test
void multipleProviderContext() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("multipleProviderContext");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. ProviderContext found multiple in SqlProvider method (org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorSqlBuilder.multipleProviderContext). ProviderContext can not define multiple in SqlProvider method argument."));
    }
}
Also used : SelectProvider(org.apache.ibatis.annotations.SelectProvider) 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

MapperMethod (org.apache.ibatis.binding.MapperMethod)37 Method (java.lang.reflect.Method)36 BaseDataTest (org.apache.ibatis.BaseDataTest)35 ProviderSqlSource (org.apache.ibatis.builder.annotation.ProviderSqlSource)35 Configuration (org.apache.ibatis.session.Configuration)35 Test (org.junit.jupiter.api.Test)35 BuilderException (org.apache.ibatis.builder.BuilderException)30 SelectProvider (org.apache.ibatis.annotations.SelectProvider)12 DeleteProvider (org.apache.ibatis.annotations.DeleteProvider)3 SqlSession (org.apache.ibatis.session.SqlSession)1