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