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