Skip to content

Commit 59a175b

Browse files
author
hewei
committed
EnumTypeStatusPlugin 和 测试用例
1 parent 5d219ad commit 59a175b

File tree

7 files changed

+220
-11
lines changed

7 files changed

+220
-11
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/EnumTypeStatusPlugin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class EnumTypeStatusPlugin extends BasePlugin {
4545
* 需要生成Enum的Column
4646
*/
4747
public final static String PRO_ENUM_COLUMNS = "enumColumns";
48-
public final static String REMARKS_PATTERN = ".*\\[(\\w+\\([\\u4e00-\\u9fa5_a-zA-Z0-9]+\\):[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\,?\\s*)+\\].*";
49-
public final static String NEED_PATTERN = "\\[((\\w+\\([\\u4e00-\\u9fa5_a-zA-Z0-9]+\\):[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\,?\\s*)+)\\]";
50-
public final static String ITEM_PATTERN = "(\\w+)\\(([\\u4e00-\\u9fa5_a-zA-Z0-9]+)\\):([\\u4e00-\\u9fa5_a-zA-Z0-9]+)";
48+
public final static String REMARKS_PATTERN = ".*\\s*\\[\\s*(\\w+\\s*\\(\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\)\\s*:\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\,?\\s*)+\\s*\\]\\s*.*";
49+
public final static String NEED_PATTERN = "\\[\\s*((\\w+\\s*\\(\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\)\\s*:\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\,?\\s*)+)\\s*\\]";
50+
public final static String ITEM_PATTERN = "(\\w+)\\s*\\(\\s*([\\u4e00-\\u9fa5_a-zA-Z0-9]+)\\s*\\)\\s*:\\s*([\\u4e00-\\u9fa5_a-zA-Z0-9]+)";
5151
private Map<IntrospectedColumn, List<EnumItemInfo>> enumColumns;
5252

5353
/**
@@ -237,7 +237,7 @@ public EnumItemInfo(IntrospectedColumn column, String name, String comment, Stri
237237
* @author hewei
238238
*/
239239
public String getComment() {
240-
return "\"" + comment + "\"";
240+
return "\"" + comment.trim() + "\"";
241241
}
242242

243243
/**

src/test/java/com/itfsw/mybatis/generator/plugins/EnumTypeStatusPluginTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback;
2020
import com.itfsw.mybatis.generator.plugins.tools.DBHelper;
2121
import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool;
22+
import com.itfsw.mybatis.generator.plugins.tools.ObjectUtil;
2223
import org.apache.ibatis.session.SqlSession;
24+
import org.junit.Assert;
2325
import org.junit.BeforeClass;
2426
import org.junit.Test;
2527

@@ -43,6 +45,25 @@ public static void init() throws SQLException, IOException {
4345
DBHelper.createDB("scripts/EnumTypeStatusPlugin/init.sql");
4446
}
4547

48+
/**
49+
* 测试配置异常
50+
*/
51+
@Test
52+
public void testWarnings() throws Exception {
53+
// 1. 注释格式不对
54+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/EnumTypeStatusPlugin/mybatis-generator-with-wrong-comment.xml");
55+
tool.generate();
56+
57+
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "没有找到column为field2对应格式的注释的字段!");
58+
59+
60+
// 2. 不支持的类型
61+
tool = MyBatisGeneratorTool.create("scripts/EnumTypeStatusPlugin/mybatis-generator-with-unsupport-type.xml");
62+
tool.generate();
63+
64+
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为field2对应Java类型不是Short、Integer、Long、String!");
65+
}
66+
4667
/**
4768
* 测试生成的enum
4869
* @throws Exception
@@ -53,7 +74,48 @@ public void testEnum() throws Exception{
5374
tool.generate(new AbstractShellCallback() {
5475
@Override
5576
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
77+
// 1. 测试标准注释
78+
ObjectUtil enumField2Success = new ObjectUtil(loader, packagz + ".Tb$Field2#SUCCESS");
79+
Assert.assertEquals(enumField2Success.invoke("value"), (short)0);
80+
Assert.assertEquals(enumField2Success.invoke("getValue"), (short)0);
81+
Assert.assertEquals(enumField2Success.invoke("getName"), "禁用");
82+
83+
ObjectUtil enumField2FailType = new ObjectUtil(loader, packagz + ".Tb$Field2#FAIL_TYPE");
84+
Assert.assertEquals(enumField2FailType.invoke("value"), (short)1);
85+
Assert.assertEquals(enumField2FailType.invoke("getValue"), (short)1);
86+
Assert.assertEquals(enumField2FailType.invoke("getName"), "启用");
87+
88+
// 2. 字符串类型的
89+
ObjectUtil enumField3StrSuccess = new ObjectUtil(loader, packagz + ".Tb$Field3Str#SUCCESS");
90+
Assert.assertEquals(enumField3StrSuccess.invoke("value"), "成都");
91+
Assert.assertEquals(enumField3StrSuccess.invoke("getValue"), "成都");
92+
Assert.assertEquals(enumField3StrSuccess.invoke("getName"), "禁用");
93+
94+
// 3. 全局支持
95+
ObjectUtil enumStatusSuccess = new ObjectUtil(loader, packagz + ".Tb$Status#SUCCESS");
96+
Assert.assertEquals(enumStatusSuccess.invoke("value"), (short)0);
97+
Assert.assertEquals(enumStatusSuccess.invoke("getValue"), (short)0);
98+
Assert.assertEquals(enumStatusSuccess.invoke("getName"), "禁用");
99+
100+
// 4. 特殊格式的注释
101+
ObjectUtil enumTypeSuccess = new ObjectUtil(loader, packagz + ".Tb$Type#SUCCESS");
102+
Assert.assertEquals(enumTypeSuccess.invoke("value"), 0L);
103+
Assert.assertEquals(enumTypeSuccess.invoke("getValue"), 0L);
104+
Assert.assertEquals(enumTypeSuccess.invoke("getName"), "禁用");
105+
ObjectUtil enumTypeFailType = new ObjectUtil(loader, packagz + ".Tb$Type#FAIL_TYPE");
106+
Assert.assertEquals(enumTypeFailType.invoke("value"), 1L);
107+
Assert.assertEquals(enumTypeFailType.invoke("getValue"), 1L);
108+
Assert.assertEquals(enumTypeFailType.invoke("getName"), "启用");
56109

110+
// 5. 有换行的
111+
ObjectUtil enumBreakLineSuccess = new ObjectUtil(loader, packagz + ".Tb$BreakLine#SUCCESS");
112+
Assert.assertEquals(enumBreakLineSuccess.invoke("value"), 0L);
113+
Assert.assertEquals(enumBreakLineSuccess.invoke("getValue"), 0L);
114+
Assert.assertEquals(enumBreakLineSuccess.invoke("getName"), "禁用");
115+
ObjectUtil enumBreakLineFailType = new ObjectUtil(loader, packagz + ".Tb$BreakLine#FAIL_TYPE");
116+
Assert.assertEquals(enumBreakLineFailType.invoke("value"), 1L);
117+
Assert.assertEquals(enumBreakLineFailType.invoke("getValue"), 1L);
118+
Assert.assertEquals(enumBreakLineFailType.invoke("getName"), "启用");
57119
}
58120
});
59121
}

src/test/java/com/itfsw/mybatis/generator/plugins/tools/ObjectUtil.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ObjectUtil {
4040
* @param loader
4141
* @param cls
4242
*/
43-
public ObjectUtil(ClassLoader loader, String cls) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
43+
public ObjectUtil(ClassLoader loader, String cls) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
4444
if (cls.indexOf("#") == -1) {
4545
this.cls = loader.loadClass(cls);
4646
this.object = this.cls.newInstance();
@@ -51,12 +51,11 @@ public ObjectUtil(ClassLoader loader, String cls) throws ClassNotFoundException,
5151
Object[] constants = this.cls.getEnumConstants();
5252
for (Object object : constants) {
5353
ObjectUtil eObject = new ObjectUtil(object);
54-
if (strs[1].equals(eObject.get("name"))) {
54+
if (strs[1].equals(eObject.invoke("name"))) {
5555
this.object = object;
5656
break;
5757
}
5858
}
59-
System.out.println("");
6059
} else {
6160
throw new ClassNotFoundException("没有找到对应枚举" + strs[0]);
6261
}

src/test/resources/scripts/EnumTypeStatusPlugin/init.sql

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,44 @@ SET FOREIGN_KEY_CHECKS=0;
2121
DROP TABLE IF EXISTS `tb`;
2222
CREATE TABLE `tb` (
2323
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
24-
`field2` smallint(3) COMMENT '注释[success(0):禁用, fail_type(1):启用]发士大夫大事发生的',
25-
`field3` varchar(255) COMMENT '注释111[success(成都):禁用, type(成都11):启用]'
24+
`field2` smallint(3) COMMENT '注释[success(0):禁用, fail_type(1):启用]',
25+
`field3_str` varchar(255) COMMENT '注释111[success(成都):禁用, type(成都11):启用]',
26+
`status` smallint(3) COMMENT '注释[success(0):禁用, fail(1):启用]',
27+
`type` bigint(20) COMMENT '注释 [ success ( 0 ) : 禁用 , fail_type ( 1 ) : 启用 ] 阿斯顿覅就就',
28+
`break_line` bigint(20) COMMENT '换行的注释
29+
[
30+
success ( 0 ) : 禁用 ,
31+
fail_type ( 1 ) : 启用
32+
]
33+
发士大夫撒旦法'
2634
);
2735

2836
-- ----------------------------
2937
-- Records of tb
38+
-- ----------------------------
39+
40+
-- ----------------------------
41+
-- Table structure for tb_unsupport_type
42+
-- ----------------------------
43+
DROP TABLE IF EXISTS `tb_unsupport_type`;
44+
CREATE TABLE `tb_unsupport_type` (
45+
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
46+
`field2` blob COMMENT '注释[success(0):禁用, fail_type(1):启用]发士大夫大事发生的'
47+
);
48+
49+
-- ----------------------------
50+
-- Records of tb_unsupport_type
51+
-- ----------------------------
52+
53+
-- ----------------------------
54+
-- Table structure for tb_wrong_comment
55+
-- ----------------------------
56+
DROP TABLE IF EXISTS `tb_wrong_comment`;
57+
CREATE TABLE `tb_wrong_comment` (
58+
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
59+
`field2` smallint(3) COMMENT '注释success(0):禁用, fail_type(1):启用]发士大夫大事发生的'
60+
);
61+
62+
-- ----------------------------
63+
-- Records of tb_wrong_comment
3064
-- ----------------------------
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2018.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
24+
<!--导入属性配置 -->
25+
<context id="default" targetRuntime="MyBatis3">
26+
<!-- 插件 -->
27+
<plugin type="com.itfsw.mybatis.generator.plugins.EnumTypeStatusPlugin"/>
28+
29+
<commentGenerator>
30+
<property name="addRemarkComments" value="true"/>
31+
</commentGenerator>
32+
<!--jdbc的数据库连接 -->
33+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
34+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
35+
targetPackage 指定生成的model生成所在的包名
36+
targetProject 指定在该项目下所在的路径 -->
37+
<javaModelGenerator targetPackage="" targetProject="">
38+
<!-- 是否对model添加 构造函数 -->
39+
<property name="constructorBased" value="true"/>
40+
<!-- 给Model添加一个父类 -->
41+
<!--<property name="rootClass" value="com.itfsw.base"/>-->
42+
</javaModelGenerator>
43+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
44+
<sqlMapGenerator targetPackage="" targetProject="" />
45+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
46+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
47+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
48+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
49+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
50+
51+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
52+
<table tableName="tb_unsupport_type">
53+
<property name="enumColumns" value="field2"/>
54+
</table>
55+
</context>
56+
</generatorConfiguration>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2018.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
24+
<!--导入属性配置 -->
25+
<context id="default" targetRuntime="MyBatis3">
26+
<!-- 插件 -->
27+
<plugin type="com.itfsw.mybatis.generator.plugins.EnumTypeStatusPlugin"/>
28+
29+
<commentGenerator>
30+
<property name="addRemarkComments" value="true"/>
31+
</commentGenerator>
32+
<!--jdbc的数据库连接 -->
33+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
34+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
35+
targetPackage 指定生成的model生成所在的包名
36+
targetProject 指定在该项目下所在的路径 -->
37+
<javaModelGenerator targetPackage="" targetProject="">
38+
<!-- 是否对model添加 构造函数 -->
39+
<property name="constructorBased" value="true"/>
40+
<!-- 给Model添加一个父类 -->
41+
<!--<property name="rootClass" value="com.itfsw.base"/>-->
42+
</javaModelGenerator>
43+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
44+
<sqlMapGenerator targetPackage="" targetProject="" />
45+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
46+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
47+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
48+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
49+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
50+
51+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
52+
<table tableName="tb_wrong_comment">
53+
<property name="enumColumns" value="field2"/>
54+
</table>
55+
</context>
56+
</generatorConfiguration>

src/test/resources/scripts/EnumTypeStatusPlugin/mybatis-generator.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
<!--导入属性配置 -->
2525
<context id="default" targetRuntime="MyBatis3">
2626
<!-- 插件 -->
27-
<plugin type="com.itfsw.mybatis.generator.plugins.EnumTypeStatusPlugin"/>
27+
<plugin type="com.itfsw.mybatis.generator.plugins.EnumTypeStatusPlugin">
28+
<property name="enumColumns" value="type, status"/>
29+
</plugin>
2830

2931
<commentGenerator>
3032
<property name="addRemarkComments" value="true"/>
@@ -50,7 +52,7 @@
5052

5153
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
5254
<table tableName="tb">
53-
<property name="enumColumns" value="field2, field3"/>
55+
<property name="enumColumns" value="field2, field3_str, break_line"/>
5456
</table>
5557
</context>
5658
</generatorConfiguration>

0 commit comments

Comments
 (0)