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.Optional;
33  import java.util.Properties;
34  
35  import org.codehaus.plexus.ContainerConfiguration;
36  import org.codehaus.plexus.PlexusConstants;
37  import org.codehaus.plexus.PlexusTestCase;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.codehaus.plexus.util.xml.XmlStreamReader;
40  import org.xml.sax.SAXException;
41  
42  /**
43   * Abstract class for Modello plugins unit-tests that check output generated by the plugin.
44   *
45   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
46   */
47  public abstract class AbstractModelloGeneratorTest extends PlexusTestCase {
48      private String name;
49  
50      protected AbstractModelloGeneratorTest(String name) {
51          this.name = name;
52      }
53  
54      protected void setUp() throws Exception {
55          super.setUp();
56  
57          FileUtils.deleteDirectory(getOutputDirectory());
58  
59          assertTrue(getOutputDirectory().mkdirs());
60      }
61  
62      protected File getOutputDirectory() {
63          return getTestFile("target/generator-results/" + getName());
64      }
65  
66      public String getName() {
67          return name;
68      }
69  
70      protected Properties getModelloParameters() {
71          Properties parameters = new Properties();
72  
73          parameters.setProperty("modello.output.directory", getOutputDirectory().getAbsolutePath());
74  
75          return parameters;
76      }
77  
78      protected Properties getModelloParameters(String version) {
79          Properties parameters = getModelloParameters(version, null);
80  
81          return parameters;
82      }
83  
84      protected Properties getModelloParameters(String version, Integer javaSource) {
85          Properties parameters = getModelloParameters();
86  
87          parameters.setProperty("modello.package.with.version", Boolean.toString(false));
88          parameters.setProperty("modello.version", version);
89          Optional.ofNullable(javaSource)
90                  .ifPresent(a -> parameters.setProperty("modello.output.java.source", Integer.toString(a)));
91  
92          return parameters;
93      }
94  
95      protected Reader getXmlResourceReader(String name) throws IOException {
96          return new XmlStreamReader(getClass().getResourceAsStream(name));
97      }
98  
99      protected SAXParser createSaxParserWithSchema(String generatedXsdName)
100             throws ParserConfigurationException, SAXException {
101         SAXParserFactory factory = SAXParserFactory.newInstance();
102         factory.setValidating(true);
103         factory.setNamespaceAware(true);
104         SAXParser saxParser = factory.newSAXParser();
105         saxParser.setProperty(
106                 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
107         saxParser.setProperty(
108                 "http://java.sun.com/xml/jaxp/properties/schemaSource",
109                 new File(getOutputDirectory(), generatedXsdName));
110         return saxParser;
111     }
112 
113     @Override
114     protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
115         containerConfiguration.setClassPathScanning("cache");
116         containerConfiguration.setAutoWiring(true);
117         containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
118     }
119 }