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;
59 public class UploadActionTest
60 extends AbstractActionTestCase
62 private UploadAction uploadAction;
64 private RepositoryContentFactory repoFactory;
66 private MockControl repoFactoryControl;
68 private MockControl managedRepoAdminControl;
70 private ManagedRepositoryAdmin managedRepositoryAdmin;
72 private MockControl archivaAdminControl;
74 private ArchivaAdministration archivaAdministration;
76 private static final String REPOSITORY_ID = "test-repo";
79 private ManagedRepository managedRepository;
86 MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
87 ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
89 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
90 repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
92 managedRepoAdminControl = MockControl.createControl( ManagedRepositoryAdmin.class );
93 managedRepositoryAdmin = (ManagedRepositoryAdmin) managedRepoAdminControl.getMock();
95 archivaAdminControl = MockControl.createControl( ArchivaAdministration.class );
96 archivaAdministration = (ArchivaAdministration) archivaAdminControl.getMock();
98 uploadAction = new UploadAction();
99 uploadAction.setScheduler( scheduler );
100 uploadAction.setManagedRepositoryAdmin( managedRepositoryAdmin );
101 uploadAction.setArchivaAdministration( archivaAdministration );
103 uploadAction.setRepositoryFactory( repoFactory );
105 File testRepo = new File( FileUtil.getBasedir(), "target/test-classes/test-repo" );
108 assertTrue( testRepo.exists() );
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 );
119 public void tearDown()
122 File testRepo = new File( this.managedRepository.getLocation() );
123 FileUtils.deleteDirectory( testRepo );
125 assertFalse( testRepo.exists() );
130 private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
131 boolean generatePom )
133 uploadAction.setRepositoryId( REPOSITORY_ID );
134 uploadAction.setGroupId( "org.apache.archiva" );
135 uploadAction.setArtifactId( "artifact-upload" );
136 uploadAction.setVersion( version );
137 uploadAction.setPackaging( "jar" );
139 uploadAction.setClassifier( classifier );
140 uploadAction.setArtifact( artifact );
141 uploadAction.setPom( pomFile );
142 uploadAction.setGeneratePom( generatePom );
145 private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation, String artifact,
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() );
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() );
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() );
170 private void verifyVersionMetadataChecksums( String repoLocation, String version )
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 );
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 ) );
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 ) );
190 private void verifyProjectMetadataChecksums( String repoLocation )
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 );
198 String contents = FileUtils.readFileToString(
199 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
200 assertTrue( StringUtils.contains( contents, sha1 ) );
202 contents = FileUtils.readFileToString(
203 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
204 assertTrue( StringUtils.contains( contents, md5 ) );
207 private void verifyPomChecksums( String repoLocation, String artifact, String version )
210 ChecksummedFile checksum;
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 );
219 contents = FileUtils.readFileToString(
220 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.sha1" ) );
221 assertTrue( StringUtils.contains( contents, sha1 ) );
223 contents = FileUtils.readFileToString(
224 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom.md5" ) );
225 assertTrue( StringUtils.contains( contents, md5 ) );
228 private void verifyArtifactChecksums( String repoLocation, String artifact, String version )
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 );
236 String contents = FileUtils.readFileToString(
237 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.sha1" ) );
238 assertTrue( StringUtils.contains( contents, sha1 ) );
240 contents = FileUtils.readFileToString(
241 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar.md5" ) );
242 assertTrue( StringUtils.contains( contents, md5 ) );
245 private String getTimestamp( String[] artifactsList, int startIndex, int index )
250 if ( artifactsList[index].contains( "jar" ) )
252 endIndex = artifactsList[index].indexOf( ".jar" );
256 endIndex = artifactsList[index].indexOf( ".pom" );
259 timestamp = artifactsList[index].substring( startIndex, endIndex );
264 private MockControl mockAuditLogs( List<String> resources )
266 return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
269 private MockControl mockAuditLogs( String action, List<String> resources )
271 MockControl control = MockControl.createControl( AuditListener.class );
272 AuditListener listener = (AuditListener) control.getMock();
273 boolean matcherSet = false;
274 for ( String resource : resources )
276 listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
279 control.setMatcher( new AuditEventArgumentsMatcher() );
285 uploadAction.setAuditListeners( Collections.singletonList( listener ) );
289 public void testArtifactUploadWithPomSuccessful()
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" ),
297 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
298 content.setRepository( getManagedRepository() );
300 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
302 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
303 getManagedRepository(), 1, 8 );
305 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
308 managedRepoAdminControl.replay();
309 archivaAdminControl.replay();
310 repoFactoryControl.replay();
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" ) );
316 String returnString = uploadAction.doUpload();
317 assertEquals( Action.SUCCESS, returnString );
319 repoFactoryControl.verify();
322 String repoLocation = getManagedRepository().getLocation();
323 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
325 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
326 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
327 verifyProjectMetadataChecksums( repoLocation );
330 public void testArtifactUploadWithClassifier()
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" ),
338 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
339 content.setRepository( getManagedRepository() );
341 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
343 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
344 getManagedRepository(), 1, 8 );
346 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
349 managedRepoAdminControl.replay();
350 archivaAdminControl.replay();
352 repoFactoryControl.replay();
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" ) );
358 String returnString = uploadAction.doUpload();
359 assertEquals( Action.SUCCESS, returnString );
361 repoFactoryControl.verify();
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() );
373 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
375 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
377 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
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() );
386 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-tests", "1.0" );
387 verifyProjectMetadataChecksums( repoLocation );
390 public void testArtifactUploadGeneratePomSuccessful()
393 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
394 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
397 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
398 content.setRepository( getManagedRepository() );
400 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
402 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
403 getManagedRepository(), 1, 8 );
405 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
408 managedRepoAdminControl.replay();
409 archivaAdminControl.replay();
411 repoFactoryControl.replay();
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" ) );
417 String returnString = uploadAction.doUpload();
418 assertEquals( Action.SUCCESS, returnString );
420 repoFactoryControl.verify();
423 String repoLocation = getManagedRepository().getLocation();
424 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
426 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
427 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
428 verifyProjectMetadataChecksums( repoLocation );
431 public void testArtifactUploadNoPomSuccessful()
434 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
435 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
438 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
439 content.setRepository( getManagedRepository() );
441 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
443 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
444 getManagedRepository(), 1, 8 );
446 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
449 managedRepoAdminControl.replay();
450 archivaAdminControl.replay();
452 repoFactoryControl.replay();
454 MockControl control =
455 mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
457 String returnString = uploadAction.doUpload();
458 assertEquals( Action.SUCCESS, returnString );
460 repoFactoryControl.verify();
463 String repoLocation = getManagedRepository().getLocation();
465 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
467 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
469 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
472 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
474 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
476 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
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() );
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 );
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 ) );
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 ) );
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 );
505 contents = FileUtils.readFileToString(
506 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
507 assertTrue( StringUtils.contains( contents, sha1 ) );
509 contents = FileUtils.readFileToString(
510 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
511 assertTrue( StringUtils.contains( contents, md5 ) );
514 public void testArtifactUploadFailedRepositoryNotFound()
517 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
518 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
521 repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
522 new RepositoryNotFoundException() );
524 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
525 getManagedRepository(), 1, 8 );
527 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
530 managedRepoAdminControl.replay();
531 archivaAdminControl.replay();
533 repoFactoryControl.replay();
535 String returnString = uploadAction.doUpload();
536 assertEquals( Action.ERROR, returnString );
538 repoFactoryControl.verify();
540 String repoLocation = getManagedRepository().getLocation();
542 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
545 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
548 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
551 public void testArtifactUploadSnapshots()
554 setUploadParameters( "1.0-SNAPSHOT", null, new File( FileUtil.getBasedir(),
555 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
558 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
559 content.setRepository( getManagedRepository() );
561 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
563 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
564 getManagedRepository(), 1, 8 );
566 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
569 managedRepoAdminControl.replay();
570 archivaAdminControl.replay();
572 repoFactoryControl.replay();
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" ) );
581 String returnString = uploadAction.doUpload();
582 assertEquals( Action.SUCCESS, returnString );
584 repoFactoryControl.verify();
587 String repoLocation = getManagedRepository().getLocation();
588 String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
589 Arrays.sort( artifactsList );
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() );
601 int startIndex = "artifact-upload-1.0-".length();
602 String timestampPath = getTimestamp( artifactsList, startIndex, 0 );
604 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
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" );
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 );
616 SnapshotVersion snapshotVersion = artifactMetadata.getSnapshotVersion();
617 assertEquals( "Incorrect build number set in artifact metadata.", 1, snapshotVersion.getBuildNumber() );
619 String timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
620 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
622 String buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
623 assertEquals( "Incorrect build number in filename.", "1", buildnumber );
625 repoFactoryControl.reset();
628 control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
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" ),
636 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
638 repoFactoryControl.replay();
640 fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
641 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
642 timestamp = fmt.format( new Date() );
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" ) );
648 returnString = uploadAction.doUpload();
649 assertEquals( Action.SUCCESS, returnString );
651 repoFactoryControl.verify();
654 artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
655 Arrays.sort( artifactsList );
657 assertEquals( 15, artifactsList.length );
659 timestampPath = getTimestamp( artifactsList, startIndex, 6 );
661 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
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" );
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 );
673 snapshotVersion = artifactMetadata.getSnapshotVersion();
674 assertEquals( "Incorrect build number set in artifact metadata.", 2, snapshotVersion.getBuildNumber() );
676 timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
677 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
679 buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
680 assertEquals( "Incorrect build number in filename.", "2", buildnumber );
683 public void testChecksumIsCorrectWhenArtifactIsReUploaded()
686 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
687 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
690 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
691 ManagedRepository repoConfig = getManagedRepository();
692 repoConfig.setBlockRedeployments( false );
693 content.setRepository( repoConfig );
695 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
697 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
700 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
703 managedRepoAdminControl.replay();
704 archivaAdminControl.replay();
706 repoFactoryControl.replay();
708 String returnString = uploadAction.doUpload();
709 assertEquals( Action.SUCCESS, returnString );
711 repoFactoryControl.verify();
713 repoFactoryControl.reset();
715 String repoLocation = getManagedRepository().getLocation();
716 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
718 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
719 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
720 verifyProjectMetadataChecksums( repoLocation );
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" ),
727 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
729 repoFactoryControl.replay();
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" ) );
737 returnString = uploadAction.doUpload();
738 assertEquals( Action.SUCCESS, returnString );
740 repoFactoryControl.verify();
743 repoLocation = getManagedRepository().getLocation();
744 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
746 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
747 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
748 verifyProjectMetadataChecksums( repoLocation );
751 public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
754 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
755 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
758 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
759 content.setRepository( getManagedRepository() );
761 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
763 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
764 getManagedRepository(), 1, 8 );
766 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
769 managedRepoAdminControl.replay();
770 archivaAdminControl.replay();
772 repoFactoryControl.replay();
774 String returnString = uploadAction.doUpload();
775 assertEquals( Action.SUCCESS, returnString );
777 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
778 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
781 MockControl control = mockAuditLogs( Collections.<String>emptyList() );
783 returnString = uploadAction.doUpload();
784 assertEquals( Action.ERROR, returnString );
786 repoFactoryControl.verify();
789 String repoLocation = getManagedRepository().getLocation();
790 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
792 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
793 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
794 verifyProjectMetadataChecksums( repoLocation );
797 public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
800 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
801 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
804 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
805 ManagedRepository repoConfig = getManagedRepository();
806 repoConfig.setBlockRedeployments( false );
807 content.setRepository( repoConfig );
809 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 1, 8 );
811 managedRepoAdminControl.expectAndReturn( managedRepositoryAdmin.getManagedRepository( REPOSITORY_ID ),
814 archivaAdminControl.expectAndReturn( archivaAdministration.getKnownContentConsumers(), new ArrayList<String>(),
817 managedRepoAdminControl.replay();
818 archivaAdminControl.replay();
820 repoFactoryControl.replay();
822 String returnString = uploadAction.doUpload();
823 assertEquals( Action.SUCCESS, returnString );
825 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
826 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
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" ) );
835 returnString = uploadAction.doUpload();
836 assertEquals( Action.SUCCESS, returnString );
838 repoFactoryControl.verify();
841 String repoLocation = getManagedRepository().getLocation();
842 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
844 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
845 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
846 verifyProjectMetadataChecksums( repoLocation );
849 ManagedRepository getManagedRepository()
851 return new BeanReplicator().replicateBean( this.managedRepository, ManagedRepository.class );