View Javadoc
1   package org.apache.maven.report.projectinfo.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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.DefaultArtifact;
30  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
33  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
34  import org.apache.maven.artifact.versioning.VersionRange;
35  import org.apache.maven.model.Build;
36  import org.apache.maven.model.DependencyManagement;
37  import org.apache.maven.model.Model;
38  import org.apache.maven.model.PluginManagement;
39  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
41  import org.apache.maven.shared.utils.io.IOUtil;
42  import org.codehaus.plexus.util.ReaderFactory;
43  import org.codehaus.plexus.util.xml.XmlStreamReader;
44  
45  /**
46   * @author Edwin Punzalan
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @author Nick Stolwijk
49   * @version $Id$
50   */
51  public abstract class ProjectInfoProjectStub
52      extends MavenProjectStub
53  {
54      private Model model;
55  
56      private Build build;
57  
58      /**
59       * Default constructor
60       */
61      public ProjectInfoProjectStub()
62      {
63          MavenXpp3Reader pomReader = new MavenXpp3Reader();
64          XmlStreamReader reader = null;
65          try
66          {
67              reader = ReaderFactory.newXmlReader( new File( getBasedir(), getPOM() ) );
68              model = pomReader.read( reader );
69              setModel( model );
70          }
71          catch ( Exception e )
72          {
73              throw new RuntimeException( e );
74          }
75          finally
76          {
77              IOUtil.close( reader );
78          }
79  
80          setGroupId( model.getGroupId() );
81          setArtifactId( model.getArtifactId() );
82          setVersion( model.getVersion() );
83          setName( model.getName() );
84          setUrl( model.getUrl() );
85          setPackaging( model.getPackaging() );
86  
87          Artifact artifact = new ProjectInfoPluginArtifactStub( getGroupId(), getArtifactId(), getVersion(), getPackaging() );
88          artifact.setArtifactHandler( new DefaultArtifactHandlerStub() );
89          setArtifact( artifact );
90  
91          Build build = new Build();
92          build.setFinalName( model.getArtifactId() );
93          build.setDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target" );
94          build.setSourceDirectory( getBasedir() + "/src/main/java" );
95          build.setOutputDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target/classes" );
96          build.setTestSourceDirectory( getBasedir() + "/src/test/java" );
97          build.setTestOutputDirectory( super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target/test-classes" );
98          setBuild( build );
99  
100         List<String> compileSourceRoots = new ArrayList<String>();
101         compileSourceRoots.add( getBasedir() + "/src/main/java" );
102         setCompileSourceRoots( compileSourceRoots );
103 
104         List<String> testCompileSourceRoots = new ArrayList<String>();
105         testCompileSourceRoots.add( getBasedir() + "/src/test/java" );
106         setTestCompileSourceRoots( testCompileSourceRoots );
107     }
108 
109     /**
110      * @return the POM file name
111      */
112     protected abstract String getPOM();
113 
114     @Override
115     public Model getModel()
116     {
117         return model;
118     }
119 
120     @Override
121     public Build getBuild()
122     {
123         return build;
124     }
125 
126     @Override
127     public void setBuild( Build build )
128     {
129         this.build = build;
130     }
131 
132     @Override
133     public File getBasedir()
134     {
135         return new File( super.getBasedir() + "/src/test/resources/plugin-configs/" );
136     }
137 
138     @Override
139     public File getFile()
140     {
141         return new File( getBasedir(), getPOM() );
142     }
143 
144     @Override
145     public Set<Artifact> getArtifacts()
146     {
147         return Collections.emptySet();
148     }
149 
150     @Override
151     public List<ArtifactRepository> getRemoteArtifactRepositories()
152     {
153         ArtifactRepository repository = new DefaultArtifactRepository( "central", "http://repo1.maven.org/maven2",
154                                                                        new DefaultRepositoryLayout() );
155 
156         return Collections.singletonList( repository );
157     }
158 
159     @Override
160     public Set<Artifact> getDependencyArtifacts()
161     {
162         Artifact artifact =
163             new DefaultArtifact( "junit", "junit", VersionRange.createFromVersion( "3.8.1" ), Artifact.SCOPE_TEST,
164                                  "jar", null, new DefaultArtifactHandler( "jar" ), false );
165         return Collections.singleton( artifact );
166     }
167 
168     @Override
169     public DependencyManagement getDependencyManagement()
170     {
171         return model.getDependencyManagement();
172     }
173 
174     @Override
175     public PluginManagement getPluginManagement()
176     {
177         PluginManagement pluginMgmt = null;
178 
179         Build build = model.getBuild();
180         if ( build != null )
181         {
182             pluginMgmt = build.getPluginManagement();
183         }
184 
185         return pluginMgmt;
186     }
187 
188 }