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