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.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   * Abstract class for Modello plugins unit-tests that check output generated by the plugin.
45   *
46   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
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 }