com.itfsw.mybatis.generator.plugins.BatchInsertPluginTest.java Source code

Java tutorial

Introduction

Here is the source code for com.itfsw.mybatis.generator.plugins.BatchInsertPluginTest.java

Source

/*
 * Copyright (c) 2017.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.itfsw.mybatis.generator.plugins;

import com.itfsw.mybatis.generator.plugins.tools.*;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * ---------------------------------------------------------------------------
 *
 * ---------------------------------------------------------------------------
 * @author: hewei
 * @time:2017/6/26 18:23
 * ---------------------------------------------------------------------------
 */
public class BatchInsertPluginTest {

    /**
     * ?
     * @throws IOException
     * @throws SQLException
     */
    @BeforeClass
    public static void init() throws Exception {
        DBHelper.createDB("scripts/BatchInsertPlugin/init.sql");
    }

    /**
     * ??
     * @throws IOException
     * @throws XMLParserException
     * @throws InvalidConfigurationException
     * @throws SQLException
     * @throws InterruptedException
     */
    @Test
    public void testWarnings1() throws Exception {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool
                .create("scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml");
        tool.generate();

        Assert.assertTrue(tool.getWarnings().size() == 2);
        Assert.assertEquals(tool.getWarnings().get(0),
                "itfsw:?com.itfsw.mybatis.generator.plugins.BatchInsertPlugin???com.itfsw.mybatis.generator.plugins.ModelColumnPlugin??");
    }

    /**
     * ?driver
     * @throws IOException
     * @throws XMLParserException
     * @throws InvalidConfigurationException
     * @throws SQLException
     * @throws InterruptedException
     */
    @Test
    public void testWarnings2() throws Exception {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool
                .create("scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml");
        tool.generate();

        Assert.assertTrue(tool.getWarnings().size() == 3);
        Assert.assertEquals(tool.getWarnings().get(1),
                "itfsw:?com.itfsw.mybatis.generator.plugins.BatchInsertPlugin?????MySQLSQLserverJDBCgetGenereatedKeys??");
    }

    /**
     * ?
     * @throws IOException
     * @throws XMLParserException
     * @throws InvalidConfigurationException
     * @throws SQLException
     * @throws InterruptedException
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     */
    @Test
    public void testMethods() throws Exception {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml");
        tool.generate(new AbstractShellCallback() {
            @Override
            public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
                // 1. Mapper?ListModel
                Class clsTbMapper = loader.loadClass(packagz + ".TbMapper");
                int count = 0;
                for (Method method : clsTbMapper.getDeclaredMethods()) {
                    if (method.getName().equals("batchInsert")) {
                        Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]),
                                packagz + ".Tb");
                        count++;
                    }
                    if (method.getName().equals("batchInsertSelective")) {
                        Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]),
                                packagz + ".Tb");
                        // TODO java8 getName . java 7 toString()
                        Assert.assertEquals(method.getGenericParameterTypes()[1].toString(),
                                packagz + ".Tb$Column[]");
                        count++;
                    }
                }
                Assert.assertEquals(count, 2);

                // 2. WithBlobs
                Class clsTbBlobsMapper = loader.loadClass(packagz + ".TbBlobsMapper");
                count = 0;
                for (Method method : clsTbBlobsMapper.getDeclaredMethods()) {
                    if (method.getName().equals("batchInsert")) {
                        Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]),
                                packagz + ".TbBlobsWithBLOBs");
                        count++;
                    }
                    if (method.getName().equals("batchInsertSelective")) {
                        Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]),
                                packagz + ".TbBlobsWithBLOBs");
                        // TODO java8 getName . java 7 toString()
                        Assert.assertEquals(method.getGenericParameterTypes()[1].toString(),
                                packagz + ".TbBlobsWithBLOBs$Column[]");
                        count++;
                    }
                }
                Assert.assertEquals(count, 2);
            }
        });
    }

    /**
     * ?sql
     */
    @Test
    public void testBatchInsert() throws Exception {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml");
        tool.generate(new AbstractShellCallback() {
            @Override
            public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
                // 1. sql
                ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
                List<Object> params = new ArrayList<>();
                params.add(new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").getObject());
                params.add(
                        new ObjectUtil(loader, packagz + ".Tb").set("field1", "test").set("field2", 1).getObject());
                String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "batchInsert", params);
                Assert.assertEquals(sql, "insert into tb (field1, field2) values ('test', null) , ('test', 1)");
                // 2. sql
                Object count = tbMapper.invoke("batchInsert", params);
                Assert.assertEquals(count, 2);
            }
        });
    }

    /**
     * ?sql
     */
    @Test
    public void testBatchInsertSelective() throws Exception {
        MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml");
        tool.generate(new AbstractShellCallback() {
            @Override
            public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
                // 1. sql
                ObjectUtil tbBlobsMapper = new ObjectUtil(
                        sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
                List<Object> params = new ArrayList<>();
                params.add(new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs").set("field1", "test").getObject());
                params.add(new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs").set("field1", "test")
                        .set("field2", "test123").getObject());
                ObjectUtil columnField2 = new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs$Column#field2");
                // java ???????Array!!!???invoke????
                Object columns = Array.newInstance(columnField2.getCls(), 1);
                Array.set(columns, 0, columnField2.getObject());

                String sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "batchInsertSelective", params,
                        columns);
                Assert.assertEquals(sql, "insert into tb_blobs ( field2 ) values ( 'null' ) , ( 'test123' )");
                // 2. sql
                Object count = tbBlobsMapper.invoke("batchInsertSelective", params, columns);
                Assert.assertEquals(count, 2);
            }
        });
    }
}