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.CommandParameters; 027import org.apache.maven.scm.ScmBranch; 028import org.apache.maven.scm.ScmException; 029import org.apache.maven.scm.ScmFileSet; 030import org.apache.maven.scm.ScmVersion; 031import org.apache.maven.scm.command.add.AddScmResult; 032import org.apache.maven.scm.command.blame.BlameScmRequest; 033import org.apache.maven.scm.command.blame.BlameScmResult; 034import org.apache.maven.scm.command.branch.BranchScmResult; 035import org.apache.maven.scm.command.changelog.ChangeLogScmRequest; 036import org.apache.maven.scm.command.changelog.ChangeLogScmResult; 037import org.apache.maven.scm.command.checkin.CheckInScmResult; 038import org.apache.maven.scm.command.checkout.CheckOutScmResult; 039import org.apache.maven.scm.command.diff.DiffScmResult; 040import org.apache.maven.scm.command.edit.EditScmResult; 041import org.apache.maven.scm.command.export.ExportScmResult; 042import org.apache.maven.scm.command.list.ListScmResult; 043import org.apache.maven.scm.command.mkdir.MkdirScmResult; 044import org.apache.maven.scm.command.remove.RemoveScmResult; 045import org.apache.maven.scm.command.status.StatusScmResult; 046import org.apache.maven.scm.command.tag.TagScmResult; 047import org.apache.maven.scm.command.unedit.UnEditScmResult; 048import org.apache.maven.scm.command.update.UpdateScmResult; 049import org.apache.maven.scm.provider.ScmProvider; 050import org.apache.maven.scm.provider.ScmProviderStub; 051import org.apache.maven.scm.repository.ScmRepository; 052import org.apache.maven.scm.repository.ScmRepositoryException; 053import org.apache.maven.scm.repository.ScmRepositoryStub; 054import org.apache.maven.scm.repository.UnknownRepositoryStructure; 055 056/** 057 * Stub implementation of ScmManager for unit testing purposes. 058 * It allows setting the expected results that the different methods will return. 059 * More information about Stubs on 060 * <a href="http://martinfowler.com/bliki/TestDouble.html">Martin Fowler's TestDouble</a> 061 * 062 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a> 063 * 064 */ 065public class ScmManagerStub implements ScmManager { 066 067 private ScmRepository scmRepository; 068 069 private ScmProvider scmProvider; 070 071 private List<String> messages; 072 073 /** 074 * Creates a new stub with stub repository and provider, and empty list of messages 075 */ 076 public ScmManagerStub() { 077 setScmRepository(new ScmRepositoryStub()); 078 setScmProvider(new ScmProviderStub()); 079 setMessages(new ArrayList<>(0)); 080 } 081 082 public void setScmProvider(ScmProvider scmProvider) { 083 this.scmProvider = scmProvider; 084 } 085 086 public ScmProvider getScmProvider() { 087 return scmProvider; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 public void setScmProvider(String providerType, ScmProvider provider) { 094 setScmProvider(provider); 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 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 /** 146 * Returns the same list as getMessages() 147 * 148 * @param scmUrl ignored 149 * @return <code>List</code> of <code>String</code> objects, the same list returned by getMessages() 150 */ 151 public List<String> validateScmRepository(String scmUrl) { 152 return getMessages(); 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 public ScmProvider getProviderByUrl(String scmUrl) throws ScmRepositoryException, NoSuchScmProviderException { 159 return getScmProvider(); 160 } 161 162 /** 163 * {@inheritDoc} 164 */ 165 public ScmProvider getProviderByType(String providerType) throws NoSuchScmProviderException { 166 return getScmProvider(); 167 } 168 169 /** 170 * {@inheritDoc} 171 */ 172 public ScmProvider getProviderByRepository(ScmRepository repository) throws NoSuchScmProviderException { 173 return getScmProvider(); 174 } 175 176 /** 177 * {@inheritDoc} 178 */ 179 public AddScmResult add(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 180 return this.getProviderByRepository(repository).add(repository, fileSet); 181 } 182 183 /** 184 * {@inheritDoc} 185 */ 186 public AddScmResult add(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException { 187 return this.getProviderByRepository(repository).add(repository, fileSet, message); 188 } 189 190 /** 191 * {@inheritDoc} 192 */ 193 @SuppressWarnings("deprecation") 194 public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName) throws ScmException { 195 return this.getProviderByRepository(repository).branch(repository, fileSet, branchName); 196 } 197 198 /** 199 * {@inheritDoc} 200 */ 201 @SuppressWarnings("deprecation") 202 public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, String branchName, String message) 203 throws ScmException { 204 return this.getProviderByRepository(repository).branch(repository, fileSet, branchName, message); 205 } 206 207 /** 208 * {@inheritDoc} 209 */ 210 public ChangeLogScmResult changeLog( 211 ScmRepository repository, ScmFileSet fileSet, Date startDate, Date endDate, int numDays, ScmBranch branch) 212 throws ScmException { 213 return this.getProviderByRepository(repository) 214 .changeLog(repository, fileSet, startDate, endDate, numDays, branch); 215 } 216 217 /** 218 * {@inheritDoc} 219 */ 220 public ChangeLogScmResult changeLog( 221 ScmRepository repository, 222 ScmFileSet fileSet, 223 Date startDate, 224 Date endDate, 225 int numDays, 226 ScmBranch branch, 227 String datePattern) 228 throws ScmException { 229 return this.getProviderByRepository(repository) 230 .changeLog(repository, fileSet, startDate, endDate, numDays, branch, datePattern); 231 } 232 233 /** 234 * {@inheritDoc} 235 */ 236 public ChangeLogScmResult changeLog(ChangeLogScmRequest request) throws ScmException { 237 final ScmRepository repository = request.getScmRepository(); 238 return this.getProviderByRepository(repository).changeLog(request); 239 } 240 241 /** 242 * {@inheritDoc} 243 */ 244 public ChangeLogScmResult changeLog( 245 ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion) 246 throws ScmException { 247 return this.getProviderByRepository(repository).changeLog(repository, fileSet, startVersion, endVersion); 248 } 249 250 /** 251 * {@inheritDoc} 252 */ 253 public ChangeLogScmResult changeLog( 254 ScmRepository repository, 255 ScmFileSet fileSet, 256 ScmVersion startRevision, 257 ScmVersion endRevision, 258 String datePattern) 259 throws ScmException { 260 return this.getProviderByRepository(repository) 261 .changeLog(repository, fileSet, startRevision, endRevision, datePattern); 262 } 263 264 /** 265 * {@inheritDoc} 266 */ 267 public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException { 268 return this.getProviderByRepository(repository).checkIn(repository, fileSet, message); 269 } 270 271 /** 272 * {@inheritDoc} 273 */ 274 public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, ScmVersion revision, String message) 275 throws ScmException { 276 return this.getProviderByRepository(repository).checkIn(repository, fileSet, revision, message); 277 } 278 279 @Override 280 public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, CommandParameters commandParameters) 281 throws ScmException { 282 return this.getProviderByRepository(repository).checkIn(repository, fileSet, commandParameters); 283 } 284 285 /** 286 * {@inheritDoc} 287 */ 288 public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 289 return this.getProviderByRepository(repository).checkOut(repository, fileSet); 290 } 291 292 /** 293 * {@inheritDoc} 294 */ 295 public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, ScmVersion version) 296 throws ScmException { 297 return this.getProviderByRepository(repository).checkOut(repository, fileSet, version); 298 } 299 300 /** 301 * {@inheritDoc} 302 */ 303 public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet, boolean recursive) 304 throws ScmException { 305 return this.getProviderByRepository(repository).checkOut(repository, fileSet, recursive); 306 } 307 308 /** 309 * {@inheritDoc} 310 */ 311 public CheckOutScmResult checkOut( 312 ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean recursive) throws ScmException { 313 return this.getProviderByRepository(repository).checkOut(repository, fileSet, version, recursive); 314 } 315 316 /** 317 * {@inheritDoc} 318 */ 319 public DiffScmResult diff( 320 ScmRepository repository, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion) 321 throws ScmException { 322 return this.getProviderByRepository(repository).diff(repository, fileSet, startVersion, endVersion); 323 } 324 325 /** 326 * {@inheritDoc} 327 */ 328 public EditScmResult edit(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 329 return this.getProviderByRepository(repository).edit(repository, fileSet); 330 } 331 332 /** 333 * {@inheritDoc} 334 */ 335 public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 336 return this.getProviderByRepository(repository).export(repository, fileSet); 337 } 338 339 /** 340 * {@inheritDoc} 341 */ 342 public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, ScmVersion version) 343 throws ScmException { 344 return this.getProviderByRepository(repository).export(repository, fileSet, version); 345 } 346 347 /** 348 * {@inheritDoc} 349 */ 350 public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, String outputDirectory) 351 throws ScmException { 352 return this.export(repository, fileSet, outputDirectory); 353 } 354 355 /** 356 * {@inheritDoc} 357 */ 358 public ExportScmResult export( 359 ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String outputDirectory) 360 throws ScmException { 361 return this.getProviderByRepository(repository).export(repository, fileSet, version, outputDirectory); 362 } 363 364 /** 365 * {@inheritDoc} 366 */ 367 public ListScmResult list(ScmRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion version) 368 throws ScmException { 369 return this.getProviderByRepository(repository).list(repository, fileSet, recursive, version); 370 } 371 372 /** 373 * {@inheritDoc} 374 */ 375 public RemoveScmResult remove(ScmRepository repository, ScmFileSet fileSet, String message) throws ScmException { 376 return this.getProviderByRepository(repository).remove(repository, fileSet, message); 377 } 378 379 /** 380 * {@inheritDoc} 381 */ 382 public StatusScmResult status(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 383 return this.getProviderByRepository(repository).status(repository, fileSet); 384 } 385 386 /** 387 * {@inheritDoc} 388 */ 389 @SuppressWarnings("deprecation") 390 public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName) throws ScmException { 391 return this.getProviderByRepository(repository).tag(repository, fileSet, tagName); 392 } 393 394 /** 395 * {@inheritDoc} 396 */ 397 @SuppressWarnings("deprecation") 398 public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tagName, String message) 399 throws ScmException { 400 return this.getProviderByRepository(repository).tag(repository, fileSet, tagName, message); 401 } 402 403 /** 404 * {@inheritDoc} 405 */ 406 public UnEditScmResult unedit(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 407 return this.getProviderByRepository(repository).unedit(repository, fileSet); 408 } 409 410 /** 411 * {@inheritDoc} 412 */ 413 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet) throws ScmException { 414 return this.getProviderByRepository(repository).update(repository, fileSet); 415 } 416 417 /** 418 * {@inheritDoc} 419 */ 420 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version) 421 throws ScmException { 422 return this.getProviderByRepository(repository).update(repository, fileSet, version); 423 } 424 425 /** 426 * {@inheritDoc} 427 */ 428 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, boolean runChangelog) 429 throws ScmException { 430 return this.getProviderByRepository(repository).update(repository, fileSet, runChangelog); 431 } 432 433 /** 434 * {@inheritDoc} 435 */ 436 public UpdateScmResult update( 437 ScmRepository repository, ScmFileSet fileSet, ScmVersion version, boolean runChangelog) 438 throws ScmException { 439 return this.getProviderByRepository(repository).update(repository, fileSet, version, runChangelog); 440 } 441 442 /** 443 * {@inheritDoc} 444 */ 445 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, String datePattern) 446 throws ScmException { 447 return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, datePattern); 448 } 449 450 /** 451 * {@inheritDoc} 452 */ 453 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, String datePattern) 454 throws ScmException { 455 return this.getProviderByRepository(repository).update(repository, fileSet, version, datePattern); 456 } 457 458 /** 459 * {@inheritDoc} 460 */ 461 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate) throws ScmException { 462 return this.getProviderByRepository(repository).update(repository, fileSet, (ScmVersion) null, lastUpdate); 463 } 464 465 /** 466 * {@inheritDoc} 467 */ 468 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate) 469 throws ScmException { 470 return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate); 471 } 472 473 /** 474 * {@inheritDoc} 475 */ 476 public UpdateScmResult update(ScmRepository repository, ScmFileSet fileSet, Date lastUpdate, String datePattern) 477 throws ScmException { 478 return this.getProviderByRepository(repository) 479 .update(repository, fileSet, (ScmVersion) null, lastUpdate, datePattern); 480 } 481 482 /** 483 * {@inheritDoc} 484 */ 485 public UpdateScmResult update( 486 ScmRepository repository, ScmFileSet fileSet, ScmVersion version, Date lastUpdate, String datePattern) 487 throws ScmException { 488 return this.getProviderByRepository(repository).update(repository, fileSet, version, lastUpdate, datePattern); 489 } 490 491 /** 492 * {@inheritDoc} 493 */ 494 public BlameScmResult blame(ScmRepository repository, ScmFileSet fileSet, String filename) throws ScmException { 495 return this.getProviderByRepository(repository).blame(repository, fileSet, filename); 496 } 497 498 public BlameScmResult blame(BlameScmRequest blameScmRequest) throws ScmException { 499 return this.getProviderByRepository(blameScmRequest.getScmRepository()).blame(blameScmRequest); 500 } 501 502 /** 503 * {@inheritDoc} 504 */ 505 public MkdirScmResult mkdir(ScmRepository repository, ScmFileSet fileSet, String message, boolean createInLocal) 506 throws ScmException { 507 return this.getProviderByRepository(repository).mkdir(repository, fileSet, message, createInLocal); 508 } 509}