1 package org.apache.archiva.web.action;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
60 public class UploadActionTest
61 extends AbstractActionTestCase
63 private UploadAction uploadAction;
65 private RepositoryContentFactory repoFactory;
67 private MockControl repoFactoryControl;
69 private MockControl managedRepoAdminControl;
71 private ManagedRepositoryAdmin managedRepositoryAdmin;
73 private MockControl archivaAdminControl;
75 private ArchivaAdministration archivaAdministration;
77 private static final String REPOSITORY_ID = "test-repo";
80 private ManagedRepository managedRepository;
88 MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
89 ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
91 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
92 repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
94 managedRepoAdminControl = MockControl.createControl( ManagedRepositoryAdmin.class );
95 managedRepositoryAdmin = (ManagedRepositoryAdmin) managedRepoAdminControl.getMock();
97 archivaAdminControl = MockControl.createControl( ArchivaAdministration.class );
98 archivaAdministration = (ArchivaAdministration) archivaAdminControl.getMock();
100 uploadAction = new UploadAction();
101 uploadAction.setScheduler( scheduler );
102 uploadAction.setManagedRepositoryAdmin( managedRepositoryAdmin );
103 uploadAction.setArchivaAdministration( archivaAdministration );
105 uploadAction.setRepositoryFactory( repoFactory );
107 File testRepo = new File( FileUtil.getBasedir(), "target/test-classes/test-repo" );
110 assertTrue( testRepo.exists() );
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 );
122 public void tearDown()
125 File testRepo = new File( this.managedRepository.getLocation() );
126 FileUtils.deleteDirectory( testRepo );
128 assertFalse( testRepo.exists() );
133 private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
134 boolean generatePom )
136 uploadAction.setRepositoryId( REPOSITORY_ID );
137 uploadAction.setGroupId( "org.apache.archiva" );
138 uploadAction.setArtifactId( "artifact-upload" );
139 uploadAction.setVersion( version );
140 uploadAction.setPackaging( "jar" );
142 uploadAction.setClassifier( classifier );
143 uploadAction.setArtifact( artifact );
144 uploadAction.setPom( pomFile );
145 uploadAction.setGeneratePom( generatePom );
148 private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation, String artifact,
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() );
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() );
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() );
173 private void verifyVersionMetadataChecksums( String repoLocation, String version )
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 );
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 ) );
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 ) );
193 private void verifyProjectMetadataChecksums( String repoLocation )
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 );
201 String contents = FileUtils.readFileToString(
202 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
203 assertTrue( StringUtils.contains( contents, sha1 ) );
205 contents = FileUtils.readFileToString(
206 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
207 assertTrue( StringUtils.contains( contents, md5 ) );
210 private void verifyPomChecksums( String repoLocation, String artifact, String version )
213 ChecksummedFile checksum;
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 );
222 contents = FileUtils.readFileToString(
223 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.sha1" ) );
224 assertTrue( StringUtils.contains( contents, sha1 ) );
226 contents = FileUtils.readFileToString(
227 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.md5" ) );
228 assertTrue( StringUtils.contains( contents, md5 ) );
231 private void verifyArtifactChecksums( String repoLocation, String artifact, String version )
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 );
239 String contents = FileUtils.readFileToString(
240 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.sha1" ) );
241 assertTrue( StringUtils.contains( contents, sha1 ) );
243 contents = FileUtils.readFileToString(
244 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.md5" ) );
245 assertTrue( StringUtils.contains( contents, md5 ) );
248 private String getTimestamp( String[] artifactsList, int startIndex, int index )
253 if ( artifactsList[index].contains( "jar" ) )
255 endIndex = artifactsList[index].indexOf( ".jar" );
259 endIndex = artifactsList[index].indexOf( ".pom" );
262 timestamp = artifactsList[index].substring( startIndex, endIndex );
267 private MockControl mockAuditLogs( List<String> resources )
269 return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
272 private MockControl mockAuditLogs( String action, List<String> resources )
274 MockControl control = MockControl.createControl( AuditListener.class );
275 AuditListener listener = (AuditListener) control.getMock();
276 boolean matcherSet = false;
277 for ( String resource : resources )
279 listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
282 control.setMatcher( new AuditEventArgumentsMatcher() );
288 uploadAction.setAuditListeners( Collections.singletonList( listener ) );
293 public void testArtifactUploadWithPomSuccessful()
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" ),
301 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
302 content.setRepository( getManagedRepository() );
304 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
306 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
307 getManagedRepository(), 1, 8 );
309 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
312 managedRepoAdminControl.replay();
313 archivaAdminControl.replay();
314 repoFactoryControl.replay();
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" ) );
320 String returnString = uploadAction.doUpload();
321 assertEquals( Action.SUCCESS, returnString );
323 repoFactoryControl.verify();
326 String repoLocation = getManagedRepository().getLocation();
327 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
329 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
330 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
331 verifyProjectMetadataChecksums( repoLocation );
335 public void testArtifactUploadWithClassifier()
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" ),
343 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
344 content.setRepository( getManagedRepository() );
346 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
348 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
349 getManagedRepository(), 1, 8 );
351 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
354 managedRepoAdminControl.replay();
355 archivaAdminControl.replay();
357 repoFactoryControl.replay();
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" ) );
363 String returnString = uploadAction.doUpload();
364 assertEquals( Action.SUCCESS, returnString );
366 repoFactoryControl.verify();
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() );
378 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
380 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
382 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
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() );
391 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-tests", "1.0" );
392 verifyProjectMetadataChecksums( repoLocation );
396 public void testArtifactUploadGeneratePomSuccessful()
399 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
400 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
403 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
404 content.setRepository( getManagedRepository() );
406 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
408 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
409 getManagedRepository(), 1, 8 );
411 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
414 managedRepoAdminControl.replay();
415 archivaAdminControl.replay();
417 repoFactoryControl.replay();
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" ) );
423 String returnString = uploadAction.doUpload();
424 assertEquals( Action.SUCCESS, returnString );
426 repoFactoryControl.verify();
429 String repoLocation = getManagedRepository().getLocation();
430 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
432 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
433 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
434 verifyProjectMetadataChecksums( repoLocation );
438 public void testArtifactUploadNoPomSuccessful()
441 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
442 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
445 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
446 content.setRepository( getManagedRepository() );
448 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
450 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
451 getManagedRepository(), 1, 8 );
453 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
456 managedRepoAdminControl.replay();
457 archivaAdminControl.replay();
459 repoFactoryControl.replay();
461 MockControl control =
462 mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
464 String returnString = uploadAction.doUpload();
465 assertEquals( Action.SUCCESS, returnString );
467 repoFactoryControl.verify();
470 String repoLocation = getManagedRepository().getLocation();
472 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
474 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
476 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
479 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
481 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
483 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
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() );
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 );
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 ) );
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 ) );
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 );
512 contents = FileUtils.readFileToString(
513 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
514 assertTrue( StringUtils.contains( contents, sha1 ) );
516 contents = FileUtils.readFileToString(
517 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
518 assertTrue( StringUtils.contains( contents, md5 ) );
522 public void testArtifactUploadFailedRepositoryNotFound()
525 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
526 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
529 repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
530 new RepositoryNotFoundException() );
532 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
533 getManagedRepository(), 1, 8 );
535 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
538 managedRepoAdminControl.replay();
539 archivaAdminControl.replay();
541 repoFactoryControl.replay();
543 String returnString = uploadAction.doUpload();
544 assertEquals( Action.ERROR, returnString );
546 repoFactoryControl.verify();
548 String repoLocation = getManagedRepository().getLocation();
550 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
553 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
556 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
560 public void testArtifactUploadSnapshots()
563 setUploadParameters( "1.0-SNAPSHOT", null, new File( FileUtil.getBasedir(),
564 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
567 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
568 content.setRepository( getManagedRepository() );
570 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
572 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
573 getManagedRepository(), 1, 8 );
575 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
578 managedRepoAdminControl.replay();
579 archivaAdminControl.replay();
581 repoFactoryControl.replay();
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" ) );
590 String returnString = uploadAction.doUpload();
591 assertEquals( Action.SUCCESS, returnString );
593 repoFactoryControl.verify();
596 String repoLocation = getManagedRepository().getLocation();
597 String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
598 Arrays.sort( artifactsList );
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() );
610 int startIndex = "artifact-upload-1.0-".length();
611 String timestampPath = getTimestamp( artifactsList, startIndex, 0 );
613 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
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" );
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 );
625 SnapshotVersion snapshotVersion = artifactMetadata.getSnapshotVersion();
626 assertEquals( "Incorrect build number set in artifact metadata.", 1, snapshotVersion.getBuildNumber() );
628 String timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
629 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
631 String buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
632 assertEquals( "Incorrect build number in filename.", "1", buildnumber );
634 repoFactoryControl.reset();
637 control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
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" ),
645 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
647 repoFactoryControl.replay();
649 fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
650 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
651 timestamp = fmt.format( new Date() );
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" ) );
657 returnString = uploadAction.doUpload();
658 assertEquals( Action.SUCCESS, returnString );
660 repoFactoryControl.verify();
663 artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
664 Arrays.sort( artifactsList );
666 assertEquals( 15, artifactsList.length );
668 timestampPath = getTimestamp( artifactsList, startIndex, 6 );
670 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
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" );
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 );
682 snapshotVersion = artifactMetadata.getSnapshotVersion();
683 assertEquals( "Incorrect build number set in artifact metadata.", 2, snapshotVersion.getBuildNumber() );
685 timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
686 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
688 buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
689 assertEquals( "Incorrect build number in filename.", "2", buildnumber );
693 public void testChecksumIsCorrectWhenArtifactIsReUploaded()
696 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
697 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
700 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
701 ManagedRepository repoConfig = getManagedRepository();
702 repoConfig.setBlockRedeployments( false );
703 content.setRepository( repoConfig );
705 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
707 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
710 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
713 managedRepoAdminControl.replay();
714 archivaAdminControl.replay();
716 repoFactoryControl.replay();
718 String returnString = uploadAction.doUpload();
719 assertEquals( Action.SUCCESS, returnString );
721 repoFactoryControl.verify();
723 repoFactoryControl.reset();
725 String repoLocation = getManagedRepository().getLocation();
726 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
728 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
729 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
730 verifyProjectMetadataChecksums( repoLocation );
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" ),
737 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
739 repoFactoryControl.replay();
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" ) );
747 returnString = uploadAction.doUpload();
748 assertEquals( Action.SUCCESS, returnString );
750 repoFactoryControl.verify();
753 repoLocation = getManagedRepository().getLocation();
754 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
756 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
757 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
758 verifyProjectMetadataChecksums( repoLocation );
762 public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
765 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
766 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
769 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
770 content.setRepository( getManagedRepository() );
772 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
774 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
775 getManagedRepository(), 1, 8 );
777 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
780 managedRepoAdminControl.replay();
781 archivaAdminControl.replay();
783 repoFactoryControl.replay();
785 String returnString = uploadAction.doUpload();
786 assertEquals( Action.SUCCESS, returnString );
788 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
789 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
792 MockControl control = mockAuditLogs( Collections.<String>emptyList() );
794 returnString = uploadAction.doUpload();
795 assertEquals( Action.ERROR, returnString );
797 repoFactoryControl.verify();
800 String repoLocation = getManagedRepository().getLocation();
801 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
803 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
804 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
805 verifyProjectMetadataChecksums( repoLocation );
809 public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
812 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
813 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
816 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
817 ManagedRepository repoConfig = getManagedRepository();
818 repoConfig.setBlockRedeployments( false );
819 content.setRepository( repoConfig );
821 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
823 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
826 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
829 managedRepoAdminControl.replay();
830 archivaAdminControl.replay();
832 repoFactoryControl.replay();
834 String returnString = uploadAction.doUpload();
835 assertEquals( Action.SUCCESS, returnString );
837 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
838 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
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" ) );
847 returnString = uploadAction.doUpload();
848 assertEquals( Action.SUCCESS, returnString );
850 repoFactoryControl.verify();
853 String repoLocation = getManagedRepository().getLocation();
854 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
856 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
857 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
858 verifyProjectMetadataChecksums( repoLocation );
861 ManagedRepository getManagedRepository()
863 return new BeanReplicator().replicateBean( this.managedRepository, ManagedRepository.class );