View Javadoc
1   /*
2    *    Copyright 2006-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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  }