1   package org.apache.maven.plugin.ant.stubs;
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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.InputStreamReader;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.DefaultArtifact;
31  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
32  import org.apache.maven.artifact.versioning.VersionRange;
33  import org.apache.maven.model.Build;
34  import org.apache.maven.model.Model;
35  import org.apache.maven.model.Reporting;
36  import org.apache.maven.model.Repository;
37  import org.apache.maven.model.Resource;
38  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
39  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
40  import org.codehaus.plexus.PlexusTestCase;
41  
42  /**
43   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
44   * @version $Id: AbstractAntTestMavenProjectStub.java 639969 2008-03-22 10:10:41Z bentmann $
45   */
46  public abstract class AbstractAntTestMavenProjectStub
47      extends MavenProjectStub
48  {
49      private Build build;
50  
51      /**
52       * Default
53       */
54      public AbstractAntTestMavenProjectStub()
55      {
56          File antTestDir = new File( PlexusTestCase.getBasedir() + "/src/test/resources/unit/" + getProjetPath() + "/" );
57  
58          MavenXpp3Reader pomReader = new MavenXpp3Reader();
59          Model model = null;
60  
61          try
62          {
63              File pomFile = new File( antTestDir, "pom.xml" );
64              // TODO: Once plexus-utils has been bumped to 1.4.4, use ReaderFactory.newXmlReader()
65              model = pomReader.read( new InputStreamReader( new FileInputStream( pomFile ), "UTF-8" ) );
66              setModel( model );
67          }
68          catch ( Exception e )
69          {
70              throw new RuntimeException( e );
71          }
72  
73          setGroupId( model.getGroupId() );
74          setArtifactId( model.getArtifactId() );
75          setVersion( model.getVersion() );
76          setName( model.getName() );
77          setUrl( model.getUrl() );
78          setPackaging( model.getPackaging() );
79  
80          build = new Build();
81          Resource resource = new Resource();
82  
83          build.setFinalName( model.getArtifactId() );
84          build.setDirectory( getBasedir().getAbsolutePath() + "/target" );
85  
86          build.setSourceDirectory( antTestDir + "/src/main/java" );
87          resource.setDirectory( antTestDir + "/src/main/resources" );
88          build.setResources( Collections.singletonList( resource ) );
89          build.setOutputDirectory( getBasedir().getAbsolutePath() + "/target/classes" );
90  
91          build.setTestSourceDirectory( antTestDir + "/src/test/java" );
92          resource = new Resource();
93          resource.setDirectory( antTestDir + "/src/test/resources" );
94          build.setTestResources( Collections.singletonList( resource ) );
95          build.setTestOutputDirectory( getBasedir().getAbsolutePath() + "/target/test-classes" );
96  
97          setBuild( build );
98  
99          Reporting reporting = new Reporting();
100 
101         reporting.setOutputDirectory( getBasedir().getAbsolutePath() + "/target/site" );
102 
103         getModel().setReporting( reporting );
104     }
105 
106     /**
107      * @see org.apache.maven.project.MavenProject#getBuild()
108      */
109     public Build getBuild()
110     {
111         return build;
112     }
113 
114     /**
115      * @see org.apache.maven.project.MavenProject#getBasedir()
116      */
117     public File getBasedir()
118     {
119         File basedir = new File( PlexusTestCase.getBasedir(), "/target/test/unit/" + getProjetPath() + "/" );
120 
121         if ( !basedir.exists() )
122         {
123             basedir.mkdirs();
124         }
125 
126         return basedir;
127     }
128 
129     /**
130      * @see org.apache.maven.project.MavenProject#getCompileSourceRoots()
131      */
132     public List getCompileSourceRoots()
133     {
134         File src = new File( PlexusTestCase.getBasedir() + "/src/test/resources/unit/" + getProjetPath() + "src/main/java" );
135         return Collections.singletonList( src.getAbsolutePath() );
136     }
137 
138     /**
139      * @see org.apache.maven.project.MavenProject#getTestCompileSourceRoots()
140      */
141     public List getTestCompileSourceRoots()
142     {
143         File test = new File( PlexusTestCase.getBasedir() + "/src/test/resources/unit/" + getProjetPath() + "src/test/java" );
144         return Collections.singletonList( test.getAbsolutePath() );
145     }
146 
147     /**
148      * @see org.apache.maven.project.MavenProject#getCompileArtifacts()
149      */
150     public List getCompileArtifacts()
151     {
152         Artifact junit = new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.1" ),
153                                               Artifact.SCOPE_TEST, "jar", null, new DefaultArtifactHandler( "jar" ),
154                                               false );
155         junit.setFile( new File( "junit/junit/3.8.1/junit-3.8.1.jar" ) );
156 
157         return Collections.singletonList( junit );
158     }
159 
160     /**
161      * @see org.apache.maven.project.MavenProject#getTestArtifacts()
162      */
163     public List getTestArtifacts()
164     {
165         Artifact junit = new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.1" ),
166                                               Artifact.SCOPE_TEST, "jar", null, new DefaultArtifactHandler( "jar" ),
167                                               false );
168         junit.setFile( new File( "junit/junit/3.8.1/junit-3.8.1.jar" ) );
169 
170         return Collections.singletonList( junit );
171     }
172 
173     /**
174      * @see org.apache.maven.project.MavenProject#getRepositories()
175      */
176     public List getRepositories()
177     {
178         Repository repo = new Repository();
179         repo.setId( "central" );
180         repo.setName( "central" );
181         repo.setUrl( "http://repo1.maven.org/maven2" );
182 
183         return Collections.singletonList( repo );
184     }
185 
186     /**
187      * @see org.apache.maven.project.MavenProject#getProperties()
188      */
189     public Properties getProperties()
190     {
191         return getModel().getProperties();
192     }
193 
194     /**
195      * @see org.apache.maven.project.MavenProject#getReporting()
196      */
197     public Reporting getReporting()
198     {
199         return getModel().getReporting();
200     }
201 
202     /**
203      * @return the project path from <code>src/test/resources/unit</code> directory
204      */
205     public abstract String getProjetPath();
206 }