]> source.dussan.org Git - archiva.git/blob
cdb8788ba7095a10b443bc471507fd2389a2a0c3
[archiva.git] /
1 package org.apache.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 com.opensymphony.xwork2.Action;
23 import net.sf.beanlib.provider.replicator.BeanReplicator;
24 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
25 import org.apache.archiva.admin.model.beans.ManagedRepository;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.archiva.audit.AuditListener;
29 import org.apache.archiva.checksum.ChecksumAlgorithm;
30 import org.apache.archiva.checksum.ChecksummedFile;
31 import org.apache.archiva.common.utils.FileUtil;
32 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
33 import org.apache.archiva.model.ArchivaRepositoryMetadata;
34 import org.apache.archiva.model.SnapshotVersion;
35 import org.apache.archiva.repository.ManagedRepositoryContent;
36 import org.apache.archiva.repository.RepositoryContentFactory;
37 import org.apache.archiva.repository.RepositoryNotFoundException;
38 import org.apache.archiva.repository.content.ManagedDefaultRepositoryContent;
39 import org.apache.archiva.repository.metadata.MetadataTools;
40 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.easymock.MockControl;
44 import org.easymock.classextension.MockClassControl;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.text.SimpleDateFormat;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.Date;
53 import java.util.List;
54 import java.util.TimeZone;
55 import org.junit.Test;
56
57 /**
58  * UploadActionTest
59  */
60 public class UploadActionTest
61     extends AbstractActionTestCase
62 {
63     private UploadAction uploadAction;
64
65     private RepositoryContentFactory repoFactory;
66
67     private MockControl repoFactoryControl;
68
69     private MockControl managedRepoAdminControl;
70
71     private ManagedRepositoryAdmin managedRepositoryAdmin;
72
73     private MockControl archivaAdminControl;
74
75     private ArchivaAdministration archivaAdministration;
76
77     private static final String REPOSITORY_ID = "test-repo";
78
79
80     private ManagedRepository managedRepository;
81
82     @Override
83     public void setUp()
84         throws Exception
85     {
86         super.setUp();
87
88         MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
89         ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
90
91         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
92         repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
93
94         managedRepoAdminControl = MockControl.createControl( ManagedRepositoryAdmin.class );
95         managedRepositoryAdmin = (ManagedRepositoryAdmin) managedRepoAdminControl.getMock();
96
97         archivaAdminControl = MockControl.createControl( ArchivaAdministration.class );
98         archivaAdministration = (ArchivaAdministration) archivaAdminControl.getMock();
99
100         uploadAction = new UploadAction();
101         uploadAction.setScheduler( scheduler );
102         uploadAction.setManagedRepositoryAdmin( managedRepositoryAdmin );
103         uploadAction.setArchivaAdministration( archivaAdministration );
104
105         uploadAction.setRepositoryFactory( repoFactory );
106
107         File testRepo = new File( FileUtil.getBasedir(), "target/test-classes/test-repo" );
108         testRepo.mkdirs();
109
110         assertTrue( testRepo.exists() );
111
112         managedRepository = new ManagedRepository();
113         managedRepository.setId( REPOSITORY_ID );
114         managedRepository.setLayout( "default" );
115         managedRepository.setLocation( testRepo.getPath() );
116         managedRepository.setName( REPOSITORY_ID );
117         managedRepository.setBlockRedeployments( true );
118
119     }
120
121     @Override
122     public void tearDown()
123         throws Exception
124     {
125         File testRepo = new File( this.managedRepository.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,
152                               "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar" ).exists() );
153         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
154             + ".jar.sha1" ).exists() );
155         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
156             + ".jar.md5" ).exists() );
157
158         assertTrue( new File( repoLocation,
159                               "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom" ).exists() );
160         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
161             + ".pom.sha1" ).exists() );
162         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
163             + ".pom.md5" ).exists() );
164
165         assertTrue(
166             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
167         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
168             + ".sha1" ).exists() );
169         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
170             + ".md5" ).exists() );
171     }
172
173     private void verifyVersionMetadataChecksums( String repoLocation, String version )
174         throws IOException
175     {
176         ChecksummedFile checksum = new ChecksummedFile( new File( repoLocation,
177                                                                   "/org/apache/archiva/artifact-upload/" + version + "/"
178                                                                       + MetadataTools.MAVEN_METADATA ) );
179         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
180         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
181
182         String contents = FileUtils.readFileToString( new File( repoLocation,
183                                                                 "/org/apache/archiva/artifact-upload/" + version + "/"
184                                                                     + MetadataTools.MAVEN_METADATA + ".sha1" ) );
185         assertTrue( StringUtils.contains( contents, sha1 ) );
186
187         contents = FileUtils.readFileToString( new File( repoLocation,
188                                                          "/org/apache/archiva/artifact-upload/" + version + "/"
189                                                              + MetadataTools.MAVEN_METADATA + ".md5" ) );
190         assertTrue( StringUtils.contains( contents, md5 ) );
191     }
192
193     private void verifyProjectMetadataChecksums( String repoLocation )
194         throws IOException
195     {
196         ChecksummedFile checksum = new ChecksummedFile(
197             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
198         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
199         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
200
201         String contents = FileUtils.readFileToString(
202             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
203         assertTrue( StringUtils.contains( contents, sha1 ) );
204
205         contents = FileUtils.readFileToString(
206             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + 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 = new ChecksummedFile(
218             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom" ) );
219         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
220         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
221
222         contents = FileUtils.readFileToString(
223             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.sha1" ) );
224         assertTrue( StringUtils.contains( contents, sha1 ) );
225
226         contents = FileUtils.readFileToString(
227             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.md5" ) );
228         assertTrue( StringUtils.contains( contents, md5 ) );
229     }
230
231     private void verifyArtifactChecksums( String repoLocation, String artifact, String version )
232         throws IOException
233     {
234         ChecksummedFile checksum = new ChecksummedFile(
235             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar" ) );
236         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
237         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
238
239         String contents = FileUtils.readFileToString(
240             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.sha1" ) );
241         assertTrue( StringUtils.contains( contents, sha1 ) );
242
243         contents = FileUtils.readFileToString(
244             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.md5" ) );
245         assertTrue( StringUtils.contains( contents, md5 ) );
246     }
247
248     private String getTimestamp( String[] artifactsList, int startIndex, int index )
249     {
250         int endIndex = -1;
251         String timestamp;
252
253         if ( artifactsList[index].contains( "jar" ) )
254         {
255             endIndex = artifactsList[index].indexOf( ".jar" );
256         }
257         else
258         {
259             endIndex = artifactsList[index].indexOf( ".pom" );
260         }
261
262         timestamp = artifactsList[index].substring( startIndex, endIndex );
263
264         return timestamp;
265     }
266
267     private MockControl mockAuditLogs( List<String> resources )
268     {
269         return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
270     }
271
272     private MockControl mockAuditLogs( String action, List<String> resources )
273     {
274         MockControl control = MockControl.createControl( AuditListener.class );
275         AuditListener listener = (AuditListener) control.getMock();
276         boolean matcherSet = false;
277         for ( String resource : resources )
278         {
279             listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
280             if ( !matcherSet )
281             {
282                 control.setMatcher( new AuditEventArgumentsMatcher() );
283                 matcherSet = true;
284             }
285         }
286         control.replay();
287
288         uploadAction.setAuditListeners( Collections.singletonList( listener ) );
289         return control;
290     }
291
292     @Test
293     public void testArtifactUploadWithPomSuccessful()
294         throws Exception
295     {
296         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
297                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
298                              new File( FileUtil.getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ),
299                              false );
300
301         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
302         content.setRepository( getManagedRepository() );
303
304         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
305
306         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
307                                                  getManagedRepository(), 1, 8 );
308
309         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
310                                              2 );
311
312         managedRepoAdminControl.replay();
313         archivaAdminControl.replay();
314         repoFactoryControl.replay();
315
316         MockControl control = mockAuditLogs(
317             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
318                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
319
320         String returnString = uploadAction.doUpload();
321         assertEquals( Action.SUCCESS, returnString );
322
323         repoFactoryControl.verify();
324         control.verify();
325
326         String repoLocation = getManagedRepository().getLocation();
327         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
328
329         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
330         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
331         verifyProjectMetadataChecksums( repoLocation );
332     }
333
334     @Test
335     public void testArtifactUploadWithClassifier()
336         throws Exception
337     {
338         setUploadParameters( "1.0", "tests", new File( FileUtil.getBasedir(),
339                                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
340                              new File( FileUtil.getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ),
341                              false );
342
343         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
344         content.setRepository( getManagedRepository() );
345
346         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
347
348         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
349                                                  getManagedRepository(), 1, 8 );
350
351         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
352                                              2 );
353
354         managedRepoAdminControl.replay();
355         archivaAdminControl.replay();
356
357         repoFactoryControl.replay();
358
359         MockControl control = mockAuditLogs(
360             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar",
361                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
362
363         String returnString = uploadAction.doUpload();
364         assertEquals( Action.SUCCESS, returnString );
365
366         repoFactoryControl.verify();
367         control.verify();
368
369         String repoLocation = getManagedRepository().getLocation();
370         assertTrue( new File( repoLocation,
371                               "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
372         assertTrue( new File( repoLocation,
373                               "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
374         assertTrue( new File( repoLocation,
375                               "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
376
377         assertTrue(
378             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
379         assertTrue(
380             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
381         assertTrue(
382             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
383
384         assertTrue(
385             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
386         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
387             + ".sha1" ).exists() );
388         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
389             + ".md5" ).exists() );
390
391         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-tests", "1.0" );
392         verifyProjectMetadataChecksums( repoLocation );
393     }
394
395     @Test
396     public void testArtifactUploadGeneratePomSuccessful()
397         throws Exception
398     {
399         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
400                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
401                              null, true );
402
403         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
404         content.setRepository( getManagedRepository() );
405
406         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
407
408         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
409                                                  getManagedRepository(), 1, 8 );
410
411         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
412                                              2 );
413
414         managedRepoAdminControl.replay();
415         archivaAdminControl.replay();
416
417         repoFactoryControl.replay();
418
419         MockControl control = mockAuditLogs(
420             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
421                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
422
423         String returnString = uploadAction.doUpload();
424         assertEquals( Action.SUCCESS, returnString );
425
426         repoFactoryControl.verify();
427         control.verify();
428
429         String repoLocation = getManagedRepository().getLocation();
430         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
431
432         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
433         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
434         verifyProjectMetadataChecksums( repoLocation );
435     }
436
437     @Test
438     public void testArtifactUploadNoPomSuccessful()
439         throws Exception
440     {
441         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
442                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
443                              null, false );
444
445         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
446         content.setRepository( getManagedRepository() );
447
448         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
449
450         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
451                                                  getManagedRepository(), 1, 8 );
452
453         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
454                                              2 );
455
456         managedRepoAdminControl.replay();
457         archivaAdminControl.replay();
458
459         repoFactoryControl.replay();
460
461         MockControl control =
462             mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
463
464         String returnString = uploadAction.doUpload();
465         assertEquals( Action.SUCCESS, returnString );
466
467         repoFactoryControl.verify();
468         control.verify();
469
470         String repoLocation = getManagedRepository().getLocation();
471         assertTrue(
472             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
473         assertTrue(
474             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
475         assertTrue(
476             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
477
478         assertFalse(
479             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
480         assertFalse(
481             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
482         assertFalse(
483             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
484
485         assertTrue(
486             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
487         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
488             + ".sha1" ).exists() );
489         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
490             + ".md5" ).exists() );
491
492         // verify checksums of jar file
493         ChecksummedFile checksum = new ChecksummedFile(
494             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
495         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
496         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
497
498         String contents = FileUtils.readFileToString(
499             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
500         assertTrue( StringUtils.contains( contents, sha1 ) );
501
502         contents = FileUtils.readFileToString(
503             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
504         assertTrue( StringUtils.contains( contents, md5 ) );
505
506         // verify checksums of metadata file
507         checksum = new ChecksummedFile(
508             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
509         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
510         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
511
512         contents = FileUtils.readFileToString(
513             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
514         assertTrue( StringUtils.contains( contents, sha1 ) );
515
516         contents = FileUtils.readFileToString(
517             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
518         assertTrue( StringUtils.contains( contents, md5 ) );
519     }
520
521     @Test
522     public void testArtifactUploadFailedRepositoryNotFound()
523         throws Exception
524     {
525         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
526                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
527                              null, false );
528
529         repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
530                                            new RepositoryNotFoundException() );
531
532         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
533                                                  getManagedRepository(), 1, 8 );
534
535         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
536                                              2 );
537
538         managedRepoAdminControl.replay();
539         archivaAdminControl.replay();
540
541         repoFactoryControl.replay();
542
543         String returnString = uploadAction.doUpload();
544         assertEquals( Action.ERROR, returnString );
545
546         repoFactoryControl.verify();
547
548         String repoLocation = getManagedRepository().getLocation();
549         assertFalse(
550             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
551
552         assertFalse(
553             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
554
555         assertFalse(
556             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
557     }
558
559     @Test
560     public void testArtifactUploadSnapshots()
561         throws Exception
562     {
563         setUploadParameters( "1.0-SNAPSHOT", null, new File( FileUtil.getBasedir(),
564                                                              "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
565                              null, true );
566
567         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
568         content.setRepository( getManagedRepository() );
569
570         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
571
572         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
573                                                  getManagedRepository(), 1, 8 );
574
575         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
576                                              2, 5 );
577
578         managedRepoAdminControl.replay();
579         archivaAdminControl.replay();
580
581         repoFactoryControl.replay();
582
583         SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
584         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
585         String timestamp = fmt.format( new Date() );
586         MockControl control = mockAuditLogs( Arrays.asList(
587             "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar",
588             "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) );
589
590         String returnString = uploadAction.doUpload();
591         assertEquals( Action.SUCCESS, returnString );
592
593         repoFactoryControl.verify();
594         control.verify();
595
596         String repoLocation = getManagedRepository().getLocation();
597         String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
598         Arrays.sort( artifactsList );
599
600         assertEquals( 9, artifactsList.length );
601         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
602             + MetadataTools.MAVEN_METADATA ).exists() );
603         assertTrue( new File( repoLocation,
604                               "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA
605                                   + ".sha1" ).exists() );
606         assertTrue( new File( repoLocation,
607                               "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA
608                                   + ".md5" ).exists() );
609
610         int startIndex = "artifact-upload-1.0-".length();
611         String timestampPath = getTimestamp( artifactsList, startIndex, 0 );
612
613         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
614                                                                "1.0-SNAPSHOT" );
615         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
616         verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
617         verifyProjectMetadataChecksums( repoLocation );
618         verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
619
620         // verify build number
621         File metadataFile = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
622             + MetadataTools.MAVEN_METADATA );
623         ArchivaRepositoryMetadata artifactMetadata = MavenMetadataReader.read( metadataFile );
624
625         SnapshotVersion snapshotVersion = artifactMetadata.getSnapshotVersion();
626         assertEquals( "Incorrect build number set in artifact metadata.", 1, snapshotVersion.getBuildNumber() );
627
628         String timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
629         assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
630
631         String buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
632         assertEquals( "Incorrect build number in filename.", "1", buildnumber );
633
634         repoFactoryControl.reset();
635         control.reset();
636
637         control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
638
639         // MRM-1353
640         // upload snapshot artifact again and check if build number was incremented
641         setUploadParameters( "1.0-SNAPSHOT", null, new File( FileUtil.getBasedir(),
642                                                              "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
643                              null, true );
644
645         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
646
647         repoFactoryControl.replay();
648
649         fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
650         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
651         timestamp = fmt.format( new Date() );
652
653         control = mockAuditLogs( Arrays.asList(
654             "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-2.jar",
655             "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-2.pom" ) );
656
657         returnString = uploadAction.doUpload();
658         assertEquals( Action.SUCCESS, returnString );
659
660         repoFactoryControl.verify();
661         control.verify();
662
663         artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
664         Arrays.sort( artifactsList );
665
666         assertEquals( 15, artifactsList.length );
667
668         timestampPath = getTimestamp( artifactsList, startIndex, 6 );
669
670         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
671                                                                "1.0-SNAPSHOT" );
672         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
673         verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
674         verifyProjectMetadataChecksums( repoLocation );
675         verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
676
677         // verify build number set in metadata and in filename
678         metadataFile = new File( repoLocation,
679                                  "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA );
680         artifactMetadata = MavenMetadataReader.read( metadataFile );
681
682         snapshotVersion = artifactMetadata.getSnapshotVersion();
683         assertEquals( "Incorrect build number set in artifact metadata.", 2, snapshotVersion.getBuildNumber() );
684
685         timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
686         assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
687
688         buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
689         assertEquals( "Incorrect build number in filename.", "2", buildnumber );
690     }
691
692     @Test
693     public void testChecksumIsCorrectWhenArtifactIsReUploaded()
694         throws Exception
695     {
696         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
697                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
698                              null, true );
699
700         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
701         ManagedRepository repoConfig = getManagedRepository();
702         repoConfig.setBlockRedeployments( false );
703         content.setRepository( repoConfig );
704
705         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
706
707         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
708                                                  repoConfig, 1, 8 );
709
710         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
711                                              2, 5 );
712
713         managedRepoAdminControl.replay();
714         archivaAdminControl.replay();
715
716         repoFactoryControl.replay();
717
718         String returnString = uploadAction.doUpload();
719         assertEquals( Action.SUCCESS, returnString );
720
721         repoFactoryControl.verify();
722
723         repoFactoryControl.reset();
724
725         String repoLocation = getManagedRepository().getLocation();
726         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
727
728         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
729         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
730         verifyProjectMetadataChecksums( repoLocation );
731
732         // RE-upload artifact
733         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
734                                                     "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
735                              null, true );
736
737         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
738
739         repoFactoryControl.replay();
740
741         // TODO: track modifications?
742 //        MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
743         MockControl control = mockAuditLogs(
744             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
745                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
746
747         returnString = uploadAction.doUpload();
748         assertEquals( Action.SUCCESS, returnString );
749
750         repoFactoryControl.verify();
751         control.verify();
752
753         repoLocation = getManagedRepository().getLocation();
754         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
755
756         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
757         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
758         verifyProjectMetadataChecksums( repoLocation );
759     }
760
761     @Test
762     public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
763         throws Exception
764     {
765         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
766                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
767                              null, true );
768
769         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
770         content.setRepository( getManagedRepository() );
771
772         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
773
774         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
775                                                  getManagedRepository(), 1, 8 );
776
777         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
778                                              2, 5 );
779
780         managedRepoAdminControl.replay();
781         archivaAdminControl.replay();
782
783         repoFactoryControl.replay();
784
785         String returnString = uploadAction.doUpload();
786         assertEquals( Action.SUCCESS, returnString );
787
788         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
789                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
790                              null, true );
791
792         MockControl control = mockAuditLogs( Collections.<String>emptyList() );
793
794         returnString = uploadAction.doUpload();
795         assertEquals( Action.ERROR, returnString );
796
797         repoFactoryControl.verify();
798         control.verify();
799
800         String repoLocation = getManagedRepository().getLocation();
801         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
802
803         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
804         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
805         verifyProjectMetadataChecksums( repoLocation );
806     }
807
808     @Test
809     public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
810         throws Exception
811     {
812         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
813                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
814                              null, true );
815
816         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
817         ManagedRepository repoConfig = getManagedRepository();
818         repoConfig.setBlockRedeployments( false );
819         content.setRepository( repoConfig );
820
821         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
822
823         managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
824                                                  repoConfig, 1, 8 );
825
826         archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
827                                              2, 5 );
828
829         managedRepoAdminControl.replay();
830         archivaAdminControl.replay();
831
832         repoFactoryControl.replay();
833
834         String returnString = uploadAction.doUpload();
835         assertEquals( Action.SUCCESS, returnString );
836
837         setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
838                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
839                              null, true );
840
841         // TODO: track modifications?
842 //        MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
843         MockControl control = mockAuditLogs(
844             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
845                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
846
847         returnString = uploadAction.doUpload();
848         assertEquals( Action.SUCCESS, returnString );
849
850         repoFactoryControl.verify();
851         control.verify();
852
853         String repoLocation = getManagedRepository().getLocation();
854         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
855
856         verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
857         verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
858         verifyProjectMetadataChecksums( repoLocation );
859     }
860
861     ManagedRepository getManagedRepository()
862     {
863         return new BeanReplicator().replicateBean( this.managedRepository, ManagedRepository.class );
864     }
865
866 }