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.testing.PlexusTestConfiguration;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.codehaus.plexus.util.xml.XmlStreamReader;
41 import org.xml.sax.SAXException;
42
43 import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
44 import static org.junit.jupiter.api.Assertions.assertTrue;
45
46
47
48
49
50
51 public abstract class AbstractModelloGeneratorTest implements PlexusTestConfiguration {
52 private String name;
53
54 protected AbstractModelloGeneratorTest(String name) {
55 this.name = name;
56 }
57
58 public void setUp() throws Exception {
59
60 FileUtils.deleteDirectory(getOutputDirectory());
61
62 assertTrue(getOutputDirectory().mkdirs());
63 }
64
65 protected File getOutputDirectory() {
66 return getTestFile("target/generator-results/" + getName());
67 }
68
69 public String getName() {
70 return name;
71 }
72
73 protected Map<String, Object> getModelloParameters() {
74 Map<String, Object> parameters = new HashMap<>();
75
76 parameters.put("modello.output.directory", getOutputDirectory().getAbsolutePath());
77
78 return parameters;
79 }
80
81 protected Map<String, Object> getModelloParameters(String version) {
82 return getModelloParameters(version, null);
83 }
84
85 protected Map<String, Object> getModelloParameters(String version, Integer javaSource) {
86 Map<String, Object> parameters = getModelloParameters();
87
88 parameters.put("modello.package.with.version", Boolean.toString(false));
89 parameters.put("modello.version", version);
90 Optional.ofNullable(javaSource)
91 .ifPresent(a -> parameters.put("modello.output.java.source", Integer.toString(a)));
92
93 return parameters;
94 }
95
96 protected Reader getXmlResourceReader(String name) throws IOException {
97 return new XmlStreamReader(getClass().getResourceAsStream(name));
98 }
99
100 protected SAXParser createSaxParserWithSchema(String generatedXsdName)
101 throws ParserConfigurationException, SAXException {
102 SAXParserFactory factory = SAXParserFactory.newInstance();
103 factory.setValidating(true);
104 factory.setNamespaceAware(true);
105 SAXParser saxParser = factory.newSAXParser();
106 saxParser.setProperty(
107 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
108 saxParser.setProperty(
109 "http://java.sun.com/xml/jaxp/properties/schemaSource",
110 new File(getOutputDirectory(), generatedXsdName));
111 return saxParser;
112 }
113
114 @Override
115 public void customizeConfiguration(ContainerConfiguration containerConfiguration) {
116 containerConfiguration.setClassPathScanning("cache");
117 containerConfiguration.setAutoWiring(true);
118 containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
119 }
120 }