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.scm.manager;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Optional;
26  
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmBranch;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.add.AddScmResult;
33  import org.apache.maven.scm.command.blame.BlameScmRequest;
34  import org.apache.maven.scm.command.blame.BlameScmResult;
35  import org.apache.maven.scm.command.branch.BranchScmResult;
36  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
37  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
38  import org.apache.maven.scm.command.checkin.CheckInScmResult;
39  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
40  import org.apache.maven.scm.command.diff.DiffScmResult;
41  import org.apache.maven.scm.command.edit.EditScmResult;
42  import org.apache.maven.scm.command.export.ExportScmResult;
43  import org.apache.maven.scm.command.list.ListScmResult;
44  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
45  import org.apache.maven.scm.command.remove.RemoveScmResult;
46  import org.apache.maven.scm.command.status.StatusScmResult;
47  import org.apache.maven.scm.command.tag.TagScmResult;
48  import org.apache.maven.scm.command.unedit.UnEditScmResult;
49  import org.apache.maven.scm.command.update.UpdateScmResult;
50  import org.apache.maven.scm.provider.ScmProvider;
51  import org.apache.maven.scm.provider.ScmProviderStub;
52  import org.apache.maven.scm.repository.ScmRepository;
53  import org.apache.maven.scm.repository.ScmRepositoryException;
54  import org.apache.maven.scm.repository.ScmRepositoryStub;
55  import org.apache.maven.scm.repository.UnknownRepositoryStructure;
56  
57  /**
58   * Stub implementation of ScmManager for unit testing purposes.
59   * It allows setting the expected results that the different methods will return.
60   * More information about Stubs on
61   * <a href="https://martinfowler.com/bliki/TestDouble.html">Martin Fowler's TestDouble</a>
62   *
63   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
64   */
65  public class ScmManagerStub implements ScmManager {
66  
67      private ScmRepository scmRepository;
68  
69      private ScmProvider scmProvider;
70  
71      private List<String> messages;
72  
73      /**
74       * Creates a new stub with stub repository and provider, and empty list of messages.
75       */
76      public ScmManagerStub() {
77          setScmRepository(new ScmRepositoryStub());
78          setScmProvider(new ScmProviderStub());
79          setMessages(new ArrayList<>(0));
80      }
81  
82      public void setScmProvider(ScmProvider scmProvider) {
83          this.scmProvider = scmProvider;
84      }
85  
86      public ScmProvider getScmProvider() {
87          return scmProvider;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public void setScmProvider(String providerType, ScmProvider provider) {
94          setScmProvider(provider);
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public void setScmProviderImplementation(String providerType, String providerImplementation) {
101         // Do nothing there
102     }
103 
104     public void setScmRepository(ScmRepository scmRepository) {
105         this.scmRepository = scmRepository;
106     }
107 
108     public ScmRepository getScmRepository() {
109         return scmRepository;
110     }
111 
112     /**
113      * Set the messages to return in validateScmRepository.
114      *
115      * @param messages <code>List</code> of <code>String</code> objects
116      */
117     public void setMessages(List<String> messages) {
118         this.messages = messages;
119     }
120 
121     /**
122      * Get the messages to return in validateScmRepository.
123      *
124      * @return <code>List</code> of <code>String</code> objects
125      */
126     public List<String> getMessages() {
127         return messages;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     public ScmRepository makeScmRepository(String scmUrl) throws ScmRepositoryException, NoSuchScmProviderException {
134         return getScmRepository();
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     public ScmRepository makeProviderScmRepository(String providerType, File path)
141             throws ScmRepositoryException, UnknownRepositoryStructure, NoSuchScmProviderException {
142         return getScmRepository();
143     }
144 
145     @Override
146     public Optional<ScmRepository> makeProviderScmRepository(File path) {
147         return Optional.ofNullable(getScmRepository());
148     }
149 
150     /**
151      * Returns the same list as getMessages()
152      *
153      * @param scmUrl ignored
154      * @return <code>List</code> of <code>String</code> objects, the same list returned by getMessages()
155      */
156     public List<String> validateScmRepository(String scmUrl) {
157         return getMessages();
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public ScmProvider getProviderByUrl(String scmUrl) throws ScmRepositoryException, NoSuchScmProviderException {
164         return getScmProvider();
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     public ScmProvider getProviderByType(String providerType) throws NoSuchScmProviderException {
171         return getScmProvider();
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     public ScmProvider getProviderByRepository(ScmRepository repository) throws NoSuchScmProviderException {
178         return getScmProvider();
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     public AddScmResult add(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
185         return this.getProviderByRepository(repository).add(repository, fileSet);
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     public AddScmResult add(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
192         return this.getProviderByRepository(repository).add(repository, fileSet, message);
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @SuppressWarnings("deprecation")
199     public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName) throws ScmException {
200         return this.getProviderByRepository(repository).branch(repository, fileSet, branchName);
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @SuppressWarnings("deprecation")
207     public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName, String message)
208             throws ScmException {
209         return this.getProviderByRepository(repository).branch(repository, fileSet, branchName, message);
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     public ChangeLogScmResult changeLog(
216             ScmRepository repository, ScmFileSet fileSet, Date startDate, Date endDate, int numDays, ScmBranch branch)
217             throws ScmException {
218         return this.getProviderByRepository(repository)
219                 .changeLog(repository, fileSet, startDate, endDate, numDays, branch);
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     public ChangeLogScmResult changeLog(
226             ScmRepository repository,
227             ScmFileSet fileSet,
228             Date startDate,
229             Date endDate,
230             int numDays,
231             ScmBranch branch,
232             String datePattern)
233             throws ScmException {
234         return this.getProviderByRepository(repository)
235                 .changeLog(repository, fileSet, startDate, endDate, numDays, branch, datePattern);
236     }
237 
238     /**
239      * {@inheritDoc}
240      */
241     public ChangeLogScmResult changeLog(ChangeLogScmRequest request) throws ScmException {
242         final ScmRepository repository = request.getScmRepository();
243         return this.getProviderByRepository(repository).changeLog(request);
244     }
245 
246     /**
247      * {@inheritDoc}
248      */
249     public ChangeLogScmResult changeLog(
250             ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
251             throws ScmException {
252         return this.getProviderByRepository(repository).changeLog(repository, fileSet, startVersion, endVersion);
253     }
254 
255     /**
256      * {@inheritDoc}
257      */
258     public ChangeLogScmResult changeLog(
259             ScmRepository repository,
260             ScmFileSet fileSet,
261             ScmVersion startRevision,
262             ScmVersion endRevision,
263             String datePattern)
264             throws ScmException {
265         return this.getProviderByRepository(repository)
266                 .changeLog(repository, fileSet, startRevision, endRevision, datePattern);
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
273         return this.getProviderByRepository(repository).checkIn(repository, fileSet, message);
274     }
275 
276     /**
277      * {@inheritDoc}
278      */
279     public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, ScmVersion revision, String message)
280             throws ScmException {
281         return this.getProviderByRepository(repository).checkIn(repository, fileSet, revision, message);
282     }
283 
284     @Override
285     public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, CommandParameters commandParameters)
286             throws ScmException {
287         return this.getProviderByRepository(repository).checkIn(repository, fileSet, commandParameters);
288     }
289 
290     /**
291      * {@inheritDoc}
292      */
293     public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
294         return this.getProviderByRepository(repository).checkOut(repository, fileSet);
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
301             throws ScmException {
302         return this.getProviderByRepository(repository).checkOut(repository, fileSet, version);
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, boolean recursive)
309             throws ScmException {
310         return this.getProviderByRepository(repository).checkOut(repository, fileSet, recursive);
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     public CheckOutScmResult checkOut(
317             ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean recursive) throws ScmException {
318         return this.getProviderByRepository(repository).checkOut(repository, fileSet, version, recursive);
319     }
320 
321     /**
322      * {@inheritDoc}
323      */
324     public DiffScmResult diff(
325             ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
326             throws ScmException {
327         return this.getProviderByRepository(repository).diff(repository, fileSet, startVersion, endVersion);
328     }
329 
330     /**
331      * {@inheritDoc}
332      */
333     public EditScmResult edit(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
334         return this.getProviderByRepository(repository).edit(repository, fileSet);
335     }
336 
337     /**
338      * {@inheritDoc}
339      */
340     public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
341         return this.getProviderByRepository(repository).export(repository, fileSet);
342     }
343 
344     /**
345      * {@inheritDoc}
346      */
347     public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
348             throws ScmException {
349         return this.getProviderByRepository(repository).export(repository, fileSet, version);
350     }
351 
352     /**
353      * {@inheritDoc}
354      */
355     public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, String outputDirectory)
356             throws ScmException {
357         return this.export(repository, fileSet, outputDirectory);
358     }
359 
360     /**
361      * {@inheritDoc}
362      */
363     public ExportScmResult export(
364             ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String outputDirectory)
365             throws ScmException {
366         return this.getProviderByRepository(repository).export(repository, fileSet, version, outputDirectory);
367     }
368 
369     /**
370      * {@inheritDoc}
371      */
372     public ListScmResult list(ScmRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion version)
373             throws ScmException {
374         return this.getProviderByRepository(repository).list(repository, fileSet, recursive, version);
375     }
376 
377     /**
378      * {@inheritDoc}
379      */
380     public RemoveScmResult remove(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
381         return this.getProviderByRepository(repository).remove(repository, fileSet, message);
382     }
383 
384     /**
385      * {@inheritDoc}
386      */
387     public StatusScmResult status(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
388         return this.getProviderByRepository(repository).status(repository, fileSet);
389     }
390 
391     /**
392      * {@inheritDoc}
393      */
394     @SuppressWarnings("deprecation")
395     public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName) throws ScmException {
396         return this.getProviderByRepository(repository).tag(repository, fileSet, tagName);
397     }
398 
399     /**
400      * {@inheritDoc}
401      */
402     @SuppressWarnings("deprecation")
403     public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName, String message)
404             throws ScmException {
405         return this.getProviderByRepository(repository).tag(repository, fileSet, tagName, message);
406     }
407 
408     /**
409      * {@inheritDoc}
410      */
411     public UnEditScmResult unedit(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
412         return this.getProviderByRepository(repository).unedit(repository, fileSet);
413     }
414 
415     /**
416      * {@inheritDoc}
417      */
418     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
419         return this.getProviderByRepository(repository).update(repository, fileSet);
420     }
421 
422     /**
423      * {@inheritDoc}
424      */
425     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
426             throws ScmException {
427         return this.getProviderByRepository(repository).update(repository, fileSet, version);
428     }
429 
430     /**
431      * {@inheritDoc}
432      */
433     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, boolean runChangelog)
434             throws ScmException {
435         return this.getProviderByRepository(repository).update(repository, fileSet, runChangelog);
436     }
437 
438     /**
439      * {@inheritDoc}
440      */
441     public UpdateScmResult update(
442             ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean runChangelog)
443             throws ScmException {
444         return this.getProviderByRepository(repository).update(repository, fileSet, version, runChangelog);
445     }
446 
447     /**
448      * {@inheritDoc}
449      */
450     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, String datePattern)
451             throws ScmException {
452         return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, datePattern);
453     }
454 
455     /**
456      * {@inheritDoc}
457      */
458     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String datePattern)
459             throws ScmException {
460         return this.getProviderByRepository(repository).update(repository, fileSet, version, datePattern);
461     }
462 
463     /**
464      * {@inheritDoc}
465      */
466     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate) throws ScmException {
467         return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, lastUpdate);
468     }
469 
470     /**
471      * {@inheritDoc}
472      */
473     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate)
474             throws ScmException {
475         return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate);
476     }
477 
478     /**
479      * {@inheritDoc}
480      */
481     public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate, String datePattern)
482             throws ScmException {
483         return this.getProviderByRepository(repository)
484                 .update(repository, fileSet, (ScmVersion) null, lastUpdate, datePattern);
485     }
486 
487     /**
488      * {@inheritDoc}
489      */
490     public UpdateScmResult update(
491             ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate, String datePattern)
492             throws ScmException {
493         return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate, datePattern);
494     }
495 
496     /**
497      * {@inheritDoc}
498      */
499     public BlameScmResult blame(ScmRepository repository, ScmFileSet fileSet, String filename) throws ScmException {
500         return this.getProviderByRepository(repository).blame(repository, fileSet, filename);
501     }
502 
503     public BlameScmResult blame(BlameScmRequest blameScmRequest) throws ScmException {
504         return this.getProviderByRepository(blameScmRequest.getScmRepository()).blame(blameScmRequest);
505     }
506 
507     /**
508      * {@inheritDoc}
509      */
510     public MkdirScmResult mkdir(ScmRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
511             throws ScmException {
512         return this.getProviderByRepository(repository).mkdir(repository, fileSet, message, createInLocal);
513     }
514 }