001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.manager;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025
026import org.apache.maven.scm.ScmBranch;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmVersion;
030import org.apache.maven.scm.command.add.AddScmResult;
031import org.apache.maven.scm.command.blame.BlameScmRequest;
032import org.apache.maven.scm.command.blame.BlameScmResult;
033import org.apache.maven.scm.command.branch.BranchScmResult;
034import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
035import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
036import org.apache.maven.scm.command.checkin.CheckInScmResult;
037import org.apache.maven.scm.command.checkout.CheckOutScmResult;
038import org.apache.maven.scm.command.diff.DiffScmResult;
039import org.apache.maven.scm.command.edit.EditScmResult;
040import org.apache.maven.scm.command.export.ExportScmResult;
041import org.apache.maven.scm.command.list.ListScmResult;
042import org.apache.maven.scm.command.mkdir.MkdirScmResult;
043import org.apache.maven.scm.command.remove.RemoveScmResult;
044import org.apache.maven.scm.command.status.StatusScmResult;
045import org.apache.maven.scm.command.tag.TagScmResult;
046import org.apache.maven.scm.command.unedit.UnEditScmResult;
047import org.apache.maven.scm.command.update.UpdateScmResult;
048import org.apache.maven.scm.provider.ScmProvider;
049import org.apache.maven.scm.provider.ScmProviderStub;
050import org.apache.maven.scm.repository.ScmRepository;
051import org.apache.maven.scm.repository.ScmRepositoryException;
052import org.apache.maven.scm.repository.ScmRepositoryStub;
053import org.apache.maven.scm.repository.UnknownRepositoryStructure;
054
055/**
056 * Stub implementation of ScmManager for unit testing purposes.
057 * It allows setting the expected results that the different methods will return.
058 * More information about Stubs on
059 * <a href="http://martinfowler.com/bliki/TestDouble.html">Martin Fowler's TestDouble</a>
060 *
061 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
062 *
063 */
064public class ScmManagerStub implements ScmManager {
065
066    private ScmRepository scmRepository;
067
068    private ScmProvider scmProvider;
069
070    private List<String> messages;
071
072    /**
073     * Creates a new stub with stub repository and provider, and empty list of messages
074     */
075    public ScmManagerStub() {
076        setScmRepository(new ScmRepositoryStub());
077        setScmProvider(new ScmProviderStub());
078        setMessages(new ArrayList<>(0));
079    }
080
081    public void setScmProvider(ScmProvider scmProvider) {
082        this.scmProvider = scmProvider;
083    }
084
085    public ScmProvider getScmProvider() {
086        return scmProvider;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    public void setScmProvider(String providerType, ScmProvider provider) {
093        setScmProvider(provider);
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    public void setScmProviderImplementation(String providerType, String providerImplementation) {
100        // Do nothing there
101    }
102
103    public void setScmRepository(ScmRepository scmRepository) {
104        this.scmRepository = scmRepository;
105    }
106
107    public ScmRepository getScmRepository() {
108        return scmRepository;
109    }
110
111    /**
112     * Set the messages to return in validateScmRepository
113     *
114     * @param messages <code>List</code> of <code>String</code> objects
115     */
116    public void setMessages(List<String> messages) {
117        this.messages = messages;
118    }
119
120    /**
121     * Get the messages to return in validateScmRepository
122     *
123     * @return <code>List</code> of <code>String</code> objects
124     */
125    public List<String> getMessages() {
126        return messages;
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    public ScmRepository makeScmRepository(String scmUrl) throws ScmRepositoryException, NoSuchScmProviderException {
133        return getScmRepository();
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    public ScmRepository makeProviderScmRepository(String providerType, File path)
140            throws ScmRepositoryException, UnknownRepositoryStructure, NoSuchScmProviderException {
141        return getScmRepository();
142    }
143
144    /**
145     * Returns the same list as getMessages()
146     *
147     * @param scmUrl ignored
148     * @return <code>List</code> of <code>String</code> objects, the same list returned by getMessages()
149     */
150    public List<String> validateScmRepository(String scmUrl) {
151        return getMessages();
152    }
153
154    /**
155     * {@inheritDoc}
156     */
157    public ScmProvider getProviderByUrl(String scmUrl) throws ScmRepositoryException, NoSuchScmProviderException {
158        return getScmProvider();
159    }
160
161    /**
162     * {@inheritDoc}
163     */
164    public ScmProvider getProviderByType(String providerType) throws NoSuchScmProviderException {
165        return getScmProvider();
166    }
167
168    /**
169     * {@inheritDoc}
170     */
171    public ScmProvider getProviderByRepository(ScmRepository repository) throws NoSuchScmProviderException {
172        return getScmProvider();
173    }
174
175    /**
176     * {@inheritDoc}
177     */
178    public AddScmResult add(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
179        return this.getProviderByRepository(repository).add(repository, fileSet);
180    }
181
182    /**
183     * {@inheritDoc}
184     */
185    public AddScmResult add(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
186        return this.getProviderByRepository(repository).add(repository, fileSet, message);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    @SuppressWarnings("deprecation")
193    public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName) throws ScmException {
194        return this.getProviderByRepository(repository).branch(repository, fileSet, branchName);
195    }
196
197    /**
198     * {@inheritDoc}
199     */
200    @SuppressWarnings("deprecation")
201    public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName, String message)
202            throws ScmException {
203        return this.getProviderByRepository(repository).branch(repository, fileSet, branchName, message);
204    }
205
206    /**
207     * {@inheritDoc}
208     */
209    public ChangeLogScmResult changeLog(
210            ScmRepository repository, ScmFileSet fileSet, Date startDate, Date endDate, int numDays, ScmBranch branch)
211            throws ScmException {
212        return this.getProviderByRepository(repository)
213                .changeLog(repository, fileSet, startDate, endDate, numDays, branch);
214    }
215
216    /**
217     * {@inheritDoc}
218     */
219    public ChangeLogScmResult changeLog(
220            ScmRepository repository,
221            ScmFileSet fileSet,
222            Date startDate,
223            Date endDate,
224            int numDays,
225            ScmBranch branch,
226            String datePattern)
227            throws ScmException {
228        return this.getProviderByRepository(repository)
229                .changeLog(repository, fileSet, startDate, endDate, numDays, branch, datePattern);
230    }
231
232    /**
233     * {@inheritDoc}
234     */
235    public ChangeLogScmResult changeLog(ChangeLogScmRequest request) throws ScmException {
236        final ScmRepository repository = request.getScmRepository();
237        return this.getProviderByRepository(repository).changeLog(request);
238    }
239
240    /**
241     * {@inheritDoc}
242     */
243    public ChangeLogScmResult changeLog(
244            ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
245            throws ScmException {
246        return this.getProviderByRepository(repository).changeLog(repository, fileSet, startVersion, endVersion);
247    }
248
249    /**
250     * {@inheritDoc}
251     */
252    public ChangeLogScmResult changeLog(
253            ScmRepository repository,
254            ScmFileSet fileSet,
255            ScmVersion startRevision,
256            ScmVersion endRevision,
257            String datePattern)
258            throws ScmException {
259        return this.getProviderByRepository(repository)
260                .changeLog(repository, fileSet, startRevision, endRevision, datePattern);
261    }
262
263    /**
264     * {@inheritDoc}
265     */
266    public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
267        return this.getProviderByRepository(repository).checkIn(repository, fileSet, message);
268    }
269
270    /**
271     * {@inheritDoc}
272     */
273    public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, ScmVersion revision, String message)
274            throws ScmException {
275        return this.getProviderByRepository(repository).checkIn(repository, fileSet, revision, message);
276    }
277
278    /**
279     * {@inheritDoc}
280     */
281    public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
282        return this.getProviderByRepository(repository).checkOut(repository, fileSet);
283    }
284
285    /**
286     * {@inheritDoc}
287     */
288    public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
289            throws ScmException {
290        return this.getProviderByRepository(repository).checkOut(repository, fileSet, version);
291    }
292
293    /**
294     * {@inheritDoc}
295     */
296    public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, boolean recursive)
297            throws ScmException {
298        return this.getProviderByRepository(repository).checkOut(repository, fileSet, recursive);
299    }
300
301    /**
302     * {@inheritDoc}
303     */
304    public CheckOutScmResult checkOut(
305            ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean recursive) throws ScmException {
306        return this.getProviderByRepository(repository).checkOut(repository, fileSet, version, recursive);
307    }
308
309    /**
310     * {@inheritDoc}
311     */
312    public DiffScmResult diff(
313            ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
314            throws ScmException {
315        return this.getProviderByRepository(repository).diff(repository, fileSet, startVersion, endVersion);
316    }
317
318    /**
319     * {@inheritDoc}
320     */
321    public EditScmResult edit(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
322        return this.getProviderByRepository(repository).edit(repository, fileSet);
323    }
324
325    /**
326     * {@inheritDoc}
327     */
328    public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
329        return this.getProviderByRepository(repository).export(repository, fileSet);
330    }
331
332    /**
333     * {@inheritDoc}
334     */
335    public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
336            throws ScmException {
337        return this.getProviderByRepository(repository).export(repository, fileSet, version);
338    }
339
340    /**
341     * {@inheritDoc}
342     */
343    public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, String outputDirectory)
344            throws ScmException {
345        return this.export(repository, fileSet, outputDirectory);
346    }
347
348    /**
349     * {@inheritDoc}
350     */
351    public ExportScmResult export(
352            ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String outputDirectory)
353            throws ScmException {
354        return this.getProviderByRepository(repository).export(repository, fileSet, version, outputDirectory);
355    }
356
357    /**
358     * {@inheritDoc}
359     */
360    public ListScmResult list(ScmRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion version)
361            throws ScmException {
362        return this.getProviderByRepository(repository).list(repository, fileSet, recursive, version);
363    }
364
365    /**
366     * {@inheritDoc}
367     */
368    public RemoveScmResult remove(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException {
369        return this.getProviderByRepository(repository).remove(repository, fileSet, message);
370    }
371
372    /**
373     * {@inheritDoc}
374     */
375    public StatusScmResult status(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
376        return this.getProviderByRepository(repository).status(repository, fileSet);
377    }
378
379    /**
380     * {@inheritDoc}
381     */
382    @SuppressWarnings("deprecation")
383    public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName) throws ScmException {
384        return this.getProviderByRepository(repository).tag(repository, fileSet, tagName);
385    }
386
387    /**
388     * {@inheritDoc}
389     */
390    @SuppressWarnings("deprecation")
391    public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName, String message)
392            throws ScmException {
393        return this.getProviderByRepository(repository).tag(repository, fileSet, tagName, message);
394    }
395
396    /**
397     * {@inheritDoc}
398     */
399    public UnEditScmResult unedit(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
400        return this.getProviderByRepository(repository).unedit(repository, fileSet);
401    }
402
403    /**
404     * {@inheritDoc}
405     */
406    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet) throws ScmException {
407        return this.getProviderByRepository(repository).update(repository, fileSet);
408    }
409
410    /**
411     * {@inheritDoc}
412     */
413    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version)
414            throws ScmException {
415        return this.getProviderByRepository(repository).update(repository, fileSet, version);
416    }
417
418    /**
419     * {@inheritDoc}
420     */
421    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, boolean runChangelog)
422            throws ScmException {
423        return this.getProviderByRepository(repository).update(repository, fileSet, runChangelog);
424    }
425
426    /**
427     * {@inheritDoc}
428     */
429    public UpdateScmResult update(
430            ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean runChangelog)
431            throws ScmException {
432        return this.getProviderByRepository(repository).update(repository, fileSet, version, runChangelog);
433    }
434
435    /**
436     * {@inheritDoc}
437     */
438    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, String datePattern)
439            throws ScmException {
440        return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, datePattern);
441    }
442
443    /**
444     * {@inheritDoc}
445     */
446    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String datePattern)
447            throws ScmException {
448        return this.getProviderByRepository(repository).update(repository, fileSet, version, datePattern);
449    }
450
451    /**
452     * {@inheritDoc}
453     */
454    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate) throws ScmException {
455        return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, lastUpdate);
456    }
457
458    /**
459     * {@inheritDoc}
460     */
461    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate)
462            throws ScmException {
463        return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate);
464    }
465
466    /**
467     * {@inheritDoc}
468     */
469    public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate, String datePattern)
470            throws ScmException {
471        return this.getProviderByRepository(repository)
472                .update(repository, fileSet, (ScmVersion) null, lastUpdate, datePattern);
473    }
474
475    /**
476     * {@inheritDoc}
477     */
478    public UpdateScmResult update(
479            ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate, String datePattern)
480            throws ScmException {
481        return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate, datePattern);
482    }
483
484    /**
485     * {@inheritDoc}
486     */
487    public BlameScmResult blame(ScmRepository repository, ScmFileSet fileSet, String filename) throws ScmException {
488        return this.getProviderByRepository(repository).blame(repository, fileSet, filename);
489    }
490
491    public BlameScmResult blame(BlameScmRequest blameScmRequest) throws ScmException {
492        return this.getProviderByRepository(blameScmRequest.getScmRepository()).blame(blameScmRequest);
493    }
494
495    /**
496     * {@inheritDoc}
497     */
498    public MkdirScmResult mkdir(ScmRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
499            throws ScmException {
500        return this.getProviderByRepository(repository).mkdir(repository, fileSet, message, createInLocal);
501    }
502}