View Javadoc

1   package org.codehaus.modello.plugin.converters;
2   
3   /*
4    * Copyright (c) 2006, 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 org.codehaus.modello.AbstractModelloJavaGeneratorTest;
26  import org.codehaus.modello.ModelloException;
27  import org.codehaus.modello.ModelloParameterConstants;
28  import org.codehaus.modello.core.ModelloCore;
29  import org.codehaus.modello.model.Model;
30  
31  import java.io.Reader;
32  import java.util.Properties;
33  
34  /**
35   */
36  public class ConverterGeneratorTest
37      extends AbstractModelloJavaGeneratorTest
38  {
39      public ConverterGeneratorTest()
40      {
41          super( "converters" );
42      }
43  
44      public void testConverterGenerator()
45          throws Throwable
46      {
47          generateConverterClasses( getXmlResourceReader( "/models/maven.mdo" ), "3.0.0", "4.0.0" );
48  
49          generateConverterClasses( getXmlResourceReader( "/features.mdo" ), "1.0.0", "1.1.0" );
50  
51          addDependency( "stax", "stax-api" );
52          addDependency( "org.codehaus.woodstox", "wstx-asl" );
53  
54          compileGeneratedSources();
55  
56          verifyCompiledGeneratedSources( "ConvertersVerifier" );
57      }
58  
59      private void generateConverterClasses( Reader modelReader, String fromVersion, String toVersion )
60          throws Throwable
61      {
62          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
63  
64          Model model = modello.loadModel( modelReader );
65  
66          Properties parameters = new Properties();
67          parameters.setProperty( ModelloParameterConstants.OUTPUT_DIRECTORY, getOutputDirectory().getAbsolutePath() );
68          parameters.setProperty( ModelloParameterConstants.ALL_VERSIONS, fromVersion + "," + toVersion );
69  
70          generateClasses( parameters, modello, model, fromVersion, toVersion, "java" );
71          generateClasses( parameters, modello, model, fromVersion, toVersion, "stax-reader" );
72          generateClasses( parameters, modello, model, fromVersion, toVersion, "stax-writer" );
73          generateClasses( parameters, modello, model, fromVersion, toVersion, "converters" );
74      }
75  
76      private void generateClasses( Properties parameters, ModelloCore modello, Model model, String fromVersion,
77                                    String toVersion, String outputType )
78          throws ModelloException
79      {
80          parameters.setProperty( ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString( false ) );
81          parameters.setProperty( ModelloParameterConstants.VERSION, toVersion );
82          modello.generate( model, outputType, parameters );
83  
84          parameters.setProperty( ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString( true ) );
85          parameters.setProperty( ModelloParameterConstants.VERSION, fromVersion );
86          modello.generate( model, outputType, parameters );
87  
88          parameters.setProperty( ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString( true ) );
89          parameters.setProperty( ModelloParameterConstants.VERSION, toVersion );
90          modello.generate( model, outputType, parameters );
91      }
92  }