From 6246cc63bc09d6f45fae3d070cbec8f6227e5c5c Mon Sep 17 00:00:00 2001 From: hewei Date: Fri, 7 Jul 2017 13:12:09 +0800 Subject: [PATCH] =?UTF-8?q?TableRenamePlugin=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/ExampleTargetPluginTest.java | 12 ++++ .../plugins/TableRenamePluginTest.java | 63 +++++++++++++++++++ .../generator/plugins/tools/DBHelper.java | 8 ++- .../plugins/tools/MyBatisGeneratorTool.java | 17 +++-- .../mybatis-generator-without-target.xml | 54 ++++++++++++++++ .../scripts/TableRenamePlugin/init.sql | 30 +++++++++ .../mybatis-generator-with-domainObject.xml | 53 ++++++++++++++++ ...ybatis-generator-with-wrong-properties.xml | 55 ++++++++++++++++ 8 files changed, 279 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/itfsw/mybatis/generator/plugins/TableRenamePluginTest.java create mode 100644 src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml create mode 100644 src/test/resources/scripts/TableRenamePlugin/init.sql create mode 100644 src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml create mode 100644 src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java index 97b90ba..8c52170 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java @@ -23,6 +23,8 @@ import org.junit.BeforeClass; import org.junit.Test; import org.mybatis.generator.api.GeneratedJavaFile; import org.mybatis.generator.api.MyBatisGenerator; +import org.mybatis.generator.exception.InvalidConfigurationException; +import org.mybatis.generator.exception.XMLParserException; import java.io.IOException; import java.sql.SQLException; @@ -48,6 +50,16 @@ public class ExampleTargetPluginTest { DBHelper.createDB("scripts/ExampleTargetPlugin/init.sql"); } + /** + * 测试异常配置 + */ + @Test + public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml"); + tool.generate(); + Assert.assertEquals(tool.getWarnings().get(0), "请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!"); + } + @Test public void testNormalPath() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml"); diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/TableRenamePluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/TableRenamePluginTest.java new file mode 100644 index 0000000..a2ddb55 --- /dev/null +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/TableRenamePluginTest.java @@ -0,0 +1,63 @@ +/* + * 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.DBHelper; +import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool; +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.sql.SQLException; + +/** + * --------------------------------------------------------------------------- + * + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2017/7/7 9:36 + * --------------------------------------------------------------------------- + */ +public class TableRenamePluginTest { + + /** + * 初始化 + */ + @BeforeClass + public static void init() throws Exception{ + DBHelper.createDB("scripts/TableRenamePlugin/init.sql"); + } + + /** + * 测试提示信息 + */ + @Test + public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + // searchString、replaceString 单独配置 + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml"); + tool.generate(); + Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.TableRenamePlugin插件的searchString、replaceString属性需配合使用,不能单独存在!"); + + // 和官方domainObjectName或者mapperName一起配置 + tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml"); + tool.generate(); + Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.TableRenamePlugin插件请不要配合table的domainObjectName或者mapperName一起使用!"); + } +} diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java index 5c21779..8525d88 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; @@ -53,11 +54,12 @@ public class DBHelper { /** * 创建数据库 - * * @param resource - * @throws Exception + * @throws ClassNotFoundException + * @throws SQLException + * @throws IOException */ - public static void createDB(String resource) throws Exception { + public static void createDB(String resource) throws ClassNotFoundException, SQLException, IOException { String driver = properties.getProperty("driver"); String url = properties.getProperty("url"); String username = properties.getProperty("username"); diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java index 36ac149..61275a5 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java @@ -27,6 +27,8 @@ import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.Context; import org.mybatis.generator.config.xml.ConfigurationParser; +import org.mybatis.generator.exception.InvalidConfigurationException; +import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import javax.sql.DataSource; @@ -62,11 +64,10 @@ public class MyBatisGeneratorTool { /** * 创建 - * * @param resource * @return */ - public static MyBatisGeneratorTool create(String resource) throws Exception { + public static MyBatisGeneratorTool create(String resource) throws IOException, XMLParserException { MyBatisGeneratorTool tool = new MyBatisGeneratorTool(); tool.warnings = new ArrayList<>(); @@ -80,14 +81,13 @@ public class MyBatisGeneratorTool { /** * 执行MyBatisGenerator - * * @param callback * @return * @throws SQLException * @throws IOException * @throws InterruptedException */ - public MyBatisGenerator generate(AbstractShellCallback callback) throws Exception { + public MyBatisGenerator generate(AbstractShellCallback callback) throws InvalidConfigurationException, InterruptedException, SQLException, IOException { callback.setTool(this); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null, null, null, true); @@ -96,13 +96,12 @@ public class MyBatisGeneratorTool { /** * 执行MyBatisGenerator(不生成文件) - * * @return * @throws SQLException * @throws IOException * @throws InterruptedException */ - public MyBatisGenerator generate() throws Exception { + public MyBatisGenerator generate() throws InvalidConfigurationException, InterruptedException, SQLException, IOException { MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), warnings); myBatisGenerator.generate(null, null, null, false); return myBatisGenerator; @@ -110,7 +109,6 @@ public class MyBatisGeneratorTool { /** * 编译项目并返回 SqlSession - * * @return */ public SqlSession compile() throws IOException, ClassNotFoundException { @@ -176,7 +174,6 @@ public class MyBatisGeneratorTool { /** * 获取指定后缀的文件 - * * @param file * @return */ @@ -198,10 +195,10 @@ public class MyBatisGeneratorTool { /** * 修正配置到指定target */ - private void fixConfigToTarget(){ + private void fixConfigToTarget() { this.targetProject = this.getClass().getClassLoader().getResource("").getPath(); this.targetPackage = DAO_PACKAGE + ".s" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); - for (Context context : config.getContexts()){ + for (Context context : config.getContexts()) { context.getJavaModelGeneratorConfiguration().setTargetProject(targetProject); context.getJavaModelGeneratorConfiguration().setTargetPackage(targetPackage); context.getSqlMapGeneratorConfiguration().setTargetProject(targetProject); diff --git a/src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml b/src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml new file mode 100644 index 0000000..16c8a30 --- /dev/null +++ b/src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/src/test/resources/scripts/TableRenamePlugin/init.sql b/src/test/resources/scripts/TableRenamePlugin/init.sql new file mode 100644 index 0000000..241549d --- /dev/null +++ b/src/test/resources/scripts/TableRenamePlugin/init.sql @@ -0,0 +1,30 @@ +/* +Navicat MySQL Data Transfer + +Source Server : localhost +Source Server Version : 50617 +Source Host : localhost:3306 +Source Database : mybatis-generator-plugin + +Target Server Type : MYSQL +Target Server Version : 50617 +File Encoding : 65001 + +Date: 2017-06-26 17:30:13 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for tb +-- ---------------------------- +DROP TABLE IF EXISTS `tb`; +CREATE TABLE `tb` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1', + `field1` varchar(255) DEFAULT NULL COMMENT '注释2', + PRIMARY KEY (`id`) +); + +-- ---------------------------- +-- Records of tb +-- ---------------------------- \ No newline at end of file diff --git a/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml b/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml new file mode 100644 index 0000000..f8a7eb9 --- /dev/null +++ b/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml b/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml new file mode 100644 index 0000000..250c9d8 --- /dev/null +++ b/src/test/resources/scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file