Coverage Report - org.apache.maven.wagon.WagonTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
WagonTestCase
0 %
0/388
0 %
0/42
1,54
WagonTestCase$ProgressArgumentMatcher
0 %
0/8
0 %
0/4
1,54
 
 1  
 package org.apache.maven.wagon;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.log4j.Logger;
 23  
 import org.apache.maven.wagon.authentication.AuthenticationException;
 24  
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 25  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 26  
 import org.apache.maven.wagon.events.TransferEvent;
 27  
 import org.apache.maven.wagon.events.TransferListener;
 28  
 import org.apache.maven.wagon.observers.ChecksumObserver;
 29  
 import org.apache.maven.wagon.observers.Debug;
 30  
 import org.apache.maven.wagon.repository.Repository;
 31  
 import org.apache.maven.wagon.repository.RepositoryPermissions;
 32  
 import org.apache.maven.wagon.resource.Resource;
 33  
 import org.codehaus.plexus.PlexusTestCase;
 34  
 import org.codehaus.plexus.util.FileUtils;
 35  
 import org.easymock.AbstractMatcher;
 36  
 import org.easymock.MockControl;
 37  
 
 38  
 import java.io.File;
 39  
 import java.io.IOException;
 40  
 import java.security.NoSuchAlgorithmException;
 41  
 import java.text.SimpleDateFormat;
 42  
 import java.util.ArrayList;
 43  
 import java.util.Iterator;
 44  
 import java.util.List;
 45  
 
 46  
 /**
 47  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 48  
  * @version $Id: WagonTestCase.java 1173679 2011-09-21 14:32:35Z olamy $
 49  
  */
 50  0
 public abstract class WagonTestCase
 51  
     extends PlexusTestCase
 52  
 {
 53  0
     protected static Logger logger = Logger.getLogger( WagonTestCase.class );
 54  
 
 55  
 
 56  0
     static final class ProgressArgumentMatcher
 57  
         extends AbstractMatcher
 58  
     {
 59  
         private int size;
 60  
 
 61  
         protected boolean argumentMatches( Object expected, Object actual )
 62  
         {
 63  0
             if ( actual instanceof byte[] )
 64  
             {
 65  0
                 return true;
 66  
             }
 67  0
             if ( actual instanceof Integer )
 68  
             {
 69  0
                 size += ( (Integer) actual ).intValue();
 70  0
                 return true;
 71  
             }
 72  0
             return super.argumentMatches( expected, actual );
 73  
         }
 74  
 
 75  
         public int getSize()
 76  
         {
 77  0
             return size;
 78  
         }
 79  
     }
 80  
 
 81  0
     protected static String POM = "pom.xml";
 82  
 
 83  
     protected Repository localRepository;
 84  
 
 85  
     protected Repository testRepository;
 86  
 
 87  
     protected String localRepositoryPath;
 88  
 
 89  
     protected File sourceFile;
 90  
 
 91  
     protected File destFile;
 92  
 
 93  
     protected String resource;
 94  
 
 95  
     protected File artifactSourceFile;
 96  
 
 97  
     protected File artifactDestFile;
 98  
 
 99  
     protected ChecksumObserver checksumObserver;
 100  
 
 101  
     protected TransferListener mockTransferListener;
 102  
 
 103  
     protected MockControl mockTransferListenerControl;
 104  
 
 105  
     // ----------------------------------------------------------------------
 106  
     // Constructors
 107  
     // ----------------------------------------------------------------------
 108  
 
 109  
     protected void setUp()
 110  
         throws Exception
 111  
     {
 112  0
         checksumObserver = new ChecksumObserver();
 113  
 
 114  0
         mockTransferListenerControl = MockControl.createControl( TransferListener.class );
 115  0
         mockTransferListener = (TransferListener) mockTransferListenerControl.getMock();
 116  
 
 117  0
         super.setUp();
 118  0
     }
 119  
 
 120  
     // ----------------------------------------------------------------------
 121  
     // Methods that should be provided by subclasses for proper testing
 122  
     // ----------------------------------------------------------------------
 123  
 
 124  
     /**
 125  
      * URL of the repository. For a complete test it should point to a non existing folder so we also check for the
 126  
      * creation of new folders in the remote site. <p/> return the URL of the repository as specified by Wagon syntax
 127  
      */
 128  
     protected abstract String getTestRepositoryUrl()
 129  
         throws IOException;
 130  
 
 131  
     /**
 132  
      * Protocol id of the Wagon to use, eg. <code>scp</code>, <code>ftp</code>
 133  
      *
 134  
      * @return the protocol id
 135  
      */
 136  
     protected abstract String getProtocol();
 137  
 
 138  
     /**
 139  
      * The number of the port which should get used to start the test server
 140  
      *
 141  
      * @return the port number for the test server
 142  
      */
 143  
     protected abstract int getTestRepositoryPort();
 144  
 
 145  
     // ----------------------------------------------------------------------
 146  
     // 1. Create a local file repository which mimic a users local file
 147  
     // Repository.
 148  
     //
 149  
     // 2. Create a test repository for the type of wagon we are testing. So,
 150  
     // for example, for testing the file wagon we might have a test
 151  
     // repository url of file://${basedir}/target/file-repository.
 152  
     // ----------------------------------------------------------------------
 153  
 
 154  
     protected void setupRepositories()
 155  
         throws Exception
 156  
     {
 157  0
         resource = "test-resource";
 158  
 
 159  
         // ----------------------------------------------------------------------
 160  
         // Create the test repository for the wagon we are testing.
 161  
         // ----------------------------------------------------------------------
 162  
 
 163  0
         testRepository = new Repository();
 164  
 
 165  0
         testRepository.setUrl( getTestRepositoryUrl() );
 166  
 
 167  0
         testRepository.setPermissions( getPermissions() );
 168  
 
 169  
         // ----------------------------------------------------------------------
 170  
         // Create a test local repository.
 171  
         // ----------------------------------------------------------------------
 172  
 
 173  0
         localRepositoryPath = FileTestUtils.createDir( "local-repository" ).getPath();
 174  
 
 175  0
         localRepository = createFileRepository( "file://" + localRepositoryPath );
 176  
 
 177  0
         message( "Local repository: " + localRepository );
 178  
 
 179  0
         File f = new File( localRepositoryPath );
 180  
 
 181  0
         if ( !f.exists() )
 182  
         {
 183  0
             f.mkdirs();
 184  
         }
 185  0
     }
 186  
 
 187  
     protected void customizeContext()
 188  
         throws Exception
 189  
     {
 190  0
         getContainer().addContextValue( "test.repository", localRepositoryPath );
 191  0
     }
 192  
 
 193  
     protected void setupWagonTestingFixtures()
 194  
         throws Exception
 195  
     {
 196  0
     }
 197  
 
 198  
     protected void tearDownWagonTestingFixtures()
 199  
         throws Exception
 200  
     {
 201  0
     }
 202  
 
 203  
     // ----------------------------------------------------------------------
 204  
     //
 205  
     // ----------------------------------------------------------------------
 206  
 
 207  
     protected AuthenticationInfo getAuthInfo()
 208  
     {
 209  0
         return new AuthenticationInfo();
 210  
     }
 211  
 
 212  
     protected RepositoryPermissions getPermissions()
 213  
     {
 214  0
         return new RepositoryPermissions();
 215  
     }
 216  
 
 217  
     protected Wagon getWagon()
 218  
         throws Exception
 219  
     {
 220  0
         Wagon wagon = (Wagon) lookup( Wagon.ROLE, getProtocol() );
 221  
 
 222  0
         Debug debug = new Debug();
 223  
 
 224  0
         wagon.addSessionListener( debug );
 225  
 
 226  0
         wagon.addTransferListener( debug );
 227  
 
 228  0
         return wagon;
 229  
     }
 230  
 
 231  
     protected void message( String message )
 232  
     {
 233  0
         logger.info( message );
 234  0
     }
 235  
 
 236  
     // ----------------------------------------------------------------------
 237  
     //
 238  
     // ----------------------------------------------------------------------
 239  
 
 240  
     public void testWagon()
 241  
         throws Exception
 242  
     {
 243  0
         if ( supportsGetIfNewer() )
 244  
         {
 245  0
             setupRepositories();
 246  
 
 247  0
             setupWagonTestingFixtures();
 248  
 
 249  0
             fileRoundTripTesting();
 250  
 
 251  0
             tearDownWagonTestingFixtures();
 252  
         }
 253  0
     }
 254  
 
 255  
     public void testWagonGetIfNewerIsNewer()
 256  
         throws Exception
 257  
     {
 258  0
         if ( supportsGetIfNewer() )
 259  
         {
 260  0
             setupRepositories();
 261  0
             setupWagonTestingFixtures();
 262  0
             int expectedSize = putFile();
 263  0
             getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false,
 264  
                         expectedSize );
 265  
         }
 266  0
     }
 267  
 
 268  
     protected boolean supportsGetIfNewer()
 269  
     {
 270  0
         return true;
 271  
     }
 272  
 
 273  
     public void testWagonGetIfNewerIsOlder()
 274  
         throws Exception
 275  
     {
 276  0
         if ( supportsGetIfNewer() )
 277  
         {
 278  0
             setupRepositories();
 279  0
             setupWagonTestingFixtures();
 280  0
             int expectedSize = putFile();
 281  0
             getIfNewer( new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2006-01-01" ).getTime(), true, expectedSize );
 282  
         }
 283  0
     }
 284  
 
 285  
     public void testWagonGetIfNewerIsSame()
 286  
         throws Exception
 287  
     {
 288  0
         if ( supportsGetIfNewer() )
 289  
         {
 290  0
             setupRepositories();
 291  0
             setupWagonTestingFixtures();
 292  0
             int expectedSize = putFile();
 293  0
             getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false, expectedSize );
 294  
         }
 295  0
     }
 296  
 
 297  
     private void getIfNewer( long timestamp, boolean expectedResult, int expectedSize )
 298  
         throws Exception
 299  
     {
 300  0
         Wagon wagon = getWagon();
 301  
 
 302  0
         ProgressArgumentMatcher progressArgumentMatcher = setupGetIfNewerTest( wagon, expectedResult, expectedSize );
 303  
 
 304  0
         connectWagon( wagon );
 305  
 
 306  0
         boolean result = wagon.getIfNewer( this.resource, destFile, timestamp );
 307  0
         assertEquals( expectedResult, result );
 308  
 
 309  0
         disconnectWagon( wagon );
 310  
 
 311  0
         assertGetIfNewerTest( progressArgumentMatcher, expectedResult, expectedSize );
 312  
 
 313  0
         tearDownWagonTestingFixtures();
 314  0
     }
 315  
 
 316  
     protected void assertGetIfNewerTest( ProgressArgumentMatcher progressArgumentMatcher, boolean expectedResult,
 317  
                                          int expectedSize )
 318  
         throws IOException
 319  
     {
 320  0
         if ( expectedResult )
 321  
         {
 322  0
             verifyMock( progressArgumentMatcher, expectedSize );
 323  
 
 324  0
             assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
 325  
 
 326  0
             assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9",
 327  
                           checksumObserver.getActualChecksum() );
 328  
 
 329  
             // Now compare the contents of the artifact that was placed in
 330  
             // the repository with the contents of the artifact that was
 331  
             // retrieved from the repository.
 332  
 
 333  0
             String sourceContent = FileUtils.fileRead( sourceFile );
 334  0
             String destContent = FileUtils.fileRead( destFile );
 335  0
             assertEquals( sourceContent, destContent );
 336  0
         }
 337  
         else
 338  
         {
 339  0
             mockTransferListenerControl.verify();
 340  
 
 341  0
             mockTransferListenerControl.reset();
 342  
 
 343  0
             assertNull( "check checksum is null", checksumObserver.getActualChecksum() );
 344  
 
 345  0
             assertFalse( destFile.exists() );
 346  
         }
 347  0
     }
 348  
 
 349  
     protected ProgressArgumentMatcher setupGetIfNewerTest( Wagon wagon, boolean expectedResult, int expectedSize )
 350  
         throws NoSuchAlgorithmException, IOException
 351  
     {
 352  0
         checksumObserver = new ChecksumObserver();
 353  
 
 354  0
         destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 355  0
         destFile.delete();
 356  0
         assertFalse( destFile.exists() );
 357  0
         destFile.deleteOnExit();
 358  
 
 359  0
         ProgressArgumentMatcher progressArgumentMatcher = null;
 360  0
         if ( expectedResult )
 361  
         {
 362  0
             progressArgumentMatcher = replaceMockForGet( wagon, expectedSize );
 363  
         }
 364  
         else
 365  
         {
 366  0
             replaceMockForSkippedGetIfNewer( wagon, expectedSize );
 367  
         }
 368  0
         return progressArgumentMatcher;
 369  
     }
 370  
 
 371  
     private void replaceMockForSkippedGetIfNewer( Wagon wagon, int expectedSize )
 372  
     {
 373  0
         Resource resource = new Resource( this.resource );
 374  0
         mockTransferListener.transferInitiated(
 375  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_INITIATED, TransferEvent.REQUEST_GET,
 376  
                                  destFile ) );
 377  0
         resource = new Resource( this.resource );
 378  0
         resource.setContentLength( getExpectedContentLengthOnGet( expectedSize ) );
 379  0
         resource.setLastModified( getExpectedLastModifiedOnGet( testRepository, resource ) );
 380  
         // TODO: transfer skipped event?
 381  
         // mockTransferListener.transferSkipped( createTransferEvent( wagon, resource, TransferEvent.TRANSFER_STARTED,
 382  
         // TransferEvent.REQUEST_GET, destFile ) );
 383  
 
 384  0
         mockTransferListener.debug( null );
 385  0
         mockTransferListenerControl.setMatcher( MockControl.ALWAYS_MATCHER );
 386  0
         mockTransferListenerControl.setVoidCallable( MockControl.ZERO_OR_MORE );
 387  
 
 388  0
         mockTransferListenerControl.replay();
 389  0
     }
 390  
 
 391  
     public void testWagonPutDirectory()
 392  
         throws Exception
 393  
     {
 394  0
         setupRepositories();
 395  
 
 396  0
         setupWagonTestingFixtures();
 397  
 
 398  0
         Wagon wagon = getWagon();
 399  
 
 400  0
         if ( wagon.supportsDirectoryCopy() )
 401  
         {
 402  0
             sourceFile = new File( FileTestUtils.getTestOutputDir(), "directory-copy" );
 403  
 
 404  0
             FileUtils.deleteDirectory( sourceFile );
 405  
 
 406  0
             writeTestFile( "test-resource-1.txt" );
 407  0
             writeTestFile( "a/test-resource-2.txt" );
 408  0
             writeTestFile( "a/b/test-resource-3.txt" );
 409  0
             writeTestFile( "c/test-resource-4.txt" );
 410  0
             writeTestFile( "d/e/f/test-resource-5.txt" );
 411  
 
 412  0
             wagon.connect( testRepository, getAuthInfo() );
 413  
 
 414  0
             wagon.putDirectory( sourceFile, "directory-copy" );
 415  
 
 416  0
             destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 417  
 
 418  0
             destFile.deleteOnExit();
 419  
 
 420  0
             wagon.get( "directory-copy/test-resource-1.txt", destFile );
 421  0
             wagon.get( "directory-copy/a/test-resource-2.txt", destFile );
 422  0
             wagon.get( "directory-copy/a/b/test-resource-3.txt", destFile );
 423  0
             wagon.get( "directory-copy/c/test-resource-4.txt", destFile );
 424  0
             wagon.get( "directory-copy/d/e/f/test-resource-5.txt", destFile );
 425  
 
 426  0
             wagon.disconnect();
 427  
         }
 428  
 
 429  0
         tearDownWagonTestingFixtures();
 430  0
     }
 431  
 
 432  
     /**
 433  
      * Test for putting a directory with a destination that multiple directories deep, all of which haven't been
 434  
      * created.
 435  
      *
 436  
      * @throws Exception
 437  
      * @since 1.0-beta-2
 438  
      */
 439  
     public void testWagonPutDirectoryDeepDestination()
 440  
         throws Exception
 441  
     {
 442  0
         setupRepositories();
 443  
 
 444  0
         setupWagonTestingFixtures();
 445  
 
 446  0
         Wagon wagon = getWagon();
 447  
 
 448  0
         if ( wagon.supportsDirectoryCopy() )
 449  
         {
 450  0
             sourceFile = new File( FileTestUtils.getTestOutputDir(), "deep0/deep1/deep2" );
 451  
 
 452  0
             FileUtils.deleteDirectory( sourceFile );
 453  
 
 454  0
             writeTestFile( "test-resource-1.txt" );
 455  0
             writeTestFile( "a/test-resource-2.txt" );
 456  0
             writeTestFile( "a/b/test-resource-3.txt" );
 457  0
             writeTestFile( "c/test-resource-4.txt" );
 458  0
             writeTestFile( "d/e/f/test-resource-5.txt" );
 459  
 
 460  0
             wagon.connect( testRepository, getAuthInfo() );
 461  
 
 462  0
             wagon.putDirectory( sourceFile, "deep0/deep1/deep2" );
 463  
 
 464  0
             destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 465  
 
 466  0
             destFile.deleteOnExit();
 467  
 
 468  0
             wagon.get( "deep0/deep1/deep2/test-resource-1.txt", destFile );
 469  0
             wagon.get( "deep0/deep1/deep2/a/test-resource-2.txt", destFile );
 470  0
             wagon.get( "deep0/deep1/deep2/a/b/test-resource-3.txt", destFile );
 471  0
             wagon.get( "deep0/deep1/deep2/c/test-resource-4.txt", destFile );
 472  0
             wagon.get( "deep0/deep1/deep2/d/e/f/test-resource-5.txt", destFile );
 473  
 
 474  0
             wagon.disconnect();
 475  
         }
 476  
 
 477  0
         tearDownWagonTestingFixtures();
 478  0
     }
 479  
 
 480  
     /**
 481  
      * Test that when putting a directory that already exists new files get also copied
 482  
      *
 483  
      * @throws Exception
 484  
      * @since 1.0-beta-1
 485  
      */
 486  
     public void testWagonPutDirectoryWhenDirectoryAlreadyExists()
 487  
         throws Exception
 488  
     {
 489  
 
 490  0
         final String dirName = "directory-copy-existing";
 491  
 
 492  0
         final String resourceToCreate = "test-resource-1.txt";
 493  
 
 494  0
         final String[] resources = { "a/test-resource-2.txt", "a/b/test-resource-3.txt", "c/test-resource-4.txt" };
 495  
 
 496  0
         setupRepositories();
 497  
 
 498  0
         setupWagonTestingFixtures();
 499  
 
 500  0
         Wagon wagon = getWagon();
 501  
 
 502  0
         if ( wagon.supportsDirectoryCopy() )
 503  
         {
 504  0
             sourceFile = new File( FileTestUtils.getTestOutputDir(), dirName );
 505  
 
 506  0
             FileUtils.deleteDirectory( sourceFile );
 507  
 
 508  0
             createDirectory( wagon, resourceToCreate, dirName );
 509  
 
 510  0
             for ( int i = 0; i < resources.length; i++ )
 511  
             {
 512  0
                 writeTestFile( resources[i] );
 513  
             }
 514  
 
 515  0
             wagon.connect( testRepository, getAuthInfo() );
 516  
 
 517  0
             wagon.putDirectory( sourceFile, dirName );
 518  
 
 519  0
             List resourceNames = new ArrayList( resources.length + 1 );
 520  
 
 521  0
             resourceNames.add( dirName + "/" + resourceToCreate );
 522  0
             for ( int i = 0; i < resources.length; i++ )
 523  
             {
 524  0
                 resourceNames.add( dirName + "/" + resources[i] );
 525  
             }
 526  
 
 527  0
             assertResourcesAreInRemoteSide( wagon, resourceNames );
 528  
 
 529  0
             wagon.disconnect();
 530  
         }
 531  
 
 532  0
         tearDownWagonTestingFixtures();
 533  0
     }
 534  
 
 535  
     /**
 536  
      * Test that when putting a directory that already exists new files get also copied and destination is "."
 537  
      *
 538  
      * @throws Exception
 539  
      * @since 1.0-beta-1
 540  
      */
 541  
     public void testWagonPutDirectoryForDot()
 542  
         throws Exception
 543  
     {
 544  0
         final String resourceToCreate = "test-resource-1.txt";
 545  
 
 546  0
         final String[] resources = { "a/test-resource-2.txt", "a/b/test-resource-3.txt", "c/test-resource-4.txt" };
 547  
 
 548  0
         setupRepositories();
 549  
 
 550  0
         setupWagonTestingFixtures();
 551  
 
 552  0
         Wagon wagon = getWagon();
 553  
 
 554  0
         if ( wagon.supportsDirectoryCopy() )
 555  
         {
 556  0
             sourceFile = new File( FileTestUtils.getTestOutputDir(), "dot-repo" );
 557  
 
 558  0
             FileUtils.deleteDirectory( sourceFile );
 559  
 
 560  0
             createDirectory( wagon, resourceToCreate, "." );
 561  
 
 562  0
             for ( int i = 0; i < resources.length; i++ )
 563  
             {
 564  0
                 writeTestFile( resources[i] );
 565  
             }
 566  
 
 567  0
             wagon.connect( testRepository, getAuthInfo() );
 568  
 
 569  0
             wagon.putDirectory( sourceFile, "." );
 570  
 
 571  0
             List<String> resourceNames = new ArrayList<String>( resources.length + 1 );
 572  
 
 573  0
             resourceNames.add( resourceToCreate );
 574  0
             for ( int i = 0; i < resources.length; i++ )
 575  
             {
 576  0
                 resourceNames.add( resources[i] );
 577  
             }
 578  
 
 579  0
             assertResourcesAreInRemoteSide( wagon, resourceNames );
 580  
 
 581  0
             wagon.disconnect();
 582  
         }
 583  
 
 584  0
         tearDownWagonTestingFixtures();
 585  0
     }
 586  
 
 587  
     /**
 588  
      * Create a directory with a resource and check that the other ones don't exist
 589  
      *
 590  
      * @param wagon
 591  
      * @param resourceToCreate name of the resource to be created
 592  
      * @param dirName          directory name to create
 593  
      * @throws Exception
 594  
      */
 595  
     protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
 596  
         throws Exception
 597  
     {
 598  0
         writeTestFile( resourceToCreate );
 599  0
     }
 600  
 
 601  
     protected void assertResourcesAreInRemoteSide( Wagon wagon, List<String> resourceNames )
 602  
         throws IOException, TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 603  
     {
 604  0
         Iterator<String> iter = resourceNames.iterator();
 605  0
         while ( iter.hasNext() )
 606  
         {
 607  0
             String resourceName = iter.next();
 608  
 
 609  0
             File destFile = FileTestUtils.createUniqueFile( getName(), resourceName );
 610  
 
 611  0
             destFile.deleteOnExit();
 612  
 
 613  0
             wagon.get( resourceName, destFile );
 614  0
         }
 615  0
     }
 616  
 
 617  
     /**
 618  
      * Assert that a resource does not exist in the remote wagon system
 619  
      *
 620  
      * @param wagon        wagon to get the resource from
 621  
      * @param resourceName name of the resource
 622  
      * @throws IOException             if a temp file can't be created
 623  
      * @throws AuthorizationException
 624  
      * @throws TransferFailedException
 625  
      * @since 1.0-beta-1
 626  
      */
 627  
     protected void assertNotExists( Wagon wagon, String resourceName )
 628  
         throws IOException, TransferFailedException, AuthorizationException
 629  
     {
 630  0
         File tmpFile = File.createTempFile( "wagon", null );
 631  
         try
 632  
         {
 633  0
             wagon.get( resourceName, tmpFile );
 634  0
             fail( "Resource exists: " + resourceName );
 635  
         }
 636  0
         catch ( ResourceDoesNotExistException e )
 637  
         {
 638  
             // ok
 639  
         }
 640  
         finally
 641  
         {
 642  0
             tmpFile.delete();
 643  0
         }
 644  0
     }
 645  
 
 646  
     private void writeTestFile( String child )
 647  
         throws IOException
 648  
     {
 649  0
         File dir = new File( sourceFile, child );
 650  0
         dir.getParentFile().mkdirs();
 651  0
         FileUtils.fileWrite( dir.getAbsolutePath(), child );
 652  0
     }
 653  
 
 654  
     public void testFailedGet()
 655  
         throws Exception
 656  
     {
 657  0
         setupRepositories();
 658  
 
 659  0
         setupWagonTestingFixtures();
 660  
 
 661  0
         message( "Getting test artifact from test repository " + testRepository );
 662  
 
 663  0
         Wagon wagon = getWagon();
 664  
 
 665  0
         wagon.addTransferListener( checksumObserver );
 666  
 
 667  0
         wagon.connect( testRepository, getAuthInfo() );
 668  
 
 669  0
         destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 670  
 
 671  0
         destFile.deleteOnExit();
 672  
 
 673  
         try
 674  
         {
 675  0
             wagon.get( "fubar.txt", destFile );
 676  0
             fail( "File was found when it shouldn't have been" );
 677  
         }
 678  0
         catch ( ResourceDoesNotExistException e )
 679  
         {
 680  
             // expected
 681  0
             assertTrue( true );
 682  
         }
 683  
         finally
 684  
         {
 685  0
             wagon.removeTransferListener( checksumObserver );
 686  
 
 687  0
             wagon.disconnect();
 688  
 
 689  0
             tearDownWagonTestingFixtures();
 690  0
         }
 691  0
     }
 692  
 
 693  
     public void testFailedGetIfNewer()
 694  
         throws Exception
 695  
     {
 696  0
         if ( supportsGetIfNewer() )
 697  
         {
 698  0
             setupRepositories();
 699  0
             setupWagonTestingFixtures();
 700  0
             message( "Getting test artifact from test repository " + testRepository );
 701  0
             Wagon wagon = getWagon();
 702  0
             wagon.addTransferListener( checksumObserver );
 703  0
             wagon.connect( testRepository, getAuthInfo() );
 704  0
             destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 705  0
             destFile.deleteOnExit();
 706  
             try
 707  
             {
 708  0
                 wagon.getIfNewer( "fubar.txt", destFile, 0 );
 709  0
                 fail( "File was found when it shouldn't have been" );
 710  
             }
 711  0
             catch ( ResourceDoesNotExistException e )
 712  
             {
 713  
                 // expected
 714  0
                 assertTrue( true );
 715  
             }
 716  
             finally
 717  
             {
 718  0
                 wagon.removeTransferListener( checksumObserver );
 719  
 
 720  0
                 wagon.disconnect();
 721  
 
 722  0
                 tearDownWagonTestingFixtures();
 723  0
             }
 724  
         }
 725  0
     }
 726  
 
 727  
     /**
 728  
      * Test {@link Wagon#getFileList(String)}.
 729  
      *
 730  
      * @throws Exception
 731  
      * @since 1.0-beta-2
 732  
      */
 733  
     public void testWagonGetFileList()
 734  
         throws Exception
 735  
     {
 736  0
         setupRepositories();
 737  
 
 738  0
         setupWagonTestingFixtures();
 739  
 
 740  0
         String dirName = "file-list";
 741  
 
 742  0
         String filenames[] =
 743  
             new String[]{ "test-resource.txt", "test-resource.pom", "test-resource b.txt", "more-resources.dat",
 744  
                 ".index.txt" };
 745  
 
 746  0
         for ( int i = 0; i < filenames.length; i++ )
 747  
         {
 748  0
             putFile( dirName + "/" + filenames[i], dirName + "/" + filenames[i], filenames[i] + "\n" );
 749  
         }
 750  
 
 751  0
         Wagon wagon = getWagon();
 752  
 
 753  0
         wagon.connect( testRepository, getAuthInfo() );
 754  
 
 755  0
         List<String> list = wagon.getFileList( dirName );
 756  0
         assertNotNull( "file list should not be null.", list );
 757  0
         assertTrue( "file list should contain more items (actually contains '" + list + "').",
 758  
                     list.size() >= filenames.length );
 759  
 
 760  0
         for ( int i = 0; i < filenames.length; i++ )
 761  
         {
 762  0
             assertTrue( "Filename '" + filenames[i] + "' should be in list.", list.contains( filenames[i] ) );
 763  
         }
 764  
 
 765  
         // WAGON-250
 766  0
         list = wagon.getFileList( "" );
 767  0
         assertNotNull( "file list should not be null.", list );
 768  0
         assertTrue( "file list should contain items (actually contains '" + list + "').", !list.isEmpty() );
 769  0
         assertTrue( list.contains( "file-list/" ) );
 770  0
         assertFalse( list.contains( "file-list" ) );
 771  0
         assertFalse( list.contains( "." ) );
 772  0
         assertFalse( list.contains( ".." ) );
 773  0
         assertFalse( list.contains( "./" ) );
 774  0
         assertFalse( list.contains( "../" ) );
 775  
 
 776  0
         wagon.disconnect();
 777  
 
 778  0
         tearDownWagonTestingFixtures();
 779  0
     }
 780  
 
 781  
     /**
 782  
      * Test {@link Wagon#getFileList(String)} when the directory does not exist.
 783  
      *
 784  
      * @throws Exception
 785  
      * @since 1.0-beta-2
 786  
      */
 787  
     public void testWagonGetFileListWhenDirectoryDoesNotExist()
 788  
         throws Exception
 789  
     {
 790  0
         setupRepositories();
 791  
 
 792  0
         setupWagonTestingFixtures();
 793  
 
 794  0
         String dirName = "file-list-unexisting";
 795  
 
 796  0
         Wagon wagon = getWagon();
 797  
 
 798  0
         wagon.connect( testRepository, getAuthInfo() );
 799  
 
 800  
         try
 801  
         {
 802  0
             wagon.getFileList( dirName );
 803  0
             fail( "getFileList on unexisting directory must throw ResourceDoesNotExistException" );
 804  
         }
 805  0
         catch ( ResourceDoesNotExistException e )
 806  
         {
 807  
             // expected
 808  
         }
 809  
         finally
 810  
         {
 811  0
             wagon.disconnect();
 812  
 
 813  0
             tearDownWagonTestingFixtures();
 814  0
         }
 815  0
     }
 816  
 
 817  
     /**
 818  
      * Test for an existing resource.
 819  
      *
 820  
      * @throws Exception
 821  
      * @since 1.0-beta-2
 822  
      */
 823  
     public void testWagonResourceExists()
 824  
         throws Exception
 825  
     {
 826  0
         setupRepositories();
 827  
 
 828  0
         setupWagonTestingFixtures();
 829  
 
 830  0
         Wagon wagon = getWagon();
 831  
 
 832  0
         putFile();
 833  
 
 834  0
         wagon.connect( testRepository, getAuthInfo() );
 835  
 
 836  0
         assertTrue( sourceFile.getName() + " does not exist", wagon.resourceExists( sourceFile.getName() ) );
 837  
 
 838  0
         wagon.disconnect();
 839  
 
 840  0
         tearDownWagonTestingFixtures();
 841  0
     }
 842  
 
 843  
     /**
 844  
      * Test for an invalid resource.
 845  
      *
 846  
      * @throws Exception
 847  
      * @since 1.0-beta-2
 848  
      */
 849  
     public void testWagonResourceNotExists()
 850  
         throws Exception
 851  
     {
 852  0
         setupRepositories();
 853  
 
 854  0
         setupWagonTestingFixtures();
 855  
 
 856  0
         Wagon wagon = getWagon();
 857  
 
 858  0
         wagon.connect( testRepository, getAuthInfo() );
 859  
 
 860  0
         assertFalse( wagon.resourceExists( "a/bad/resource/name/that/should/not/exist.txt" ) );
 861  
 
 862  0
         wagon.disconnect();
 863  
 
 864  0
         tearDownWagonTestingFixtures();
 865  0
     }
 866  
 
 867  
     // ----------------------------------------------------------------------
 868  
     // File <--> File round trip testing
 869  
     // ----------------------------------------------------------------------
 870  
     // We are testing taking a file, our sourcefile, and placing it into the
 871  
     // test repository that we have setup.
 872  
     // ----------------------------------------------------------------------
 873  
 
 874  
     protected void putFile( String resourceName, String testFileName, String content )
 875  
         throws Exception
 876  
     {
 877  0
         sourceFile = new File( FileTestUtils.getTestOutputDir(), testFileName );
 878  0
         sourceFile.getParentFile().mkdirs();
 879  0
         FileUtils.fileWrite( sourceFile.getAbsolutePath(), content );
 880  
 
 881  0
         Wagon wagon = getWagon();
 882  
 
 883  0
         ProgressArgumentMatcher progressArgumentMatcher = replayMockForPut( resourceName, content, wagon );
 884  
 
 885  0
         message( "Putting test artifact: " + resourceName + " into test repository " + testRepository );
 886  
 
 887  0
         connectWagon( wagon );
 888  
 
 889  0
         wagon.put( sourceFile, resourceName );
 890  
 
 891  0
         disconnectWagon( wagon );
 892  
 
 893  0
         verifyMock( progressArgumentMatcher, content.length() );
 894  0
     }
 895  
 
 896  
     protected ProgressArgumentMatcher replayMockForPut( String resourceName, String content, Wagon wagon )
 897  
     {
 898  0
         Resource resource = new Resource( resourceName );
 899  0
         mockTransferListener.transferInitiated(
 900  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_INITIATED, TransferEvent.REQUEST_PUT,
 901  
                                  sourceFile ) );
 902  0
         resource = new Resource( resourceName );
 903  0
         resource.setContentLength( content.length() );
 904  0
         resource.setLastModified( sourceFile.lastModified() );
 905  0
         mockTransferListener.transferStarted(
 906  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_STARTED, TransferEvent.REQUEST_PUT,
 907  
                                  sourceFile ) );
 908  0
         mockTransferListener.transferProgress(
 909  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_PUT,
 910  
                                  sourceFile ), new byte[]{ }, 0 );
 911  0
         ProgressArgumentMatcher progressArgumentMatcher = new ProgressArgumentMatcher();
 912  0
         mockTransferListenerControl.setMatcher( progressArgumentMatcher );
 913  
 
 914  0
         mockTransferListener.debug( null );
 915  0
         mockTransferListenerControl.setMatcher( MockControl.ALWAYS_MATCHER );
 916  0
         mockTransferListenerControl.setVoidCallable( MockControl.ZERO_OR_MORE );
 917  
 
 918  0
         mockTransferListener.transferCompleted(
 919  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_COMPLETED, TransferEvent.REQUEST_PUT,
 920  
                                  sourceFile ) );
 921  
 
 922  0
         mockTransferListenerControl.replay();
 923  0
         return progressArgumentMatcher;
 924  
     }
 925  
 
 926  
     protected TransferEvent createTransferEvent( Wagon wagon, Resource resource, int eventType, int requestType,
 927  
                                                  File file )
 928  
     {
 929  0
         TransferEvent transferEvent = new TransferEvent( wagon, resource, eventType, requestType );
 930  0
         transferEvent.setLocalFile( file );
 931  0
         return transferEvent;
 932  
     }
 933  
 
 934  
     protected int putFile()
 935  
         throws Exception
 936  
     {
 937  0
         String content = "test-resource.txt\n";
 938  0
         putFile( resource, "test-resource", content );
 939  0
         return content.length();
 940  
     }
 941  
 
 942  
     protected void getFile( int expectedSize )
 943  
         throws Exception
 944  
     {
 945  0
         destFile = FileTestUtils.createUniqueFile( getName(), getName() );
 946  0
         destFile.deleteOnExit();
 947  
 
 948  0
         Wagon wagon = getWagon();
 949  
 
 950  0
         ProgressArgumentMatcher progressArgumentMatcher = replaceMockForGet( wagon, expectedSize );
 951  
 
 952  0
         message( "Getting test artifact from test repository " + testRepository );
 953  
 
 954  0
         connectWagon( wagon );
 955  
 
 956  0
         wagon.get( this.resource, destFile );
 957  
 
 958  0
         disconnectWagon( wagon );
 959  
 
 960  0
         verifyMock( progressArgumentMatcher, expectedSize );
 961  0
     }
 962  
 
 963  
     protected void verifyMock( ProgressArgumentMatcher progressArgumentMatcher, int length )
 964  
     {
 965  0
         mockTransferListenerControl.verify();
 966  
 
 967  0
         assertEquals( length, progressArgumentMatcher.getSize() );
 968  
 
 969  0
         mockTransferListenerControl.reset();
 970  0
     }
 971  
 
 972  
     protected void disconnectWagon( Wagon wagon )
 973  
         throws ConnectionException
 974  
     {
 975  0
         wagon.removeTransferListener( mockTransferListener );
 976  
 
 977  0
         wagon.removeTransferListener( checksumObserver );
 978  
 
 979  0
         wagon.disconnect();
 980  0
     }
 981  
 
 982  
     protected void connectWagon( Wagon wagon )
 983  
         throws ConnectionException, AuthenticationException
 984  
     {
 985  0
         wagon.addTransferListener( checksumObserver );
 986  
 
 987  0
         wagon.addTransferListener( mockTransferListener );
 988  
 
 989  0
         wagon.connect( testRepository, getAuthInfo() );
 990  0
     }
 991  
 
 992  
     protected ProgressArgumentMatcher replaceMockForGet( Wagon wagon, int expectedSize )
 993  
     {
 994  0
         Resource resource = new Resource( this.resource );
 995  0
         mockTransferListener.transferInitiated(
 996  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_INITIATED, TransferEvent.REQUEST_GET,
 997  
                                  destFile ) );
 998  0
         resource = new Resource( this.resource );
 999  0
         resource.setContentLength( getExpectedContentLengthOnGet( expectedSize ) );
 1000  0
         resource.setLastModified( getExpectedLastModifiedOnGet( testRepository, resource ) );
 1001  0
         TransferEvent te =
 1002  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_STARTED, TransferEvent.REQUEST_GET, null );
 1003  0
         mockTransferListener.transferStarted( te );
 1004  0
         mockTransferListener.transferProgress(
 1005  
             new TransferEvent( wagon, resource, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_GET ),
 1006  
             new byte[]{ }, 0 );
 1007  0
         ProgressArgumentMatcher progressArgumentMatcher = new ProgressArgumentMatcher();
 1008  0
         mockTransferListenerControl.setMatcher( progressArgumentMatcher );
 1009  
 
 1010  0
         mockTransferListener.debug( null );
 1011  0
         mockTransferListenerControl.setMatcher( MockControl.ALWAYS_MATCHER );
 1012  0
         mockTransferListenerControl.setVoidCallable( MockControl.ZERO_OR_MORE );
 1013  
 
 1014  0
         mockTransferListener.transferCompleted(
 1015  
             createTransferEvent( wagon, resource, TransferEvent.TRANSFER_COMPLETED, TransferEvent.REQUEST_GET,
 1016  
                                  destFile ) );
 1017  
 
 1018  0
         mockTransferListenerControl.replay();
 1019  0
         return progressArgumentMatcher;
 1020  
     }
 1021  
 
 1022  
     protected int getExpectedContentLengthOnGet( int expectedSize )
 1023  
     {
 1024  0
         return expectedSize;
 1025  
     }
 1026  
 
 1027  
     protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
 1028  
     {
 1029  
         // default implementation - prone to failing if the time between test file creation and completion of putFile()
 1030  
         // cross the "second" boundary, causing the "remote" and local files to have different times.
 1031  
 
 1032  0
         return sourceFile.lastModified();
 1033  
     }
 1034  
 
 1035  
     protected void fileRoundTripTesting()
 1036  
         throws Exception
 1037  
     {
 1038  0
         message( "File round trip testing ..." );
 1039  
 
 1040  0
         int expectedSize = putFile();
 1041  
 
 1042  0
         assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
 1043  
 
 1044  0
         assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
 1045  
 
 1046  0
         checksumObserver = new ChecksumObserver();
 1047  
 
 1048  0
         getFile( expectedSize );
 1049  
 
 1050  0
         assertNotNull( "check checksum is not null", checksumObserver.getActualChecksum() );
 1051  
 
 1052  0
         assertEquals( "compare checksums", "6b144b7285ffd6b0bc8300da162120b9", checksumObserver.getActualChecksum() );
 1053  
 
 1054  
         // Now compare the conents of the artifact that was placed in
 1055  
         // the repository with the contents of the artifact that was
 1056  
         // retrieved from the repository.
 1057  
 
 1058  0
         String sourceContent = FileUtils.fileRead( sourceFile );
 1059  
 
 1060  0
         String destContent = FileUtils.fileRead( destFile );
 1061  
 
 1062  0
         assertEquals( sourceContent, destContent );
 1063  0
     }
 1064  
 
 1065  
     // ----------------------------------------------------------------------
 1066  
     //
 1067  
     // ----------------------------------------------------------------------
 1068  
 
 1069  
     protected Repository createFileRepository( String url )
 1070  
     {
 1071  0
         File path = new File( url.substring( 7 ) );
 1072  
 
 1073  0
         path.mkdirs();
 1074  
 
 1075  0
         Repository repository = new Repository();
 1076  
 
 1077  0
         repository.setUrl( url );
 1078  
 
 1079  0
         return repository;
 1080  
     }
 1081  
 
 1082  
 }