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  
26  import org.apache.maven.scm.ScmBranch;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmVersion;
30  import org.apache.maven.scm.command.add.AddScmResult;
31  import org.apache.maven.scm.command.blame.BlameScmRequest;
32  import org.apache.maven.scm.command.blame.BlameScmResult;
33  import org.apache.maven.scm.command.branch.BranchScmResult;
34  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
35  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
36  import org.apache.maven.scm.command.checkin.CheckInScmResult;
37  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
38  import org.apache.maven.scm.command.diff.DiffScmResult;
39  import org.apache.maven.scm.command.edit.EditScmResult;
40  import org.apache.maven.scm.command.export.ExportScmResult;
41  import org.apache.maven.scm.command.list.ListScmResult;
42  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
43  import org.apache.maven.scm.command.remove.RemoveScmResult;
44  import org.apache.maven.scm.command.status.StatusScmResult;
45  import org.apache.maven.scm.command.tag.TagScmResult;
46  import org.apache.maven.scm.command.unedit.UnEditScmResult;
47  import org.apache.maven.scm.command.update.UpdateScmResult;
48  import org.apache.maven.scm.provider.ScmProvider;
49  import org.apache.maven.scm.provider.ScmProviderStub;
50  import org.apache.maven.scm.repository.ScmRepository;
51  import org.apache.maven.scm.repository.ScmRepositoryException;
52  import org.apache.maven.scm.repository.ScmRepositoryStub;
53  import org.apache.maven.scm.repository.UnknownRepositoryStructure;
54  
55  /**
56   * Stub implementation of ScmManager for unit testing purposes.
57   * It allows setting the expected results that the different methods will return.
58   * More information about Stubs on
59   * <a href="http://martinfowler.com/bliki/TestDouble.html">Martin Fowler's TestDouble</a>
60   *
61   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
62   *
63   */
64  public class ScmManagerStub implements ScmManager {
65  
66      private ScmRepository scmRepository;
67  
68      private ScmProvider scmProvider;
69  
70      private List<String> messages;
71  
72      /**
73       * Creates a new stub with stub repository and provider, and empty list of messages
74       */
75      public ScmManagerStub() {
76          setScmRepository(new ScmRepositoryStub());
77          setScmProvider(new ScmProviderStub());
78          setMessages(new ArrayList<String>(0));
79      }
80  
81      public void setScmProvider(ScmProvider scmProvider) {
82          this.scmProvider = scmProvider;
83      }
84  
85      public ScmProvider getScmProvider() {
86          return scmProvider;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public void setScmProvider(String providerType, ScmProvider provider) {
93          setScmProvider(provider);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      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 }