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