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