1 package org.apache.maven.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 org.apache.archiva.audit.AuditEvent;
24 import org.apache.archiva.audit.AuditListener;
25 import org.apache.archiva.checksum.ChecksumAlgorithm;
26 import org.apache.archiva.checksum.ChecksummedFile;
27 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.common.utils.FileUtil;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
35 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
36 import org.apache.maven.archiva.model.SnapshotVersion;
37 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
38 import org.apache.maven.archiva.repository.RepositoryContentFactory;
39 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
40 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
41 import org.apache.maven.archiva.repository.metadata.MetadataTools;
42 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
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 ArchivaConfiguration archivaConfig;
66 private MockControl archivaConfigControl;
68 private RepositoryContentFactory repoFactory;
70 private MockControl repoFactoryControl;
72 private static final String REPOSITORY_ID = "test-repo";
74 private Configuration config;
81 MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
82 ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
84 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
85 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
87 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
88 repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
90 uploadAction = new UploadAction();
91 uploadAction.setScheduler( scheduler );
92 uploadAction.setConfiguration( archivaConfig );
93 uploadAction.setRepositoryFactory( repoFactory );
95 File testRepo = new File( FileUtil.getBasedir(), "target/test-classes/test-repo" );
98 assertTrue( testRepo.exists() );
100 config = new Configuration();
101 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
102 repoConfig.setId( REPOSITORY_ID );
103 repoConfig.setLayout( "default" );
104 repoConfig.setLocation( testRepo.getPath() );
105 repoConfig.setName( REPOSITORY_ID );
106 repoConfig.setBlockRedeployments( true );
107 config.addManagedRepository( repoConfig );
109 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
110 repoScanning.setKnownContentConsumers( new ArrayList<String>() );
111 config.setRepositoryScanning( repoScanning );
114 public void tearDown()
117 File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
118 FileUtils.deleteDirectory( testRepo );
120 assertFalse( testRepo.exists() );
125 private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
126 boolean generatePom )
128 uploadAction.setRepositoryId( REPOSITORY_ID );
129 uploadAction.setGroupId( "org.apache.archiva" );
130 uploadAction.setArtifactId( "artifact-upload" );
131 uploadAction.setVersion( version );
132 uploadAction.setPackaging( "jar" );
134 uploadAction.setClassifier( classifier );
135 uploadAction.setArtifact( artifact );
136 uploadAction.setPom( pomFile );
137 uploadAction.setGeneratePom( generatePom );
140 private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation, String artifact,
143 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".jar" ).exists() );
144 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
145 + ".jar.sha1" ).exists() );
146 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
147 + ".jar.md5" ).exists() );
149 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact + ".pom" ).exists() );
150 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
151 + ".pom.sha1" ).exists() );
152 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/" + artifact
153 + ".pom.md5" ).exists() );
155 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
156 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
157 + ".sha1" ).exists() );
158 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
159 + ".md5" ).exists() );
162 private void verifyVersionMetadataChecksums( String repoLocation, String version )
165 ChecksummedFile checksum =
166 new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
167 + MetadataTools.MAVEN_METADATA ) );
168 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
169 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
172 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
173 + MetadataTools.MAVEN_METADATA + ".sha1" ) );
174 assertTrue( StringUtils.contains( contents, sha1 ) );
177 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
178 + MetadataTools.MAVEN_METADATA + ".md5" ) );
179 assertTrue( StringUtils.contains( contents, md5 ) );
182 private void verifyProjectMetadataChecksums( String repoLocation )
185 ChecksummedFile checksum =
186 new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
187 + MetadataTools.MAVEN_METADATA ) );
188 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
189 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
192 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
193 + MetadataTools.MAVEN_METADATA + ".sha1" ) );
194 assertTrue( StringUtils.contains( contents, sha1 ) );
197 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/"
198 + MetadataTools.MAVEN_METADATA + ".md5" ) );
199 assertTrue( StringUtils.contains( contents, md5 ) );
202 private void verifyPomChecksums( String repoLocation, String artifact, String version )
205 ChecksummedFile checksum;
210 new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
211 + artifact + ".pom" ) );
212 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
213 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
216 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
217 + artifact + ".pom.sha1" ) );
218 assertTrue( StringUtils.contains( contents, sha1 ) );
221 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
222 + artifact + ".pom.md5" ) );
223 assertTrue( StringUtils.contains( contents, md5 ) );
226 private void verifyArtifactChecksums( String repoLocation, String artifact, String version )
229 ChecksummedFile checksum =
230 new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
231 + artifact + ".jar" ) );
232 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
233 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
236 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
237 + artifact + ".jar.sha1" ) );
238 assertTrue( StringUtils.contains( contents, sha1 ) );
241 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + version + "/"
242 + artifact + ".jar.md5" ) );
243 assertTrue( StringUtils.contains( contents, md5 ) );
246 private String getTimestamp( String[] artifactsList, int startIndex, int index )
251 if ( artifactsList[index].contains( "jar" ) )
253 endIndex = artifactsList[index].indexOf( ".jar" );
257 endIndex = artifactsList[index].indexOf( ".pom" );
260 timestamp = artifactsList[index].substring( startIndex, endIndex );
265 private MockControl mockAuditLogs( List<String> resources )
267 return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
270 private MockControl mockAuditLogs( String action, List<String> resources )
272 MockControl control = MockControl.createControl( AuditListener.class );
273 AuditListener listener = (AuditListener) control.getMock();
274 boolean matcherSet = false;
275 for ( String resource : resources )
277 listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
280 control.setMatcher( new AuditEventArgumentsMatcher() );
286 uploadAction.setAuditListeners( Collections.singletonList( listener ) );
290 public void testArtifactUploadWithPomSuccessful()
293 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
294 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
295 new File( FileUtil.getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
297 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
298 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
300 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
301 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
303 archivaConfigControl.replay();
304 repoFactoryControl.replay();
306 MockControl control = mockAuditLogs(
307 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
308 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
310 String returnString = uploadAction.doUpload();
311 assertEquals( Action.SUCCESS, returnString );
313 archivaConfigControl.verify();
314 repoFactoryControl.verify();
317 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
318 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
320 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
321 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
322 verifyProjectMetadataChecksums( repoLocation );
325 public void testArtifactUploadWithClassifier()
328 setUploadParameters( "1.0", "tests", new File( FileUtil.getBasedir(),
329 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
330 new File( FileUtil.getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
332 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
333 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
335 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
336 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
338 archivaConfigControl.replay();
339 repoFactoryControl.replay();
341 MockControl control = mockAuditLogs(
342 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar",
343 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
345 String returnString = uploadAction.doUpload();
346 assertEquals( Action.SUCCESS, returnString );
348 archivaConfigControl.verify();
349 repoFactoryControl.verify();
352 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
353 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
354 assertTrue( new File( repoLocation,
355 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
356 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
358 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
359 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
360 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
362 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
363 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
364 + ".sha1" ).exists() );
365 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA
366 + ".md5" ).exists() );
368 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-tests", "1.0" );
369 verifyProjectMetadataChecksums( repoLocation );
372 public void testArtifactUploadGeneratePomSuccessful()
375 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
376 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
379 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
380 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
382 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
383 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
385 archivaConfigControl.replay();
386 repoFactoryControl.replay();
388 MockControl control = mockAuditLogs(
389 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
390 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
392 String returnString = uploadAction.doUpload();
393 assertEquals( Action.SUCCESS, returnString );
395 archivaConfigControl.verify();
396 repoFactoryControl.verify();
399 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
400 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
402 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
403 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
404 verifyProjectMetadataChecksums( repoLocation );
407 public void testArtifactUploadNoPomSuccessful()
410 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
411 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
414 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
415 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
417 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
418 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
420 archivaConfigControl.replay();
421 repoFactoryControl.replay();
423 MockControl control =
424 mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
426 String returnString = uploadAction.doUpload();
427 assertEquals( Action.SUCCESS, returnString );
429 archivaConfigControl.verify();
430 repoFactoryControl.verify();
433 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
435 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
437 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
439 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
442 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
444 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
446 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
449 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
450 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
451 ".sha1" ).exists() );
452 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
455 // verify checksums of jar file
456 ChecksummedFile checksum = new ChecksummedFile(
457 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
458 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
459 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
461 String contents = FileUtils.readFileToString(
462 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
463 assertTrue( StringUtils.contains( contents, sha1 ) );
465 contents = FileUtils.readFileToString(
466 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
467 assertTrue( StringUtils.contains( contents, md5 ) );
469 // verify checksums of metadata file
470 checksum = new ChecksummedFile(
471 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
472 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
473 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
475 contents = FileUtils.readFileToString(
476 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
477 assertTrue( StringUtils.contains( contents, sha1 ) );
479 contents = FileUtils.readFileToString(
480 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
481 assertTrue( StringUtils.contains( contents, md5 ) );
484 public void testArtifactUploadFailedRepositoryNotFound()
487 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
488 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
491 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
492 repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
493 new RepositoryNotFoundException() );
495 archivaConfigControl.replay();
496 repoFactoryControl.replay();
498 String returnString = uploadAction.doUpload();
499 assertEquals( Action.ERROR, returnString );
501 archivaConfigControl.verify();
502 repoFactoryControl.verify();
504 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
505 assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
507 assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
509 assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
512 public void testArtifactUploadSnapshots()
515 setUploadParameters( "1.0-SNAPSHOT", null, new File( FileUtil.getBasedir(),
516 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
519 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
520 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
522 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
523 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
525 archivaConfigControl.replay();
526 repoFactoryControl.replay();
528 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
529 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
530 String timestamp = fmt.format( new Date() );
531 MockControl control = mockAuditLogs( Arrays.asList(
532 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar",
533 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) );
535 String returnString = uploadAction.doUpload();
536 assertEquals( Action.SUCCESS, returnString );
538 archivaConfigControl.verify();
539 repoFactoryControl.verify();
542 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
543 String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
544 Arrays.sort( artifactsList );
546 assertEquals( 9, artifactsList.length );
547 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
548 + MetadataTools.MAVEN_METADATA ).exists() );
549 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
550 + MetadataTools.MAVEN_METADATA + ".sha1" ).exists() );
551 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/"
552 + MetadataTools.MAVEN_METADATA + ".md5" ).exists() );
554 int startIndex = "artifact-upload-1.0-".length();
555 String timestampPath = getTimestamp( artifactsList, startIndex, 0 );
557 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
559 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
560 verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
561 verifyProjectMetadataChecksums( repoLocation );
562 verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
564 // verify build number
566 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA );
567 ArchivaRepositoryMetadata artifactMetadata = RepositoryMetadataReader.read( metadataFile );
569 SnapshotVersion snapshotVersion = artifactMetadata.getSnapshotVersion();
570 assertEquals( "Incorrect build number set in artifact metadata.", 1, snapshotVersion.getBuildNumber() );
572 String timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
573 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
575 String buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
576 assertEquals( "Incorrect build number in filename.", "1", buildnumber );
578 archivaConfigControl.reset();
579 repoFactoryControl.reset();
582 control.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
585 // upload snapshot artifact again and check if build number was incremented
586 setUploadParameters( "1.0-SNAPSHOT", null,
587 new File( FileUtil.getBasedir(),
588 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
591 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
592 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
594 archivaConfigControl.replay();
595 repoFactoryControl.replay();
597 fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
598 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
599 timestamp = fmt.format( new Date() );
601 control = mockAuditLogs( Arrays.asList(
602 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-2.jar",
603 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-2.pom" ) );
605 returnString = uploadAction.doUpload();
606 assertEquals( Action.SUCCESS, returnString );
608 archivaConfigControl.verify();
609 repoFactoryControl.verify();
612 artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
613 Arrays.sort( artifactsList );
615 assertEquals( 15, artifactsList.length );
617 timestampPath = getTimestamp( artifactsList, startIndex, 6 );
619 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0-" + timestampPath,
621 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
622 verifyPomChecksums( repoLocation, "artifact-upload-1.0-" + timestampPath, "1.0-SNAPSHOT" );
623 verifyProjectMetadataChecksums( repoLocation );
624 verifyVersionMetadataChecksums( repoLocation, "1.0-SNAPSHOT" );
626 // verify build number set in metadata and in filename
628 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" + MetadataTools.MAVEN_METADATA );
629 artifactMetadata = RepositoryMetadataReader.read( metadataFile );
631 snapshotVersion = artifactMetadata.getSnapshotVersion();
632 assertEquals( "Incorrect build number set in artifact metadata.", 2, snapshotVersion.getBuildNumber() );
634 timestampPart = StringUtils.substringBeforeLast( timestampPath, "-" );
635 assertEquals( "Incorrect timestamp set in artifact metadata.", timestampPart, snapshotVersion.getTimestamp() );
637 buildnumber = StringUtils.substringAfterLast( timestampPath, "-" );
638 assertEquals( "Incorrect build number in filename.", "2", buildnumber );
641 public void testChecksumIsCorrectWhenArtifactIsReUploaded()
644 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
645 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
648 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
649 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
650 repoConfig.setBlockRedeployments( false );
651 content.setRepository( repoConfig );
653 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
654 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
656 archivaConfigControl.replay();
657 repoFactoryControl.replay();
659 String returnString = uploadAction.doUpload();
660 assertEquals( Action.SUCCESS, returnString );
662 archivaConfigControl.verify();
663 repoFactoryControl.verify();
665 archivaConfigControl.reset();
666 repoFactoryControl.reset();
668 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
669 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
671 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
672 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
673 verifyProjectMetadataChecksums( repoLocation );
675 // RE-upload artifact
676 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
677 "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
680 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
681 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
683 archivaConfigControl.replay();
684 repoFactoryControl.replay();
686 // TODO: track modifications?
687 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
688 MockControl control = mockAuditLogs(
689 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
690 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
692 returnString = uploadAction.doUpload();
693 assertEquals( Action.SUCCESS, returnString );
695 archivaConfigControl.verify();
696 repoFactoryControl.verify();
699 repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
700 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
702 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
703 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
704 verifyProjectMetadataChecksums( repoLocation );
707 public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
710 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
711 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
714 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
715 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
717 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
718 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
720 archivaConfigControl.replay();
721 repoFactoryControl.replay();
723 String returnString = uploadAction.doUpload();
724 assertEquals( Action.SUCCESS, returnString );
726 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
727 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
730 MockControl control = mockAuditLogs( Collections.<String>emptyList() );
732 returnString = uploadAction.doUpload();
733 assertEquals( Action.ERROR, returnString );
735 archivaConfigControl.verify();
736 repoFactoryControl.verify();
739 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
740 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
742 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
743 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
744 verifyProjectMetadataChecksums( repoLocation );
747 public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
750 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
751 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
754 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
755 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
756 repoConfig.setBlockRedeployments( false );
757 content.setRepository( repoConfig );
759 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
760 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
762 archivaConfigControl.replay();
763 repoFactoryControl.replay();
765 String returnString = uploadAction.doUpload();
766 assertEquals( Action.SUCCESS, returnString );
768 setUploadParameters( "1.0", null, new File( FileUtil.getBasedir(),
769 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
772 // TODO: track modifications?
773 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
774 MockControl control = mockAuditLogs(
775 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
776 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
778 returnString = uploadAction.doUpload();
779 assertEquals( Action.SUCCESS, returnString );
781 archivaConfigControl.verify();
782 repoFactoryControl.verify();
785 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
786 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation, "artifact-upload-1.0", "1.0" );
788 verifyArtifactChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
789 verifyPomChecksums( repoLocation, "artifact-upload-1.0", "1.0" );
790 verifyProjectMetadataChecksums( repoLocation );