View Javadoc
1   package org.codehaus.modello;
2   
3   /*
4    * Copyright (c) 2005, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * Abstract class for Modello plugins unit-tests that check output generated by the plugin.
48   *
49   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
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 }