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.api.plugin.testing.stubs;
20  
21  import java.nio.file.Path;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Optional;
27  import java.util.Set;
28  
29  import org.apache.maven.api.DependencyCoordinates;
30  import org.apache.maven.api.Language;
31  import org.apache.maven.api.Packaging;
32  import org.apache.maven.api.PathType;
33  import org.apache.maven.api.ProducedArtifact;
34  import org.apache.maven.api.Project;
35  import org.apache.maven.api.Type;
36  import org.apache.maven.api.annotations.Nonnull;
37  import org.apache.maven.api.model.Model;
38  import org.apache.maven.api.model.PluginContainer;
39  import org.apache.maven.api.model.Profile;
40  
41  /**
42   * A stub implementation of {@link Project} for testing Maven plugins.
43   * Provides a simplified project structure with basic model information.
44   *
45   * @since 4.0.0
46   */
47  public class ProjectStub implements Project {
48  
49      private Model model = Model.newInstance();
50      private Path basedir;
51      private Path pomPath;
52      private boolean topProject;
53      private Path rootDirectory;
54      private ProducedArtifact mainArtifact;
55      private List<Profile> declaredProfiles = List.of();
56      private List<Profile> effectiveProfiles = List.of();
57      private List<Profile> declaredActiveProfiles = List.of();
58      private List<Profile> effectiveActiveProfiles = List.of();
59  
60      public void setModel(Model model) {
61          this.model = model;
62      }
63  
64      @Nonnull
65      @Override
66      public String getGroupId() {
67          return model.getGroupId();
68      }
69  
70      @Nonnull
71      @Override
72      public String getArtifactId() {
73          return model.getArtifactId();
74      }
75  
76      @Nonnull
77      @Override
78      public String getVersion() {
79          return model.getVersion();
80      }
81  
82      public String getName() {
83          return model.getName();
84      }
85  
86      @Nonnull
87      @Override
88      public Packaging getPackaging() {
89          return new Packaging() {
90              @Override
91              public String id() {
92                  return model.getPackaging();
93              }
94  
95              @Override
96              public Type type() {
97                  return new Type() {
98                      @Override
99                      public String id() {
100                         return model.getPackaging();
101                     }
102 
103                     @Override
104                     public Language getLanguage() {
105                         return null;
106                     }
107 
108                     @Override
109                     public String getExtension() {
110                         return model.getPackaging();
111                     }
112 
113                     @Override
114                     public String getClassifier() {
115                         return "";
116                     }
117 
118                     @Override
119                     public boolean isIncludesDependencies() {
120                         return false;
121                     }
122 
123                     @Override
124                     public Set<PathType> getPathTypes() {
125                         return Set.of();
126                     }
127                 };
128             }
129 
130             @Override
131             public Map<String, PluginContainer> plugins() {
132                 return Map.of();
133             }
134         };
135     }
136 
137     @Override
138     public List<ProducedArtifact> getArtifacts() {
139         ProducedArtifact pomArtifact = new ProducedArtifactStub(getGroupId(), getArtifactId(), "", getVersion(), "pom");
140         return mainArtifact != null ? Arrays.asList(pomArtifact, mainArtifact) : Arrays.asList(pomArtifact);
141     }
142 
143     @Nonnull
144     @Override
145     public Model getModel() {
146         return model;
147     }
148 
149     @Nonnull
150     @Override
151     public Path getPomPath() {
152         return pomPath;
153     }
154 
155     @Nonnull
156     @Override
157     public List<DependencyCoordinates> getDependencies() {
158         return null;
159     }
160 
161     @Nonnull
162     @Override
163     public List<DependencyCoordinates> getManagedDependencies() {
164         return null;
165     }
166 
167     @Override
168     public Path getBasedir() {
169         return basedir;
170     }
171 
172     @Override
173     public Optional<Project> getParent() {
174         return Optional.empty();
175     }
176 
177     @Override
178     public boolean isTopProject() {
179         return topProject;
180     }
181 
182     @Override
183     public boolean isRootProject() {
184         return model.isRoot();
185     }
186 
187     @Override
188     public Path getRootDirectory() {
189         return rootDirectory;
190     }
191 
192     //
193     // Setters
194     //
195 
196     public ProjectStub setBasedir(Path basedir) {
197         this.basedir = basedir;
198         return this;
199     }
200 
201     public ProjectStub setGroupId(String groupId) {
202         model = model.withGroupId(groupId);
203         return this;
204     }
205 
206     public ProjectStub setArtifactId(String artifactId) {
207         model = model.withArtifactId(artifactId);
208         return this;
209     }
210 
211     public ProjectStub setVersion(String version) {
212         model = model.withVersion(version);
213         return this;
214     }
215 
216     public ProjectStub setName(String name) {
217         model = model.withName(name);
218         return this;
219     }
220 
221     public ProjectStub setDescription(String desc) {
222         model = model.withDescription(desc);
223         return this;
224     }
225 
226     public ProjectStub setPackaging(String packaging) {
227         model = model.withPackaging(packaging);
228         return this;
229     }
230 
231     public ProjectStub setMainArtifact(ProducedArtifact mainArtifact) {
232         this.mainArtifact = mainArtifact;
233         return this;
234     }
235 
236     public ProjectStub setPomPath(Path pomPath) {
237         this.pomPath = pomPath;
238         return this;
239     }
240 
241     public ProjectStub setTopProject(boolean topProject) {
242         this.topProject = topProject;
243         return this;
244     }
245 
246     public ProjectStub setMavenModel(org.apache.maven.model.Model model) {
247         this.model = model.getDelegate();
248         return this;
249     }
250 
251     public ProjectStub setRootDirectory(Path rootDirectory) {
252         this.rootDirectory = rootDirectory;
253         return this;
254     }
255 
256     public ProjectStub addProperty(String key, String value) {
257         Map<String, String> props = new HashMap<>(model.getProperties());
258         props.put(key, value);
259         model = model.withProperties(props);
260         return this;
261     }
262 
263     @Override
264     @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because list is unmodifiable.
265     public List<Profile> getDeclaredProfiles() {
266         return declaredProfiles;
267     }
268 
269     public void setDeclaredProfiles(List<Profile> values) {
270         declaredProfiles = List.copyOf(values);
271     }
272 
273     @Override
274     @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because list is unmodifiable.
275     public List<Profile> getEffectiveProfiles() {
276         return effectiveProfiles;
277     }
278 
279     public void setEffectiveProfiles(List<Profile> values) {
280         effectiveProfiles = List.copyOf(values);
281     }
282 
283     @Override
284     @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because list is unmodifiable.
285     public List<Profile> getDeclaredActiveProfiles() {
286         return declaredActiveProfiles;
287     }
288 
289     public void setDeclaredActiveProfiles(List<Profile> values) {
290         declaredActiveProfiles = List.copyOf(values);
291     }
292 
293     @Override
294     @SuppressWarnings("ReturnOfCollectionOrArrayField") // Safe because list is unmodifiable.
295     public List<Profile> getEffectiveActiveProfiles() {
296         return effectiveActiveProfiles;
297     }
298 
299     public void setEffectiveActiveProfiles(List<Profile> values) {
300         effectiveActiveProfiles = List.copyOf(values);
301     }
302 }