View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo.stubs;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Objects;
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.ArtifactRepositoryPolicy;
33  import org.apache.maven.artifact.repository.MavenArtifactRepository;
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 extends MavenProjectStub {
53      private Model model;
54  
55      private Build build;
56  
57      /**
58       * Default constructor
59       */
60      public ProjectInfoProjectStub() {
61          MavenXpp3Reader pomReader = new MavenXpp3Reader();
62          XmlStreamReader reader = null;
63          try {
64              reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()));
65              model = pomReader.read(reader);
66              reader.close();
67              reader = null;
68              setModel(model);
69          } catch (Exception e) {
70              throw new RuntimeException(e);
71          } finally {
72              IOUtil.close(reader);
73          }
74  
75          setGroupId(model.getGroupId());
76          setArtifactId(model.getArtifactId());
77          setVersion(model.getVersion());
78          setName(model.getName());
79          setUrl(model.getUrl());
80          setPackaging(model.getPackaging());
81  
82          String type = Objects.toString(super.getPackaging(), "jar");
83          Artifact artifact = new ProjectInfoPluginArtifactStub(getGroupId(), getArtifactId(), getVersion(), type);
84          artifact.setArtifactHandler(new DefaultArtifactHandlerStub());
85          setArtifact(artifact);
86  
87          Build build = new Build();
88          build.setFinalName(model.getArtifactId());
89          build.setDirectory(super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target");
90          build.setSourceDirectory(getBasedir() + "/src/main/java");
91          build.setOutputDirectory(super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target/classes");
92          build.setTestSourceDirectory(getBasedir() + "/src/test/java");
93          build.setTestOutputDirectory(
94                  super.getBasedir() + "/target/test/unit/" + model.getArtifactId() + "/target/test-classes");
95          setBuild(build);
96  
97          List<String> compileSourceRoots = new ArrayList<>();
98          compileSourceRoots.add(getBasedir() + "/src/main/java");
99          setCompileSourceRoots(compileSourceRoots);
100 
101         List<String> testCompileSourceRoots = new ArrayList<>();
102         testCompileSourceRoots.add(getBasedir() + "/src/test/java");
103         setTestCompileSourceRoots(testCompileSourceRoots);
104     }
105 
106     /**
107      * @return the POM file name
108      */
109     protected abstract String getPOM();
110 
111     @Override
112     public Model getModel() {
113         return model;
114     }
115 
116     @Override
117     public Build getBuild() {
118         return build;
119     }
120 
121     @Override
122     public void setBuild(Build build) {
123         this.build = build;
124     }
125 
126     @Override
127     public File getBasedir() {
128         return new File(super.getBasedir() + "/src/test/resources/plugin-configs/");
129     }
130 
131     @Override
132     public File getFile() {
133         return new File(getBasedir(), getPOM());
134     }
135 
136     @Override
137     public Set<Artifact> getArtifacts() {
138         return Collections.emptySet();
139     }
140 
141     @Override
142     public List<ArtifactRepository> getRemoteArtifactRepositories() {
143         ArtifactRepository repository = new MavenArtifactRepository(
144                 "central",
145                 "https://repo1.maven.org/maven2",
146                 new DefaultRepositoryLayout(),
147                 new ArtifactRepositoryPolicy(),
148                 new ArtifactRepositoryPolicy());
149 
150         return Collections.singletonList(repository);
151     }
152 
153     @Override
154     public Set<Artifact> getDependencyArtifacts() {
155         Artifact artifact = new DefaultArtifact(
156                 "junit",
157                 "junit",
158                 VersionRange.createFromVersion("3.8.1"),
159                 Artifact.SCOPE_TEST,
160                 "jar",
161                 null,
162                 new DefaultArtifactHandler("jar"),
163                 false);
164         return Collections.singleton(artifact);
165     }
166 
167     @Override
168     public DependencyManagement getDependencyManagement() {
169         return model.getDependencyManagement();
170     }
171 
172     @Override
173     public PluginManagement getPluginManagement() {
174         PluginManagement pluginMgmt = null;
175 
176         Build build = model.getBuild();
177         if (build != null) {
178             pluginMgmt = build.getPluginManagement();
179         }
180 
181         return pluginMgmt;
182     }
183 }