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.Assert.fail;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31 import org.junit.runners.Parameterized.Parameters;
32 import org.mybatis.generator.api.GeneratedXmlFile;
33 import org.mybatis.generator.api.MyBatisGenerator;
34 import org.mybatis.generator.config.Configuration;
35 import org.mybatis.generator.config.xml.ConfigurationParser;
36 import org.mybatis.generator.internal.DefaultShellCallback;
37 import org.xml.sax.EntityResolver;
38 import org.xml.sax.ErrorHandler;
39 import org.xml.sax.InputSource;
40 import org.xml.sax.SAXException;
41 import org.xml.sax.SAXParseException;
42
43 @RunWith(Parameterized.class)
44 public class XmlCodeGenerationTest {
45
46 private GeneratedXmlFile generatedXmlFile;
47
48 public XmlCodeGenerationTest(GeneratedXmlFile generatedXmlFile) {
49 this.generatedXmlFile = generatedXmlFile;
50 }
51
52 @Test
53 public void testXmlParse() {
54 ByteArrayInputStream is = new ByteArrayInputStream(
55 generatedXmlFile.getFormattedContent().getBytes());
56 try {
57 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
58 factory.setValidating(true);
59 DocumentBuilder builder = factory.newDocumentBuilder();
60 builder.setEntityResolver(new TestEntityResolver());
61 builder.setErrorHandler(new TestErrorHandler());
62 builder.parse(is);
63 } catch (Exception e) {
64 fail("Generated XML File " + generatedXmlFile.getFileName() + " will not parse");
65 }
66 }
67
68 @Parameters
69 public static List<GeneratedXmlFile> generateXmlFiles() throws Exception {
70 List<GeneratedXmlFile> generatedFiles = new ArrayList<GeneratedXmlFile>();
71 generatedFiles.addAll(generateXmlFilesMybatis());
72 generatedFiles.addAll(generateXmlFilesIbatis());
73 return generatedFiles;
74 }
75
76 private static List<GeneratedXmlFile> generateXmlFilesMybatis() throws Exception {
77 JavaCodeGenerationTest.createDatabase();
78 return generateXmlFiles("/scripts/generatorConfig.xml");
79 }
80
81 private static List<GeneratedXmlFile> generateXmlFilesIbatis() throws Exception {
82 JavaCodeGenerationTest.createDatabase();
83 return generateXmlFiles("/scripts/ibatorConfig.xml");
84 }
85
86 private static List<GeneratedXmlFile> generateXmlFiles(String configFile) throws Exception {
87 List<String> warnings = new ArrayList<String>();
88 ConfigurationParser cp = new ConfigurationParser(warnings);
89 Configuration config = cp.parseConfiguration(JavaCodeGenerationTest.class.getResourceAsStream(configFile));
90
91 DefaultShellCallback shellCallback = new DefaultShellCallback(true);
92
93 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
94 myBatisGenerator.generate(null, null, null, false);
95 return myBatisGenerator.getGeneratedXmlFiles();
96 }
97
98 public static class TestEntityResolver implements EntityResolver {
99
100 @Override
101 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
102
103 return new InputSource(new ByteArrayInputStream("".getBytes()));
104 }
105 }
106
107 public static class TestErrorHandler implements ErrorHandler {
108
109 private List<String> errors = new ArrayList<String>();
110 private List<String> warnings = new ArrayList<String>();
111
112 @Override
113 public void warning(SAXParseException exception) throws SAXException {
114 warnings.add(exception.getMessage());
115 }
116
117 @Override
118 public void error(SAXParseException exception) throws SAXException {
119 errors.add(exception.getMessage());
120 }
121
122 @Override
123 public void fatalError(SAXParseException exception) throws SAXException {
124 errors.add(exception.getMessage());
125 }
126
127 public List<String> getErrors() {
128 return errors;
129 }
130
131 public List<String> getWarnings() {
132 return warnings;
133 }
134 }
135 }