Java Code Examples for org.mybatis.generator.api.GeneratedJavaFile#getCompilationUnit()

The following examples show how to use org.mybatis.generator.api.GeneratedJavaFile#getCompilationUnit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapperAnnotationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试默认配置
 */
@Test
public void testDefault() throws Exception{
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof Interface && compilationUnit.getType().getShortName().endsWith("Mapper")) {
            Interface interfaze = (Interface) compilationUnit;

            Assert.assertEquals(interfaze.getAnnotations().size(), 1);
            Assert.assertEquals(interfaze.getAnnotations().get(0), "@Mapper");
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")));
        }
    }
}
 
Example 2
Source File: MapperAnnotationPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试配置Repository
 * @throws Exception
 */
@Test
public void testWithRepository() throws Exception{
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator-with-repository.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof Interface && compilationUnit.getType().getShortName().endsWith("Mapper")) {
            Interface interfaze = (Interface) compilationUnit;

            Assert.assertEquals(interfaze.getAnnotations().size(), 2);
            Assert.assertEquals(interfaze.getAnnotations().get(0), "@Mapper");
            Assert.assertEquals(interfaze.getAnnotations().get(1), "@Repository");
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")));
            Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.springframework.stereotype.Repository")));
        }
    }
}
 
Example 3
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试具体生成(只有keys的特殊情况,尽量使用Builder)
 */
@Test
public void testGenerateWithOnlyKeys() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator-with-only-keys.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if (name.equals("TbOnlyKeysKey")){
                Assert.assertTrue(topLevelClass.getAnnotations().contains("@Builder"));
            }
        }
    }
}
 
Example 4
Source File: CommentPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 测试配置了模板参数转换
 */
@Test
public void testGenerateWithOutComment() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/mybatis-generator-without-comment.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().size(), 0);
            // addFieldComment
            Field id = topLevelClass.getFields().get(0);
            Assert.assertEquals(id.getJavaDocLines().size(), 0);
            // addGeneralMethodComment
            Method cons = topLevelClass.getMethods().get(0);
            Assert.assertEquals(cons.getJavaDocLines().size(), 0);
            // addSetterComment
            Method setter = topLevelClass.getMethods().get(5);
            Assert.assertEquals(setter.getJavaDocLines().size(), 0);
        }
    }
}
 
Example 5
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerateDefault() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator-default.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<String> comm = Arrays.asList("@Data");
    List<String> child = new ArrayList<>(Arrays.asList("@EqualsAndHashCode(callSuper = true)", "@ToString(callSuper = true)"));
    child.addAll(comm);

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if ("TbKeyBlobKey".equals(name)) {
                Assert.assertEquals(comm.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(comm.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeyBlobWithBLOBs".equals(name)) {
                Assert.assertEquals(child.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(child.containsAll(topLevelClass.getAnnotations()));
            }

            // tb 没有继承
            if ("Tb".equals(name)) {
                Assert.assertEquals(comm.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(comm.containsAll(topLevelClass.getAnnotations()));
            }
        }
    }
}
 
Example 6
Source File: CommentPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 测试配置了模板参数转换
 */
@Test
public void testGenerateWithTemplate() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/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<Element> els = ((XmlElement) (doc.getRootElement().getElements().get(0))).getElements();
    String comment = ((TextElement) els.get(0)).getContent();
    Assert.assertEquals(comment, "addComment:BaseResultMap");
}
 
Example 7
Source File: LombokPluginTest.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 测试具体生成
 */
@Test
public void testGenerate() throws Exception {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator.xml");
    MyBatisGenerator myBatisGenerator = tool.generate();

    List<String> comm = Arrays.asList("@Data", "@NoArgsConstructor", "@AllArgsConstructor");
    List<String> child = new ArrayList<>(Arrays.asList("@EqualsAndHashCode(callSuper = true)", "@ToString(callSuper = true)"));
    child.addAll(comm);
    List<String> superBuilderParent = new ArrayList<>(Arrays.asList("@SuperBuilder"));
    superBuilderParent.addAll(comm);
    List<String> superBuilderChild = new ArrayList<>(Arrays.asList("@SuperBuilder"));
    superBuilderChild.addAll(child);
    List<String> builder = new ArrayList<>(Arrays.asList("@Builder"));
    builder.addAll(comm);

    for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
        CompilationUnit compilationUnit = file.getCompilationUnit();
        if (compilationUnit instanceof TopLevelClass) {
            TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
            String name = topLevelClass.getType().getShortName();
            if ("TbKeyBlobKey".equals(name)) {
                Assert.assertEquals(superBuilderParent.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderParent.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeyBlobWithBLOBs".equals(name)) {
                Assert.assertEquals(superBuilderChild.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderChild.containsAll(topLevelClass.getAnnotations()));
            }

            // tb 没有继承
            if ("Tb".equals(name)) {
                Assert.assertEquals(builder.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(builder.containsAll(topLevelClass.getAnnotations()));
            }

            if ("TbKeysKey".equals(name)) {
                Assert.assertEquals(superBuilderParent.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderParent.containsAll(topLevelClass.getAnnotations()));
            } else if ("TbKeys".equals(name)) {
                Assert.assertEquals(superBuilderChild.size(), topLevelClass.getAnnotations().size());
                Assert.assertTrue(superBuilderChild.containsAll(topLevelClass.getAnnotations()));
            }
        }
    }
}