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.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
37 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
38 import org.apache.maven.archiva.repository.metadata.MetadataTools;
39 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
40 import org.easymock.MockControl;
41 import org.easymock.classextension.MockClassControl;
44 import java.io.IOException;
45 import java.text.SimpleDateFormat;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.List;
51 import java.util.TimeZone;
56 public class UploadActionTest
57 extends PlexusInSpringTestCase
59 private UploadAction uploadAction;
61 private ArchivaConfiguration archivaConfig;
63 private MockControl archivaConfigControl;
65 private RepositoryContentFactory repoFactory;
67 private MockControl repoFactoryControl;
69 private static final String REPOSITORY_ID = "test-repo";
71 private Configuration config;
78 MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
79 ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
81 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
82 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
84 repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
85 repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
87 uploadAction = new UploadAction();
88 uploadAction.setScheduler( scheduler );
89 uploadAction.setConfiguration( archivaConfig );
90 uploadAction.setRepositoryFactory( repoFactory );
92 File testRepo = new File( getBasedir(), "target/test-classes/test-repo" );
95 assertTrue( testRepo.exists() );
97 config = new Configuration();
98 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
99 repoConfig.setId( REPOSITORY_ID );
100 repoConfig.setLayout( "default" );
101 repoConfig.setLocation( testRepo.getPath() );
102 repoConfig.setName( REPOSITORY_ID );
103 repoConfig.setBlockRedeployments( true );
104 config.addManagedRepository( repoConfig );
106 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
107 repoScanning.setKnownContentConsumers( new ArrayList<String>() );
108 config.setRepositoryScanning( repoScanning );
111 public void tearDown()
114 File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
115 FileUtils.deleteDirectory( testRepo );
117 assertFalse( testRepo.exists() );
122 private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
123 boolean generatePom )
125 uploadAction.setRepositoryId( REPOSITORY_ID );
126 uploadAction.setGroupId( "org.apache.archiva" );
127 uploadAction.setArtifactId( "artifact-upload" );
128 uploadAction.setVersion( version );
129 uploadAction.setPackaging( "jar" );
131 uploadAction.setClassifier( classifier );
132 uploadAction.setArtifact( artifact );
133 uploadAction.setPom( pomFile );
134 uploadAction.setGeneratePom( generatePom );
137 private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation )
140 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
142 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
144 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
147 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
149 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
151 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
154 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
155 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
156 ".sha1" ).exists() );
157 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
161 private void verifyChecksums( String repoLocation )
164 // verify checksums of jar file
165 ChecksummedFile checksum = new ChecksummedFile(
166 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
167 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
168 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
170 String contents = FileUtils.readFileToString(
171 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
172 assertTrue( StringUtils.contains( contents, sha1 ) );
174 contents = FileUtils.readFileToString(
175 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
176 assertTrue( StringUtils.contains( contents, md5 ) );
178 // verify checksums of pom file
179 checksum = new ChecksummedFile(
180 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
181 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
182 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
184 contents = FileUtils.readFileToString(
185 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
186 assertTrue( StringUtils.contains( contents, sha1 ) );
188 contents = FileUtils.readFileToString(
189 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
190 assertTrue( StringUtils.contains( contents, md5 ) );
192 // verify checksums of metadata file
193 checksum = new ChecksummedFile(
194 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
195 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
196 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
198 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 public void testArtifactUploadWithPomSuccessful()
210 setUploadParameters( "1.0", null, new File( getBasedir(),
211 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
212 new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
214 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
215 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
217 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
218 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
220 archivaConfigControl.replay();
221 repoFactoryControl.replay();
223 MockControl control = mockAuditLogs(
224 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
225 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
227 String returnString = uploadAction.doUpload();
228 assertEquals( Action.SUCCESS, returnString );
230 archivaConfigControl.verify();
231 repoFactoryControl.verify();
234 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
235 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
237 verifyChecksums( repoLocation );
240 private MockControl mockAuditLogs( List<String> resources )
242 return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
245 private MockControl mockAuditLogs( String action, List<String> resources )
247 MockControl control = MockControl.createControl( AuditListener.class );
248 AuditListener listener = (AuditListener) control.getMock();
249 boolean matcherSet = false;
250 for ( String resource : resources )
252 listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
255 control.setMatcher( new AuditEventArgumentsMatcher() );
261 uploadAction.setAuditListeners( Collections.singletonList( listener ) );
265 public void testArtifactUploadWithClassifier()
268 setUploadParameters( "1.0", "tests", new File( getBasedir(),
269 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
270 new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
272 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
273 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
275 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
276 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
278 archivaConfigControl.replay();
279 repoFactoryControl.replay();
281 MockControl control = mockAuditLogs(
282 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar",
283 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
285 String returnString = uploadAction.doUpload();
286 assertEquals( Action.SUCCESS, returnString );
288 archivaConfigControl.verify();
289 repoFactoryControl.verify();
292 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
293 assertTrue( new File( repoLocation,
294 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
295 assertTrue( new File( repoLocation,
296 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
297 assertTrue( new File( repoLocation,
298 "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
301 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
303 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
305 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
308 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
309 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
310 ".sha1" ).exists() );
311 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
314 // verify checksums of jar file
315 ChecksummedFile checksum = new ChecksummedFile(
316 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) );
317 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
318 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
320 String contents = FileUtils.readFileToString(
321 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) );
322 assertTrue( StringUtils.contains( contents, sha1 ) );
324 contents = FileUtils.readFileToString(
325 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) );
326 assertTrue( StringUtils.contains( contents, md5 ) );
328 // verify checksums of jar file
329 checksum = new ChecksummedFile(
330 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
331 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
332 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
334 contents = FileUtils.readFileToString(
335 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
336 assertTrue( StringUtils.contains( contents, sha1 ) );
338 contents = FileUtils.readFileToString(
339 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
340 assertTrue( StringUtils.contains( contents, md5 ) );
342 // verify checksums of metadata file
343 checksum = new ChecksummedFile(
344 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
345 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
346 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
348 contents = FileUtils.readFileToString(
349 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
350 assertTrue( StringUtils.contains( contents, sha1 ) );
352 contents = FileUtils.readFileToString(
353 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
354 assertTrue( StringUtils.contains( contents, md5 ) );
357 public void testArtifactUploadGeneratePomSuccessful()
360 setUploadParameters( "1.0", null, new File( getBasedir(),
361 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
364 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
365 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
367 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
368 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
370 archivaConfigControl.replay();
371 repoFactoryControl.replay();
373 MockControl control = mockAuditLogs(
374 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
375 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
377 String returnString = uploadAction.doUpload();
378 assertEquals( Action.SUCCESS, returnString );
380 archivaConfigControl.verify();
381 repoFactoryControl.verify();
384 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
385 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
387 verifyChecksums( repoLocation );
390 public void testArtifactUploadNoPomSuccessful()
393 setUploadParameters( "1.0", null, new File( getBasedir(),
394 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
397 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
398 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
400 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
401 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
403 archivaConfigControl.replay();
404 repoFactoryControl.replay();
406 MockControl control =
407 mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
409 String returnString = uploadAction.doUpload();
410 assertEquals( Action.SUCCESS, returnString );
412 archivaConfigControl.verify();
413 repoFactoryControl.verify();
416 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
418 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
420 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
422 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
425 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
427 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
429 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
432 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
433 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
434 ".sha1" ).exists() );
435 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
438 // verify checksums of jar file
439 ChecksummedFile checksum = new ChecksummedFile(
440 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
441 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
442 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
444 String contents = FileUtils.readFileToString(
445 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
446 assertTrue( StringUtils.contains( contents, sha1 ) );
448 contents = FileUtils.readFileToString(
449 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
450 assertTrue( StringUtils.contains( contents, md5 ) );
452 // verify checksums of metadata file
453 checksum = new ChecksummedFile(
454 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
455 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
456 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
458 contents = FileUtils.readFileToString(
459 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
460 assertTrue( StringUtils.contains( contents, sha1 ) );
462 contents = FileUtils.readFileToString(
463 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
464 assertTrue( StringUtils.contains( contents, md5 ) );
467 public void testArtifactUploadFailedRepositoryNotFound()
470 setUploadParameters( "1.0", null, new File( getBasedir(),
471 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
474 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
475 repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
476 new RepositoryNotFoundException() );
478 archivaConfigControl.replay();
479 repoFactoryControl.replay();
481 String returnString = uploadAction.doUpload();
482 assertEquals( Action.ERROR, returnString );
484 archivaConfigControl.verify();
485 repoFactoryControl.verify();
487 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
489 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
492 new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
495 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
498 public void testArtifactUploadSnapshots()
501 setUploadParameters( "1.0-SNAPSHOT", null, new File( getBasedir(),
502 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
505 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
506 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
508 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
509 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
511 archivaConfigControl.replay();
512 repoFactoryControl.replay();
514 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
515 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
516 String timestamp = fmt.format( new Date() );
517 MockControl control = mockAuditLogs( Arrays.asList(
518 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar",
519 "org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) );
521 String returnString = uploadAction.doUpload();
522 assertEquals( Action.SUCCESS, returnString );
524 archivaConfigControl.verify();
525 repoFactoryControl.verify();
528 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
529 String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
531 assertEquals( 6, artifactsList.length );
534 new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
535 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
536 ".sha1" ).exists() );
537 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
540 int startIndex = "artifact-upload-1.0-".length();
543 if ( artifactsList[0].contains( "jar" ) )
545 endIndex = artifactsList[0].indexOf( ".jar" );
549 endIndex = artifactsList[0].indexOf( ".pom" );
552 String actualTimestamp = artifactsList[0].substring( startIndex, endIndex );
554 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar" ).exists() );
555 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar.md5" ).exists() );
556 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar.sha1" ).exists() );
557 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom" ).exists() );
558 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom.md5" ).exists() );
559 assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom.sha1" ).exists() );
561 // verify checksums of jar file
562 ChecksummedFile checksum =
563 new ChecksummedFile( new File( repoLocation,
564 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar" ) );
565 String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
566 String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
569 FileUtils.readFileToString( new File( repoLocation,
570 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar.sha1" ) );
571 assertTrue( StringUtils.contains( contents, sha1 ) );
574 FileUtils.readFileToString( new File( repoLocation,
575 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".jar.md5" ) );
576 assertTrue( StringUtils.contains( contents, md5 ) );
578 // verify checksums of pom file
580 new ChecksummedFile( new File( repoLocation,
581 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom" ) );
582 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
583 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
586 FileUtils.readFileToString( new File( repoLocation,
587 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom.sha1" ) );
588 assertTrue( StringUtils.contains( contents, sha1 ) );
591 FileUtils.readFileToString( new File( repoLocation,
592 "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + actualTimestamp + ".pom.md5" ) );
593 assertTrue( StringUtils.contains( contents, md5 ) );
596 // verify checksums of metadata file
598 new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
599 MetadataTools.MAVEN_METADATA ) );
600 sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
601 md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
604 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
605 MetadataTools.MAVEN_METADATA + ".sha1" ) );
606 assertTrue( StringUtils.contains( contents, sha1 ) );
609 FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
610 MetadataTools.MAVEN_METADATA + ".md5" ) );
611 assertTrue( StringUtils.contains( contents, md5 ) );
614 public void testChecksumIsCorrectWhenArtifactIsReUploaded()
617 setUploadParameters( "1.0", null, new File( getBasedir(),
618 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
621 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
622 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
623 repoConfig.setBlockRedeployments( false );
624 content.setRepository( repoConfig );
626 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
627 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
629 archivaConfigControl.replay();
630 repoFactoryControl.replay();
632 String returnString = uploadAction.doUpload();
633 assertEquals( Action.SUCCESS, returnString );
635 archivaConfigControl.verify();
636 repoFactoryControl.verify();
638 archivaConfigControl.reset();
639 repoFactoryControl.reset();
641 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
642 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
644 verifyChecksums( repoLocation );
646 // RE-upload artifact
647 setUploadParameters( "1.0", null, new File( getBasedir(),
648 "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
651 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
652 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
654 archivaConfigControl.replay();
655 repoFactoryControl.replay();
657 // TODO: track modifications?
658 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
659 MockControl control = mockAuditLogs(
660 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
661 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
663 returnString = uploadAction.doUpload();
664 assertEquals( Action.SUCCESS, returnString );
666 archivaConfigControl.verify();
667 repoFactoryControl.verify();
670 repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
671 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
673 verifyChecksums( repoLocation );
676 public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
679 setUploadParameters( "1.0", null, new File( getBasedir(),
680 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
683 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
684 content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
686 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
687 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
689 archivaConfigControl.replay();
690 repoFactoryControl.replay();
692 String returnString = uploadAction.doUpload();
693 assertEquals( Action.SUCCESS, returnString );
695 setUploadParameters( "1.0", null, new File( getBasedir(),
696 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
699 MockControl control = mockAuditLogs( Collections.<String>emptyList() );
701 returnString = uploadAction.doUpload();
702 assertEquals( Action.ERROR, returnString );
704 archivaConfigControl.verify();
705 repoFactoryControl.verify();
708 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
709 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
711 verifyChecksums( repoLocation );
714 public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
717 setUploadParameters( "1.0", null, new File( getBasedir(),
718 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
721 ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
722 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
723 repoConfig.setBlockRedeployments( false );
724 content.setRepository( repoConfig );
726 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
727 repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
729 archivaConfigControl.replay();
730 repoFactoryControl.replay();
732 String returnString = uploadAction.doUpload();
733 assertEquals( Action.SUCCESS, returnString );
735 setUploadParameters( "1.0", null, new File( getBasedir(),
736 "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
739 // TODO: track modifications?
740 // MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
741 MockControl control = mockAuditLogs(
742 Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
743 "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
745 returnString = uploadAction.doUpload();
746 assertEquals( Action.SUCCESS, returnString );
748 archivaConfigControl.verify();
749 repoFactoryControl.verify();
752 String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
753 assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
755 verifyChecksums( repoLocation );