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