miemie 5 年之前
父節點
當前提交
ffdea88b1e

+ 119 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/BaseDbTest.java

@@ -0,0 +1,119 @@
+package com.baomidou.mybatisplus.test;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import org.apache.ibatis.builder.xml.XMLMapperBuilder;
+import org.apache.ibatis.logging.slf4j.Slf4jImpl;
+import org.apache.ibatis.mapping.Environment;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
+import org.h2.Driver;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.SimpleDriverDataSource;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * @author miemie
+ * @since 2020-06-23
+ */
+public abstract class BaseDbTest {
+
+    protected SqlSessionFactory sqlSessionFactory;
+
+    public BaseDbTest() {
+        DataSource ds = dataSource();
+        List<String> tableSql = tableSql();
+        String tableDataSql = tableDataSql();
+        String mapperXml = mapperXml();
+        GlobalConfig globalConfig = globalConfig();
+        List<Interceptor> interceptors = interceptors();
+        List<Class<?>> mappers = mappers();
+
+        JdbcTemplate template = new JdbcTemplate(ds);
+        if (CollectionUtils.isNotEmpty(tableSql)) {
+            for (String sql : tableSql) {
+                if (StringUtils.isNotBlank(sql)) {
+                    template.execute(sql);
+                }
+            }
+        }
+
+        if (StringUtils.isNotBlank(tableDataSql)) {
+            template.execute(tableDataSql);
+        }
+        MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
+        Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
+        MybatisConfiguration configuration = new MybatisConfiguration(environment);
+        GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
+        configuration.setLogImpl(Slf4jImpl.class);
+        if (StringUtils.isNotBlank(mapperXml)) {
+            ClassPathResource resource = new ClassPathResource(mapperXml);
+            try {
+                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resource.getInputStream(),
+                    configuration, resource.toString(), configuration.getSqlFragments());
+                xmlMapperBuilder.parse();
+            } catch (IOException e) {
+                throw ExceptionUtils.mpe(e);
+            }
+        }
+        mappers.forEach(configuration::addMapper);
+        if (CollectionUtils.isNotEmpty(interceptors)) {
+            interceptors.forEach(configuration::addInterceptor);
+        }
+        sqlSessionFactory = builder.build(configuration);
+    }
+
+    private DataSource dataSource() {
+        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
+        dataSource.setDriver(new Driver());
+        dataSource.setUrl("jdbc:h2:mem:test;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
+        dataSource.setUsername("sa");
+        dataSource.setPassword("");
+        return dataSource;
+    }
+
+    protected <T> void doTest(Class<T> mapper, Consumer<T> consumer) {
+        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
+            doTest(sqlSession, mapper, consumer);
+        }
+    }
+
+    protected <T> void doTest(SqlSession sqlSession, Class<T> mapper, Consumer<T> consumer) {
+        T t = sqlSession.getMapper(mapper);
+        consumer.accept(t);
+    }
+
+    protected List<String> tableSql() {
+        return null;
+    }
+
+    protected String tableDataSql() {
+        return null;
+    }
+
+    protected String mapperXml() {
+        return null;
+    }
+
+    protected List<Interceptor> interceptors() {
+        return null;
+    }
+
+    protected abstract List<Class<?>> mappers();
+
+    protected GlobalConfig globalConfig() {
+        return GlobalConfigUtils.defaults();
+    }
+}

+ 18 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/Entity.java

@@ -0,0 +1,18 @@
+package com.baomidou.mybatisplus.test.pagination;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author miemie
+ * @since 2020-06-23
+ */
+@Data
+public class Entity implements Serializable {
+    private static final long serialVersionUID = 6962439201546719734L;
+
+    private Long id;
+
+    private String name;
+}

+ 12 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/EntityMapper.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.pagination;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.CacheNamespace;
+
+/**
+ * @author miemie
+ * @since 2020-06-23
+ */
+@CacheNamespace
+public interface EntityMapper extends BaseMapper<Entity> {
+}

+ 69 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/PaginationTest.java

@@ -0,0 +1,69 @@
+package com.baomidou.mybatisplus.test.pagination;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.test.BaseDbTest;
+import org.apache.ibatis.plugin.Interceptor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author miemie
+ * @since 2020-06-23
+ */
+public class PaginationTest extends BaseDbTest {
+
+    @Test
+    void page() {
+        doTest(EntityMapper.class, m -> {
+            Page<Entity> page = new Page<>(1, 5);
+            IPage<Entity> result = m.selectPage(page, null);
+            assertThat(page).isEqualTo(result);
+            assertThat(result.getTotal()).isEqualTo(2L);
+            assertThat(result.getRecords().size()).isEqualTo(2);
+        });
+
+        doTest(EntityMapper.class, m -> {
+            Page<Entity> page = new Page<>(1, 5);
+            IPage<Entity> result = m.selectPage(page, null);
+            assertThat(page).isEqualTo(result);
+            assertThat(result.getTotal()).isEqualTo(2L);
+            assertThat(result.getRecords().size()).isEqualTo(2);
+        });
+    }
+
+    @Override
+    protected List<Interceptor> interceptors() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
+        return Collections.singletonList(interceptor);
+    }
+
+    @Override
+    protected List<Class<?>> mappers() {
+        return Collections.singletonList(EntityMapper.class);
+    }
+
+    @Override
+    protected String tableDataSql() {
+        return "insert into entity(id,name) values(1,'1');\n" +
+            "insert into entity(id,name) values(2,'2');";
+    }
+
+    @Override
+    protected List<String> tableSql() {
+        return Arrays.asList("drop table if exists entity",
+            "CREATE TABLE IF NOT EXISTS entity (\n" +
+                "id BIGINT(20) NOT NULL,\n" +
+                "name VARCHAR(30) NULL DEFAULT NULL,\n" +
+                "PRIMARY KEY (id)" +
+                ")");
+    }
+}