View Javadoc

1   package org.apache.archiva.redback.components.registry.modello;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.modello.AbstractModelloJavaGeneratorTest;
23  import org.codehaus.modello.ModelloException;
24  import org.codehaus.modello.ModelloParameterConstants;
25  import org.codehaus.modello.core.ModelloCore;
26  import org.codehaus.modello.model.Model;
27  import org.codehaus.modello.model.ModelValidationException;
28  import org.codehaus.plexus.compiler.CompilerConfiguration;
29  import org.codehaus.plexus.compiler.CompilerException;
30  import org.codehaus.plexus.compiler.CompilerMessage;
31  import org.codehaus.plexus.compiler.javac.JavacCompiler;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.ReaderFactory;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.List;
41  import java.util.Properties;
42  
43  public abstract class AbstractRegistryGeneratorTestCase
44      extends AbstractModelloJavaGeneratorTest
45  {
46      public AbstractRegistryGeneratorTestCase( String name )
47      {
48          super( name );
49      }
50  
51  
52  
53      protected void prepareTest( String outputType )
54          throws ComponentLookupException, ModelloException, ModelValidationException, IOException, CompilerException
55      {
56          ModelloCore modello = (ModelloCore) container.lookup( ModelloCore.ROLE );
57  
58          Model model = modello.loadModel( ReaderFactory.newXmlReader( getTestFile( "src/test/resources/model.mdo" ) ) );
59  
60          File generatedSources = getTestFile( "target/" + outputType + "/sources" );
61  
62          File classes = getOutputDirectory();
63  
64          FileUtils.deleteDirectory( generatedSources );
65  
66          FileUtils.deleteDirectory( classes );
67  
68          generatedSources.mkdirs();
69  
70          classes.mkdirs();
71  
72          Properties parameters = new Properties();
73  
74          parameters.setProperty( ModelloParameterConstants.OUTPUT_DIRECTORY, generatedSources.getAbsolutePath() );
75  
76          parameters.setProperty( ModelloParameterConstants.VERSION, "1.0.0" );
77  
78          parameters.setProperty( ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString( false ) );
79  
80          modello.generate( model, "java", parameters );
81  
82          modello.generate( model, outputType, parameters );
83  
84          String registryVersion = System.getProperty( "registryVersion" );
85  
86          addDependency( "org.codehaus.modello", "modello-core" );//, getModelloVersion() );
87          addDependency( "org.apache.archiva.redback.components.registry", "spring-registry-api" );//, registryVersion );
88          addDependency( "org.apache.archiva.redback.components.registry",
89                         "spring-registry-commons" );//, registryVersion );
90          addDependency( "org.codehaus.plexus", "plexus-container-default" );//, "1.0-alpha-33" );
91          addDependency( "commons-collections", "commons-collections" );//, "3.1" );
92          addDependency( "commons-configuration", "commons-configuration" );//, "1.8" );
93          addDependency( "commons-lang", "commons-lang" );//, "2.6" );
94          addDependency( "commons-logging", "commons-logging-api" );//, "1.0.4" );
95          addDependency( "org.springframework", "spring-context" );//, System.getProperty( "springVersion" ) );
96          addDependency( "org.codehaus.plexus", "plexus-interpolation" );//, "1.14" );
97          addDependency( "org.slf4j", "slf4j-api" );//, System.getProperty( "slf4jVersion" ) );
98          addDependency( "org.slf4j", "slf4j-simple" );//, System.getProperty( "slf4jVersion" ) );
99  
100         if ( "1.5".compareTo( System.getProperty( "java.specification.version" ) ) <= 0 )
101         {
102             // causes a conflict with JDK 1.4 => add this dependency only with JDK 1.5+
103             addDependency( "xerces", "xercesImpl" );//, "2.9.1" );
104         }
105 
106         compile( generatedSources, classes );
107     }
108 
109     protected void compile( File generatedSources, File destinationDirectory )
110         throws IOException, CompilerException
111     {
112 
113         addDependency( "junit", "junit" );//, "3.8.1" );
114 
115         addDependency( "org.codehaus.plexus", "plexus-utils" );//, "1.4.5" );
116 
117         addDependency( "org.codehaus.modello", "modello-test" );//, getModelloVersion() );
118 
119         List<String> classPathElements = new ArrayList<String>( getClasspath().size() + 2 );
120 
121         classPathElements.add( getTestPath( "target/classes" ) );
122 
123         classPathElements.add( getTestPath( "target/test-classes" ) );
124 
125         for ( int i = 0; i < getClasspath().size(); i++ )
126         {
127             classPathElements.add( getClasspath().get( i ).getAbsolutePath() );
128         }
129 
130         org.codehaus.plexus.compiler.Compiler compiler = new JavacCompiler();
131 
132         CompilerConfiguration configuration = new CompilerConfiguration();
133         configuration.setClasspathEntries( classPathElements );
134         configuration.setSourceLocations(
135             Arrays.asList( getTestPath( "src/test/verifiers/" + getName() ), generatedSources.getAbsolutePath() ) );
136         configuration.setOutputLocation( getOutputClasses().getAbsolutePath() );// destinationDirectory.getAbsolutePath() );
137         configuration.setDebug( true );
138         configuration.setSourceVersion( "1.5" );
139         configuration.setTargetVersion( "1.5" );
140 
141         List<CompilerMessage> messages = compiler.performCompile( configuration ).getCompilerMessages();
142 
143         int error = 0;
144 
145         for ( CompilerMessage message : messages )
146         {
147             System.out.println( message.getFile() + "[" + message.getStartLine() + "," + message.getStartColumn() +
148                                     "]: " + message.getMessage() );
149             if ( message.getKind() == CompilerMessage.Kind.ERROR )
150             {
151                 error++;
152             }
153         }
154 
155         assertEquals( "There was compilation errors.:" + messages, 0, error );
156     }
157 }