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