View Javadoc
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.DefaultArtifact;
24  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Reporting;
29  import org.apache.maven.model.Repository;
30  import org.apache.maven.model.Resource;
31  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
32  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
33  import org.codehaus.plexus.PlexusTestCase;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.InputStreamReader;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Properties;
41  
42  /**
43   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
44   * @version $Id: AbstractAntTestMavenProjectStub.java 1517969 2013-08-27 20:14:02Z krosenvold $
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;
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             //noinspection ResultOfMethodCallIgnored
124             basedir.mkdirs();
125         }
126 
127         return basedir;
128     }
129 
130     /**
131      * @see org.apache.maven.project.MavenProject#getCompileSourceRoots()
132      */
133     public List getCompileSourceRoots()
134     {
135         File src =
136             new File( PlexusTestCase.getBasedir() + "/src/test/resources/unit/" + getProjetPath() + "src/main/java" );
137         return Collections.singletonList( src.getAbsolutePath() );
138     }
139 
140     /**
141      * @see org.apache.maven.project.MavenProject#getTestCompileSourceRoots()
142      */
143     public List getTestCompileSourceRoots()
144     {
145         File test =
146             new File( PlexusTestCase.getBasedir() + "/src/test/resources/unit/" + getProjetPath() + "src/test/java" );
147         return Collections.singletonList( test.getAbsolutePath() );
148     }
149 
150     /**
151      * @see org.apache.maven.project.MavenProject#getCompileArtifacts()
152      */
153     public List getCompileArtifacts()
154     {
155         Artifact junit =
156             new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.2" ), Artifact.SCOPE_TEST,
157                                  "jar", null, new DefaultArtifactHandler( "jar" ), false );
158         junit.setFile( new File( "junit/junit/3.8.2/junit-3.8.2.jar" ) );
159 
160         return Collections.singletonList( junit );
161     }
162 
163     /**
164      * @see org.apache.maven.project.MavenProject#getTestArtifacts()
165      */
166     public List getTestArtifacts()
167     {
168         Artifact junit =
169             new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.2" ), Artifact.SCOPE_TEST,
170                                  "jar", null, new DefaultArtifactHandler( "jar" ), false );
171         junit.setFile( new File( "junit/junit/3.8.2/junit-3.8.2.jar" ) );
172 
173         return Collections.singletonList( junit );
174     }
175 
176     /**
177      * @see org.apache.maven.project.MavenProject#getRepositories()
178      */
179     public List getRepositories()
180     {
181         Repository repo = new Repository();
182         repo.setId( "central" );
183         repo.setName( "central" );
184         repo.setUrl( "http://repo1.maven.org/maven2" );
185 
186         return Collections.singletonList( repo );
187     }
188 
189     /**
190      * @see org.apache.maven.project.MavenProject#getProperties()
191      */
192     public Properties getProperties()
193     {
194         return getModel().getProperties();
195     }
196 
197     /**
198      * @see org.apache.maven.project.MavenProject#getReporting()
199      */
200     public Reporting getReporting()
201     {
202         return getModel().getReporting();
203     }
204 
205     /**
206      * @return the project path from <code>src/test/resources/unit</code> directory
207      */
208     public abstract String getProjetPath();
209 }