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
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.TimeZone;
32 import com.opensymphony.xwork2.Action;
33 import org.apache.archiva.checksum.ChecksumAlgorithm;
34 import org.apache.archiva.checksum.ChecksummedFile;
35 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.lang.StringUtils;
38 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
39 import org.apache.maven.archiva.configuration.Configuration;
40 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
41 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.apache.maven.archiva.repository.RepositoryContentFactory;
44 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
45 import org.apache.maven.archiva.repository.audit.AuditEvent;
46 import org.apache.maven.archiva.repository.audit.AuditListener;
47 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
48 import org.apache.maven.archiva.repository.metadata.MetadataTools;
49 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
50 import org.easymock.ArgumentsMatcher;
51 import org.easymock.MockControl;
52 import org.easymock.classextension.MockClassControl;
57 public class UploadActionTest
58 extends PlexusInSpringTestCase
60 private UploadAction uploadAction;
62 private ArchivaConfiguration archivaConfig;
64 private MockControl archivaConfigControl;
66 private RepositoryContentFactory repoFactory;
68 private MockControl repoFactoryControl;
70 private static final String REPOSITORY_ID = "test-repo";
72 private Configuration config;
79 MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
80 ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
82 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
83 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
85 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
86 repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
88 uploadAction = new UploadAction();
89 uploadAction.setScheduler( scheduler );
90 uploadAction.setConfiguration( archivaConfig );
91 uploadAction.setRepositoryFactory( repoFactory );
93 File testRepo = new File( getBasedir(), "target/test-classes/test-repo" );
96 assertTrue( testRepo.exists() );
98 config = new Configuration();
99 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
100 repoConfig.setId( REPOSITORY_ID );
101 repoConfig.setLayout( "default" );
102 repoConfig.setLocation( testRepo.getPath() );
103 repoConfig.setName( REPOSITORY_ID );
104 repoConfig.setBlockRedeployments( true );
105 config.addManagedRepository( repoConfig );
107 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
108 repoScanning.setKnownContentConsumers( new ArrayList<String>() );
109 config.setRepositoryScanning( repoScanning );
112 public void tearDown()
115 File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
116 FileUtils.deleteDirectory( testRepo );
118 assertFalse( testRepo.exists() );
123 private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
124 boolean generatePom )
126 uploadAction.setRepositoryId( REPOSITORY_ID );
127 uploadAction.setGroupId( "org.apache.archiva" );
128 uploadAction.setArtifactId( "artifact-upload" );
129 uploadAction.setVersion( version );
130 uploadAction.setPackaging( "jar" );
132 uploadAction.setClassifier( classifier );
133 uploadAction.setArtifact( artifact );
134 uploadAction.setPom( pomFile );
135 uploadAction.setGeneratePom( generatePom );
138 private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation )
141 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
143 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
145 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
148 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
150 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
152 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
155 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 +
162 private void verifyChecksums( String repoLocation )
165 // verify checksums of jar file
166 ChecksummedFile checksum = new ChecksummedFile(
167 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
168 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
169 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
171 String contents = FileUtils.readFileToString(
172 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
173 assertTrue( StringUtils.contains( contents, sha1 ) );
175 contents = FileUtils.readFileToString(
176 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
177 assertTrue( StringUtils.contains( contents, md5 ) );
179 // verify checksums of pom file
180 checksum = new ChecksummedFile(
181 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
182 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
183 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
185 contents = FileUtils.readFileToString(
186 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
187 assertTrue( StringUtils.contains( contents, sha1 ) );
189 contents = FileUtils.readFileToString(
190 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
191 assertTrue( StringUtils.contains( contents, md5 ) );
193 // verify checksums of metadata file
194 checksum = new ChecksummedFile(
195 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
196 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
197 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
199 contents = FileUtils.readFileToString(
200 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
201 assertTrue( StringUtils.contains( contents, sha1 ) );
203 contents = FileUtils.readFileToString(
204 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
205 assertTrue( StringUtils.contains( contents, md5 ) );
208 public void testArtifactUploadWithPomSuccessful()
211 setUploadParameters( "1.0", null, new File( getBasedir(),
212 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
213 new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
215 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
216 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
218 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
219 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
221 archivaConfigControl.replay();
222 repoFactoryControl.replay();
224 MockControl control = mockAuditLogs(
225 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
226 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
228 String returnString = uploadAction.doUpload();
229 assertEquals( Action.SUCCESS, returnString );
231 archivaConfigControl.verify();
232 repoFactoryControl.verify();
235 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
236 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
238 verifyChecksums( repoLocation );
241 private MockControl mockAuditLogs( List<String> resources )
243 return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
246 private MockControl mockAuditLogs( String action, List<String> resources )
248 MockControl control = MockControl.createControl( AuditListener.class );
249 AuditListener listener = (AuditListener) control.getMock();
250 boolean matcherSet = false;
251 for ( String resource : resources )
253 listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
256 control.setMatcher( new AuditEventArgumentsMatcher() );
262 uploadAction.setAuditListeners( Collections.singletonList( listener ) );
266 public void testArtifactUploadWithClassifier()
269 setUploadParameters( "1.0", "tests", new File( getBasedir(),
270 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
271 new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
273 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
274 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
276 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
277 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
279 archivaConfigControl.replay();
280 repoFactoryControl.replay();
282 MockControl control = mockAuditLogs(
283 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar",
284 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
286 String returnString = uploadAction.doUpload();
287 assertEquals( Action.SUCCESS, returnString );
289 archivaConfigControl.verify();
290 repoFactoryControl.verify();
293 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
294 assertTrue( new File( repoLocation,
295 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
296 assertTrue( new File( repoLocation,
297 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
298 assertTrue( new File( repoLocation,
299 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
302 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
304 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
306 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
309 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
310 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
311 ".sha1" ).exists() );
312 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
315 // verify checksums of jar file
316 ChecksummedFile checksum = new ChecksummedFile(
317 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) );
318 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
319 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
321 String contents = FileUtils.readFileToString(
322 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) );
323 assertTrue( StringUtils.contains( contents, sha1 ) );
325 contents = FileUtils.readFileToString(
326 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) );
327 assertTrue( StringUtils.contains( contents, md5 ) );
329 // verify checksums of jar file
330 checksum = new ChecksummedFile(
331 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
332 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
333 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
335 contents = FileUtils.readFileToString(
336 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
337 assertTrue( StringUtils.contains( contents, sha1 ) );
339 contents = FileUtils.readFileToString(
340 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
341 assertTrue( StringUtils.contains( contents, md5 ) );
343 // verify checksums of metadata file
344 checksum = new ChecksummedFile(
345 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
346 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
347 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
349 contents = FileUtils.readFileToString(
350 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
351 assertTrue( StringUtils.contains( contents, sha1 ) );
353 contents = FileUtils.readFileToString(
354 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
355 assertTrue( StringUtils.contains( contents, md5 ) );
358 public void testArtifactUploadGeneratePomSuccessful()
361 setUploadParameters( "1.0", null, new File( getBasedir(),
362 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
365 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
366 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
368 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
369 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
371 archivaConfigControl.replay();
372 repoFactoryControl.replay();
374 MockControl control = mockAuditLogs(
375 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
376 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
378 String returnString = uploadAction.doUpload();
379 assertEquals( Action.SUCCESS, returnString );
381 archivaConfigControl.verify();
382 repoFactoryControl.verify();
385 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
386 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
388 verifyChecksums( repoLocation );
391 public void testArtifactUploadNoPomSuccessful()
394 setUploadParameters( "1.0", null, new File( getBasedir(),
395 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
398 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
399 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
401 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
402 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
404 archivaConfigControl.replay();
405 repoFactoryControl.replay();
407 MockControl control =
408 mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
410 String returnString = uploadAction.doUpload();
411 assertEquals( Action.SUCCESS, returnString );
413 archivaConfigControl.verify();
414 repoFactoryControl.verify();
417 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
419 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
421 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
423 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
426 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
428 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
430 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
433 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
434 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
435 ".sha1" ).exists() );
436 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
439 // verify checksums of jar file
440 ChecksummedFile checksum = new ChecksummedFile(
441 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
442 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
443 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
445 String contents = FileUtils.readFileToString(
446 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
447 assertTrue( StringUtils.contains( contents, sha1 ) );
449 contents = FileUtils.readFileToString(
450 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
451 assertTrue( StringUtils.contains( contents, md5 ) );
453 // verify checksums of metadata file
454 checksum = new ChecksummedFile(
455 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
456 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
457 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
459 contents = FileUtils.readFileToString(
460 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
461 assertTrue( StringUtils.contains( contents, sha1 ) );
463 contents = FileUtils.readFileToString(
464 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
465 assertTrue( StringUtils.contains( contents, md5 ) );
468 public void testArtifactUploadFailedRepositoryNotFound()
471 setUploadParameters( "1.0", null, new File( getBasedir(),
472 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
475 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
476 repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
477 new RepositoryNotFoundException() );
479 archivaConfigControl.replay();
480 repoFactoryControl.replay();
482 String returnString = uploadAction.doUpload();
483 assertEquals( Action.ERROR, returnString );
485 archivaConfigControl.verify();
486 repoFactoryControl.verify();
488 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
490 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
493 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
496 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
499 public void testArtifactUploadSnapshots()
502 setUploadParameters( "1.0-SNAPSHOT", null, new File( getBasedir(),
503 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
506 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
507 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
509 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
510 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
512 archivaConfigControl.replay();
513 repoFactoryControl.replay();
515 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
516 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
517 String timestamp = fmt.format( new Date() );
518 MockControl control = mockAuditLogs( Arrays.asList(
519 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar",
520 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) );
522 String returnString = uploadAction.doUpload();
523 assertEquals( Action.SUCCESS, returnString );
525 archivaConfigControl.verify();
526 repoFactoryControl.verify();
529 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
530 assertEquals( 6, new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list().length );
533 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
534 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
535 ".sha1" ).exists() );
536 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
540 public void testChecksumIsCorrectWhenArtifactIsReUploaded()
543 setUploadParameters( "1.0", null, new File( getBasedir(),
544 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
547 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
548 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
549 repoConfig.setBlockRedeployments( false );
550 content.setRepository( repoConfig );
552 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
553 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
555 archivaConfigControl.replay();
556 repoFactoryControl.replay();
558 String returnString = uploadAction.doUpload();
559 assertEquals( Action.SUCCESS, returnString );
561 archivaConfigControl.verify();
562 repoFactoryControl.verify();
564 archivaConfigControl.reset();
565 repoFactoryControl.reset();
567 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
568 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
570 verifyChecksums( repoLocation );
572 // RE-upload artifact
573 setUploadParameters( "1.0", null, new File( getBasedir(),
574 "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
577 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
578 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
580 archivaConfigControl.replay();
581 repoFactoryControl.replay();
583 // TODO: track modifications?
584 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
585 MockControl control = mockAuditLogs(
586 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
587 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
589 returnString = uploadAction.doUpload();
590 assertEquals( Action.SUCCESS, returnString );
592 archivaConfigControl.verify();
593 repoFactoryControl.verify();
596 repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
597 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
599 verifyChecksums( repoLocation );
602 public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
605 setUploadParameters( "1.0", null, new File( getBasedir(),
606 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
609 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
610 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
612 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
613 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
615 archivaConfigControl.replay();
616 repoFactoryControl.replay();
618 String returnString = uploadAction.doUpload();
619 assertEquals( Action.SUCCESS, returnString );
621 setUploadParameters( "1.0", null, new File( getBasedir(),
622 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
625 MockControl control = mockAuditLogs( Collections.<String>emptyList() );
627 returnString = uploadAction.doUpload();
628 assertEquals( Action.ERROR, returnString );
630 archivaConfigControl.verify();
631 repoFactoryControl.verify();
634 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
635 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
637 verifyChecksums( repoLocation );
640 public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
643 setUploadParameters( "1.0", null, new File( getBasedir(),
644 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
647 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
648 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
649 repoConfig.setBlockRedeployments( false );
650 content.setRepository( repoConfig );
652 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
653 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
655 archivaConfigControl.replay();
656 repoFactoryControl.replay();
658 String returnString = uploadAction.doUpload();
659 assertEquals( Action.SUCCESS, returnString );
661 setUploadParameters( "1.0", null, new File( getBasedir(),
662 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
665 // TODO: track modifications?
666 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
667 MockControl control = mockAuditLogs(
668 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
669 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
671 returnString = uploadAction.doUpload();
672 assertEquals( Action.SUCCESS, returnString );
674 archivaConfigControl.verify();
675 repoFactoryControl.verify();
678 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
679 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
681 verifyChecksums( repoLocation );
684 private static class AuditEventArgumentsMatcher
685 implements ArgumentsMatcher
687 public boolean matches( Object[] objects, Object[] objects1 )
689 if ( objects.length != 1 || objects1.length != 1 )
695 AuditEvent o1 = (AuditEvent) objects[0];
696 AuditEvent o2 = (AuditEvent) objects1[0];
697 o2.setTimestamp( o1.getTimestamp() ); // effectively ignore the timestamp
698 return o1.equals( o2 );
702 public String toString( Object[] objects )
704 return Arrays.asList( objects ).toString();