1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertThrows;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.junit.jupiter.api.Test;
25 import org.mybatis.generator.api.MyBatisGenerator;
26 import org.mybatis.generator.config.Configuration;
27 import org.mybatis.generator.config.ConnectionFactoryConfiguration;
28 import org.mybatis.generator.config.Context;
29 import org.mybatis.generator.config.JDBCConnectionConfiguration;
30 import org.mybatis.generator.config.ModelType;
31 import org.mybatis.generator.config.xml.ConfigurationParser;
32 import org.mybatis.generator.exception.InvalidConfigurationException;
33 import org.mybatis.generator.internal.DefaultShellCallback;
34
35 class MyBatisGeneratorTest {
36
37 @Test
38 void testGenerateMyBatis3WithInvalidConfig() throws Exception {
39 List<String> warnings = new ArrayList<>();
40 ConfigurationParser cp = new ConfigurationParser(warnings);
41 Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3_badConfig.xml"));
42
43 DefaultShellCallback shellCallback = new DefaultShellCallback(true);
44
45 InvalidConfigurationException e =
46 assertThrows(InvalidConfigurationException.class, () -> {
47 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
48 myBatisGenerator.generate(null, null, null, false);
49 });
50
51 assertEquals(2, e.getErrors().size());
52 }
53
54 @Test
55 void testGenerateInvalidConfigWithNoConnectionSources() {
56 List<String> warnings = new ArrayList<>();
57 Configuration config = new Configuration();
58 Context context = new Context(ModelType.HIERARCHICAL);
59 context.setId("MyContext");
60 config.addContext(context);
61
62 DefaultShellCallback shellCallback = new DefaultShellCallback(true);
63
64 InvalidConfigurationException e =
65 assertThrows(InvalidConfigurationException.class, () -> {
66 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
67 myBatisGenerator.generate(null, null, null, false);
68 });
69 assertEquals(3, e.getErrors().size());
70 }
71
72 @Test
73 void testGenerateInvalidConfigWithTwoConnectionSources() {
74 List<String> warnings = new ArrayList<>();
75 Configuration config = new Configuration();
76 Context context = new Context(ModelType.HIERARCHICAL);
77 context.setId("MyContext");
78 context.setConnectionFactoryConfiguration(new ConnectionFactoryConfiguration());
79 context.setJdbcConnectionConfiguration(new JDBCConnectionConfiguration());
80 config.addContext(context);
81
82 DefaultShellCallback shellCallback = new DefaultShellCallback(true);
83
84 InvalidConfigurationException e =
85 assertThrows(InvalidConfigurationException.class, () -> {
86 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
87 myBatisGenerator.generate(null, null, null, false);
88 });
89 assertEquals(3, e.getErrors().size());
90 }
91 }