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