1 package org.codehaus.modello;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.parsers.SAXParser;
27 import javax.xml.parsers.SAXParserFactory;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Optional;
35
36 import org.codehaus.plexus.ContainerConfiguration;
37 import org.codehaus.plexus.PlexusConstants;
38 import org.codehaus.plexus.PlexusTestCase;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.codehaus.plexus.util.xml.XmlStreamReader;
41 import org.xml.sax.SAXException;
42
43
44
45
46
47
48 public abstract class AbstractModelloGeneratorTest extends PlexusTestCase {
49 private String name;
50
51 protected AbstractModelloGeneratorTest(String name) {
52 this.name = name;
53 }
54
55 protected void setUp() throws Exception {
56 super.setUp();
57
58 FileUtils.deleteDirectory(getOutputDirectory());
59
60 assertTrue(getOutputDirectory().mkdirs());
61 }
62
63 protected File getOutputDirectory() {
64 return getTestFile("target/generator-results/" + getName());
65 }
66
67 public String getName() {
68 return name;
69 }
70
71 protected Map<String, Object> getModelloParameters() {
72 Map<String, Object> parameters = new HashMap<>();
73
74 parameters.put("modello.output.directory", getOutputDirectory().getAbsolutePath());
75
76 return parameters;
77 }
78
79 protected Map<String, Object> getModelloParameters(String version) {
80 return getModelloParameters(version, null);
81 }
82
83 protected Map<String, Object> getModelloParameters(String version, Integer javaSource) {
84 Map<String, Object> parameters = getModelloParameters();
85
86 parameters.put("modello.package.with.version", Boolean.toString(false));
87 parameters.put("modello.version", version);
88 Optional.ofNullable(javaSource)
89 .ifPresent(a -> parameters.put("modello.output.java.source", Integer.toString(a)));
90
91 return parameters;
92 }
93
94 protected Reader getXmlResourceReader(String name) throws IOException {
95 return new XmlStreamReader(getClass().getResourceAsStream(name));
96 }
97
98 protected SAXParser createSaxParserWithSchema(String generatedXsdName)
99 throws ParserConfigurationException, SAXException {
100 SAXParserFactory factory = SAXParserFactory.newInstance();
101 factory.setValidating(true);
102 factory.setNamespaceAware(true);
103 SAXParser saxParser = factory.newSAXParser();
104 saxParser.setProperty(
105 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
106 saxParser.setProperty(
107 "http://java.sun.com/xml/jaxp/properties/schemaSource",
108 new File(getOutputDirectory(), generatedXsdName));
109 return saxParser;
110 }
111
112 @Override
113 protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
114 containerConfiguration.setClassPathScanning("cache");
115 containerConfiguration.setAutoWiring(true);
116 containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
117 }
118 }