diff --git a/.gitignore b/.gitignore index 7fb9ef6..d89b17c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ hs_err_pid* .idea/ mybatis-generator-plugin.iml -target/ \ No newline at end of file +target/ diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java index 06d8737..b2fd1a9 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java @@ -63,6 +63,7 @@ public class TemplateCommentGenerator implements CommentGenerator { if (useForDefault){ InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath); doc = new SAXReader().read(inputStream); + inputStream.close(); } else { File file = new File(templatePath); if (file.exists()) { diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/CommentPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/CommentPluginTest.java index 9bb368b..2630ef4 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/CommentPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/CommentPluginTest.java @@ -16,6 +16,27 @@ package com.itfsw.mybatis.generator.plugins; +import com.itfsw.mybatis.generator.plugins.tools.DBHelper; +import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool; +import com.itfsw.mybatis.generator.plugins.tools.ObjectUtil; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mybatis.generator.api.GeneratedJavaFile; +import org.mybatis.generator.api.MyBatisGenerator; +import org.mybatis.generator.api.dom.java.Field; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.TopLevelClass; +import org.mybatis.generator.api.dom.xml.Document; +import org.mybatis.generator.api.dom.xml.Element; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.config.MergeConstants; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + /** * --------------------------------------------------------------------------- * @@ -25,4 +46,63 @@ package com.itfsw.mybatis.generator.plugins; * --------------------------------------------------------------------------- */ public class CommentPluginTest { + /** + * 初始化 + */ + @BeforeClass + public static void init() throws Exception { + DBHelper.createDB("scripts/ComentPlugin/init.sql"); + } + + /** + * 测试没有配置模板的情况 + */ + @Test + public void testGenerateWithoutTemplate() throws Exception { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ComentPlugin/mybatis-generator-without-template.xml"); + MyBatisGenerator myBatisGenerator = tool.generate(); + // 是否在使用系统默认模板 + int count = 0; + for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) { + if (file.getFormattedContent().indexOf("@project https://github.com/itfsw/mybatis-generator-plugin") != -1) { + count++; + } + } + Assert.assertTrue(count > 0); + } + + /** + * 测试配置了模板参数转换 + */ + @Test + public void testGenerateWithTemplate() throws Exception { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ComentPlugin/mybatis-generator.xml"); + MyBatisGenerator myBatisGenerator = tool.generate(); + + // java中的注释 + for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) { + if (file.getFileName().equals("Tb.java")) { + TopLevelClass topLevelClass = (TopLevelClass) file.getCompilationUnit(); + // addJavaFileComment + Assert.assertEquals(topLevelClass.getFileCommentLines().get(0), "TestAddJavaFileComment:Tb:" + new SimpleDateFormat("yyyy-MM").format(new Date())); + // addFieldComment 同时测试 if 判断和 mbg + Field id = topLevelClass.getFields().get(0); + Assert.assertEquals(id.getJavaDocLines().get(0), "注释1"); + Assert.assertEquals(id.getJavaDocLines().get(1), MergeConstants.NEW_ELEMENT_TAG); + // addGeneralMethodComment + Method cons = topLevelClass.getMethods().get(0); + Assert.assertEquals(cons.getJavaDocLines().get(0), "addGeneralMethodComment:Tb:tb"); + // addSetterComment + Method setter = topLevelClass.getMethods().get(5); + Assert.assertEquals(setter.getJavaDocLines().get(0), "addSetterComment:field1:field1"); + } + } + + // xml注释 + ObjectUtil xml = new ObjectUtil(myBatisGenerator.getGeneratedXmlFiles().get(0)); + Document doc = (Document) xml.get("document"); + List els = ((XmlElement) (doc.getRootElement().getElements().get(0))).getElements(); + String comment = ((TextElement) els.get(0)).getContent(); + Assert.assertEquals(comment, "addComment:BaseResultMap"); + } } diff --git a/src/test/resources/scripts/ComentPlugin/init.sql b/src/test/resources/scripts/ComentPlugin/init.sql new file mode 100644 index 0000000..241549d --- /dev/null +++ b/src/test/resources/scripts/ComentPlugin/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/ComentPlugin/mybatis-generator-without-template.xml b/src/test/resources/scripts/ComentPlugin/mybatis-generator-without-template.xml new file mode 100644 index 0000000..5c50a36 --- /dev/null +++ b/src/test/resources/scripts/ComentPlugin/mybatis-generator-without-template.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/src/test/resources/scripts/ComentPlugin/mybatis-generator.xml b/src/test/resources/scripts/ComentPlugin/mybatis-generator.xml new file mode 100644 index 0000000..04aaccc --- /dev/null +++ b/src/test/resources/scripts/ComentPlugin/mybatis-generator.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/src/test/resources/scripts/ComentPlugin/test-comment.ftl b/src/test/resources/scripts/ComentPlugin/test-comment.ftl new file mode 100644 index 0000000..f5606d3 --- /dev/null +++ b/src/test/resources/scripts/ComentPlugin/test-comment.ftl @@ -0,0 +1,238 @@ + + \ No newline at end of file