]> source.dussan.org Git - archiva.git/blob
52683e87a6e112aa522347213248c8f3c79ef287
[archiva.git] /
1 package org.apache.maven.archiva.web.action;
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 java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25
26 import org.apache.archiva.checksum.ChecksumAlgorithm;
27 import org.apache.archiva.checksum.ChecksummedFile;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
34 import org.apache.maven.archiva.database.ArchivaAuditLogsDao;
35 import org.apache.maven.archiva.model.ArchivaAuditLogs;
36 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
37 import org.apache.maven.archiva.model.SnapshotVersion;
38 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
39 import org.apache.maven.archiva.repository.RepositoryContentFactory;
40 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
41 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
42 import org.apache.maven.archiva.repository.metadata.MetadataTools;
43 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
44 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
45 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
46 import org.easymock.MockControl;
47 import org.easymock.classextension.MockClassControl;
48
49 import com.opensymphony.xwork2.Action;
50
51 /**
52  * UploadActionTest
53  */
54 public class UploadActionTest
55     extends PlexusInSpringTestCase
56 {
57     private ArchivaTaskScheduler scheduler;
58
59     private MockControl schedulerControl;
60
61     private UploadAction uploadAction;
62
63     private ArchivaConfiguration archivaConfig;
64
65     private MockControl archivaConfigControl;
66
67     private RepositoryContentFactory repoFactory;
68
69     private MockControl repoFactoryControl;
70
71     private ArchivaAuditLogsDao auditLogsDao;
72
73     private MockControl auditLogsDaoControl;
74
75     private static final String REPOSITORY_ID = "test-repo";
76
77     private Configuration config;
78
79     public void setUp()
80         throws Exception
81     {
82         super.setUp();
83
84         schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
85         scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
86
87         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
88         archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
89
90         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
91         repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
92
93         auditLogsDaoControl = MockControl.createControl( ArchivaAuditLogsDao.class );
94         auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
95         auditLogsDao = (ArchivaAuditLogsDao) auditLogsDaoControl.getMock();
96
97         uploadAction = new UploadAction();
98         uploadAction.setScheduler( scheduler );
99         uploadAction.setConfiguration( archivaConfig );
100         uploadAction.setRepositoryFactory( repoFactory );
101         uploadAction.setAuditLogsDao( auditLogsDao );
102
103         File testRepo = new File( getBasedir(), "target/test-classes/test-repo" );
104         testRepo.mkdirs();
105
106         assertTrue( testRepo.exists() );
107
108         config = new Configuration();
109         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
110         repoConfig.setId( REPOSITORY_ID );
111         repoConfig.setLayout( "default" );
112         repoConfig.setLocation( testRepo.getPath() );
113         repoConfig.setName( REPOSITORY_ID );
114         repoConfig.setBlockRedeployments( true );
115         config.addManagedRepository( repoConfig );
116
117         RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
118         repoScanning.setKnownContentConsumers( new ArrayList<String>() );
119         config.setRepositoryScanning( repoScanning );
120     }
121
122     public void tearDown()
123         throws Exception
124     {
125         File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
126         FileUtils.deleteDirectory( testRepo );
127
128         assertFalse( testRepo.exists() );
129
130         super.tearDown();
131     }
132
133     private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
134                                       boolean generatePom )
135     {
136         uploadAction.setRepositoryId( REPOSITORY_ID );
137         uploadAction.setGroupId( "org.apache.archiva" );
138         uploadAction.setArtifactId( "artifact-upload" );
139         uploadAction.setVersion( version );
140         uploadAction.setPackaging( "jar" );
141
142         uploadAction.setClassifier( classifier );
143         uploadAction.setArtifact( artifact );
144         uploadAction.setPom( pomFile );
145         uploadAction.setGeneratePom( generatePom );
146     }
147
148     private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation, String artifact,
149                                                                         String version )
150     {
151         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar" ).exists() );
152         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
153             + ".jar.sha1" ).exists() );
154         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
155             + ".jar.md5" ).exists() );
156
157         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom" ).exists() );
158         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
159             + ".pom.sha1" ).exists() );
160         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
161             + ".pom.md5" ).exists() );
162
163         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
164         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
165             + ".sha1" ).exists() );
166         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
167             + ".md5" ).exists() );
168     }
169
170     private void verifyVersionMetadataChecksums( String repoLocation, String version )
171         throws IOException
172     {
173         ChecksummedFile checksum =
174             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
175                 + MetadataTools.MAVEN_METADATA ) );
176         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
177         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
178
179         String contents =
180             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
181                 + MetadataTools.MAVEN_METADATA + ".sha1" ) );
182         assertTrue( StringUtils.contains( contents, sha1 ) );
183
184         contents =
185             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
186                 + MetadataTools.MAVEN_METADATA + ".md5" ) );
187         assertTrue( StringUtils.contains( contents, md5 ) );
188     }
189
190     private void verifyProjectMetadataChecksums( String repoLocation )
191         throws IOException
192     {
193         ChecksummedFile checksum =
194             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
195                 + MetadataTools.MAVEN_METADATA ) );
196         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
197         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
198
199         String contents =
200             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
201                 + MetadataTools.MAVEN_METADATA + ".sha1" ) );
202         assertTrue( StringUtils.contains( contents, sha1 ) );
203
204         contents =
205             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
206                 + MetadataTools.MAVEN_METADATA + ".md5" ) );
207         assertTrue( StringUtils.contains( contents, md5 ) );
208     }
209
210     private void verifyPomChecksums( String repoLocation, String artifact, String version )
211         throws IOException
212     {
213         ChecksummedFile checksum;
214         String sha1;
215         String md5;
216         String contents;
217         checksum =
218             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
219                 + artifact + ".pom" ) );
220         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
221         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
222
223         contents =
224             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
225                 + artifact + ".pom.sha1" ) );
226         assertTrue( StringUtils.contains( contents, sha1 ) );
227
228         contents =
229             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
230                 + artifact + ".pom.md5" ) );
231         assertTrue( StringUtils.contains( contents, md5 ) );
232     }
233
234     private void verifyArtifactChecksums( String repoLocation, String artifact, String version )
235         throws IOException
236     {
237         ChecksummedFile checksum =
238             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
239                 + artifact + ".jar" ) );
240         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
241         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
242
243         String contents =
244             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
245                 + artifact + ".jar.sha1" ) );
246         assertTrue( StringUtils.contains( contents, sha1 ) );
247
248         contents =
249             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
250                 + artifact + ".jar.md5" ) );
251         assertTrue( StringUtils.contains( contents, md5 ) );
252     }
253
254     private String getTimestamp( String[] artifactsList, int startIndex, int index )
255     {
256         int endIndex = -1;
257         String timestamp;
258
259         if ( artifactsList[index].contains( "jar" ) )
260         {
261             endIndex = artifactsList[index].indexOf( ".jar" );
262         }
263         else
264         {
265             endIndex = artifactsList[index].indexOf( ".pom" );
266         }
267
268         timestamp = artifactsList[index].substring( startIndex, endIndex );
269
270         return timestamp;
271     }
272
273     public void testArtifactUploadWithPomSuccessful()
274         throws Exception
275     {
276         setUploadParameters( "1.0", null,
277                              new File( getBasedir(),
278                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
279                              new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
280
281         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
282         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
283
284         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
285         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
286         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
287
288         archivaConfigControl.replay();
289         repoFactoryControl.replay();
290         auditLogsDaoControl.replay();
291
292         String returnString = uploadAction.doUpload();
293         assertEquals( Action.SUCCESS, returnString );
294
295         archivaConfigControl.verify();
296         repoFactoryControl.verify();
297         auditLogsDaoControl.verify();
298
299         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
300         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
301
302         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
303         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
304         verifyProjectMetadataChecksums( repoLocation );
305     }
306
307     public void testArtifactUploadWithClassifier()
308         throws Exception
309     {
310         setUploadParameters( "1.0", "tests",
311                              new File( getBasedir(),
312                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
313                              false );
314
315         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
316         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
317
318         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
319         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
320         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
321
322         archivaConfigControl.replay();
323         repoFactoryControl.replay();
324         auditLogsDaoControl.replay();
325
326         String returnString = uploadAction.doUpload();
327         assertEquals( Action.SUCCESS, returnString );
328
329         archivaConfigControl.verify();
330         repoFactoryControl.verify();
331         auditLogsDaoControl.verify();
332
333         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
334         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
335         assertTrue( new File( repoLocation,
336                               "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
337         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
338
339         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
340         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
341         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
342
343         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
344         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
345             + ".sha1" ).exists() );
346         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
347             + ".md5" ).exists() );
348
349         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-tests", "1.0" );
350         verifyProjectMetadataChecksums( repoLocation );
351     }
352
353     public void testArtifactUploadGeneratePomSuccessful()
354         throws Exception
355     {
356         setUploadParameters( "1.0", null,
357                              new File( getBasedir(),
358                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
359                              true );
360
361         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
362         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
363
364         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
365         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
366         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
367
368         archivaConfigControl.replay();
369         repoFactoryControl.replay();
370         auditLogsDaoControl.replay();
371
372         String returnString = uploadAction.doUpload();
373         assertEquals( Action.SUCCESS, returnString );
374
375         archivaConfigControl.verify();
376         repoFactoryControl.verify();
377         auditLogsDaoControl.verify();
378
379         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
380         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
381
382         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
383         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
384         verifyProjectMetadataChecksums( repoLocation );
385     }
386
387     public void testArtifactUploadFailedRepositoryNotFound()
388         throws Exception
389     {
390         setUploadParameters( "1.0", null,
391                              new File( getBasedir(),
392                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
393                              false );
394
395         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
396         repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
397                                            new RepositoryNotFoundException() );
398
399         archivaConfigControl.replay();
400         repoFactoryControl.replay();
401
402         String returnString = uploadAction.doUpload();
403         assertEquals( Action.ERROR, returnString );
404
405         archivaConfigControl.verify();
406         repoFactoryControl.verify();
407
408         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
409         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
410
411         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
412
413         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
414     }
415
416     public void testArtifactUploadSnapshots()
417         throws Exception
418     {
419         setUploadParameters( "1.0-SNAPSHOT", null,
420                              new File( getBasedir(),
421                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
422                              true );
423
424         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
425         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
426
427         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
428         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
429         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
430
431         archivaConfigControl.replay();
432         repoFactoryControl.replay();
433         auditLogsDaoControl.replay();
434
435         String returnString = uploadAction.doUpload();
436         assertEquals( Action.SUCCESS, returnString );
437
438         archivaConfigControl.verify();
439         repoFactoryControl.verify();
440         auditLogsDaoControl.verify();
441
442         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
443         String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
444
445         assertEquals( 9, artifactsList.length );
446         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
447             + MetadataTools.MAVEN_METADATA ).exists() );
448         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
449             + MetadataTools.MAVEN_METADATA + ".sha1" ).exists() );
450         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
451             + MetadataTools.MAVEN_METADATA + ".md5" ).exists() );
452
453         int startIndex = "artifact-upload-1.0-".length();
454         String timestamp = getTimestamp( artifactsList, startIndex, 0 );
455
456         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestamp,
457                                                                "1.0-SNAPSHOT" );
458         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestamp, "1.0-SNAPSHOT" );
459         verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestamp, "1.0-SNAPSHOT" );
460         verifyProjectMetadataChecksums( repoLocation );
461         verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
462
463         // verify build number
464         File metadataFile =
465             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA );
466         ArchivaRepositoryMetadata artifactMetadata = RepositoryMetadataReader.read( metadataFile );
467
468         SnapshotVersion snapshotVersion = artifactMetadata.getSnapshotVersion();
469         assertEquals( "Incorrect build number set in artifact metadata.", 1, snapshotVersion.getBuildNumber() );
470
471         String timestampPart = StringUtils.substringBeforeLast( timestamp, "-" );
472         assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
473
474         String buildnumber = StringUtils.substringAfterLast( timestamp, "-" );
475         assertEquals( "Incorrect build number in filename.", "1", buildnumber );
476
477         archivaConfigControl.reset();
478         repoFactoryControl.reset();
479         auditLogsDaoControl.reset();
480
481         auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
482
483         // MRM-1353
484         // upload snapshot artifact again and check if build number was incremented
485         setUploadParameters( "1.0-SNAPSHOT", null,
486                              new File( getBasedir(),
487                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
488                              true );
489
490         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
491         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
492         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
493
494         archivaConfigControl.replay();
495         repoFactoryControl.replay();
496         auditLogsDaoControl.replay();
497
498         returnString = uploadAction.doUpload();
499         assertEquals( Action.SUCCESS, returnString );
500
501         archivaConfigControl.verify();
502         repoFactoryControl.verify();
503         auditLogsDaoControl.verify();
504
505         artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
506
507         assertEquals( 15, artifactsList.length );
508
509         timestamp = getTimestamp( artifactsList, startIndex, 6 );
510
511         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestamp,
512                                                                "1.0-SNAPSHOT" );
513         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestamp, "1.0-SNAPSHOT" );
514         verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestamp, "1.0-SNAPSHOT" );
515         verifyProjectMetadataChecksums( repoLocation );
516         verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
517
518         // verify build number set in metadata and in filename
519         metadataFile =
520             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA );
521         artifactMetadata = RepositoryMetadataReader.read( metadataFile );
522
523         snapshotVersion = artifactMetadata.getSnapshotVersion();
524         assertEquals( "Incorrect build number set in artifact metadata.", 2, snapshotVersion.getBuildNumber() );
525
526         timestampPart = StringUtils.substringBeforeLast( timestamp, "-" );
527         assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
528
529         buildnumber = StringUtils.substringAfterLast( timestamp, "-" );
530         assertEquals( "Incorrect build number in filename.", "2", buildnumber );
531     }
532
533     public void testChecksumIsCorrectWhenArtifactIsReUploaded()
534         throws Exception
535     {
536         setUploadParameters( "1.0", null,
537                              new File( getBasedir(),
538                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
539                              true );
540
541         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
542         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
543         repoConfig.setBlockRedeployments( false );
544         content.setRepository( repoConfig );
545
546         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
547         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
548         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
549
550         archivaConfigControl.replay();
551         repoFactoryControl.replay();
552         auditLogsDaoControl.replay();
553
554         String returnString = uploadAction.doUpload();
555         assertEquals( Action.SUCCESS, returnString );
556
557         archivaConfigControl.verify();
558         repoFactoryControl.verify();
559         auditLogsDaoControl.verify();
560
561         archivaConfigControl.reset();
562         repoFactoryControl.reset();
563         auditLogsDaoControl.reset();
564         auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
565
566         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
567         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
568
569         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
570         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
571         verifyProjectMetadataChecksums( repoLocation );
572
573         // RE-upload artifact
574         setUploadParameters( "1.0", null,
575                              new File( getBasedir(),
576                                        "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
577                              null, true );
578
579         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
580         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
581         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
582
583         archivaConfigControl.replay();
584         repoFactoryControl.replay();
585         auditLogsDaoControl.replay();
586
587         returnString = uploadAction.doUpload();
588         assertEquals( Action.SUCCESS, returnString );
589
590         archivaConfigControl.verify();
591         repoFactoryControl.verify();
592         auditLogsDaoControl.verify();
593
594         repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
595         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
596
597         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
598         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
599         verifyProjectMetadataChecksums( repoLocation );
600     }
601
602     public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
603         throws Exception
604     {
605         setUploadParameters( "1.0", null,
606                              new File( getBasedir(),
607                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
608                              true );
609
610         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
611         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
612
613         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
614         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
615         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
616
617         archivaConfigControl.replay();
618         repoFactoryControl.replay();
619         auditLogsDaoControl.replay();
620
621         String returnString = uploadAction.doUpload();
622         assertEquals( Action.SUCCESS, returnString );
623
624         setUploadParameters( "1.0", null,
625                              new File( getBasedir(),
626                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
627                              true );
628
629         returnString = uploadAction.doUpload();
630         assertEquals( Action.ERROR, returnString );
631
632         archivaConfigControl.verify();
633         repoFactoryControl.verify();
634         auditLogsDaoControl.verify();
635
636         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
637         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
638
639         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
640         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
641         verifyProjectMetadataChecksums( repoLocation );
642     }
643
644     public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
645         throws Exception
646     {
647         setUploadParameters( "1.0", null,
648                              new File( getBasedir(),
649                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
650                              true );
651
652         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
653         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
654         repoConfig.setBlockRedeployments( false );
655         content.setRepository( repoConfig );
656
657         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
658         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
659         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null, 2 );
660
661         archivaConfigControl.replay();
662         repoFactoryControl.replay();
663         auditLogsDaoControl.replay();
664
665         String returnString = uploadAction.doUpload();
666         assertEquals( Action.SUCCESS, returnString );
667
668         setUploadParameters( "1.0", null,
669                              new File( getBasedir(),
670                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
671                              true );
672
673         returnString = uploadAction.doUpload();
674         assertEquals( Action.SUCCESS, returnString );
675
676         archivaConfigControl.verify();
677         repoFactoryControl.verify();
678         auditLogsDaoControl.verify();
679
680         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
681         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
682
683         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
684         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
685         verifyProjectMetadataChecksums( repoLocation );
686     }
687 }