]> source.dussan.org Git - archiva.git/blob
4ffa8e1b7b6c240de19f4c556c994b871d3c050e
[archiva.git] /
1 package org.apache.maven.archiva.web.action;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25
26 import org.apache.archiva.checksum.ChecksumAlgorithm;
27 import org.apache.archiva.checksum.ChecksummedFile;
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.database.ArchivaAuditLogsDao;
35 import org.apache.maven.archiva.model.ArchivaAuditLogs;
36 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
37 import org.apache.maven.archiva.repository.RepositoryContentFactory;
38 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
39 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
40 import org.apache.maven.archiva.repository.metadata.MetadataTools;
41 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
42 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 import org.easymock.MockControl;
44 import org.easymock.classextension.MockClassControl;
45
46 import com.opensymphony.xwork2.Action;
47
48 /**
49  * UploadActionTest
50  */
51 public class UploadActionTest
52     extends PlexusInSpringTestCase
53 {
54     private ArchivaTaskScheduler scheduler;
55
56     private MockControl schedulerControl;
57
58     private UploadAction uploadAction;
59
60     private ArchivaConfiguration archivaConfig;
61
62     private MockControl archivaConfigControl;
63
64     private RepositoryContentFactory repoFactory;
65
66     private MockControl repoFactoryControl;
67     
68     private ArchivaAuditLogsDao auditLogsDao;
69
70     private MockControl auditLogsDaoControl;
71
72     private static final String REPOSITORY_ID = "test-repo";
73
74     private Configuration config;    
75
76     public void setUp()
77         throws Exception
78     {
79         super.setUp();
80
81         schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
82         scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
83
84         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
85         archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
86
87         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
88         repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
89         
90         auditLogsDaoControl = MockControl.createControl( ArchivaAuditLogsDao.class );
91         auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
92         auditLogsDao = (ArchivaAuditLogsDao) auditLogsDaoControl.getMock();
93
94         uploadAction = new UploadAction();
95         uploadAction.setScheduler( scheduler );
96         uploadAction.setConfiguration( archivaConfig );
97         uploadAction.setRepositoryFactory( repoFactory );
98         uploadAction.setAuditLogsDao( auditLogsDao );
99
100         File testRepo = new File( getBasedir(), "target/test-classes/test-repo" );
101         testRepo.mkdirs();
102         
103         assertTrue( testRepo.exists() );
104         
105         config = new Configuration();
106         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
107         repoConfig.setId( REPOSITORY_ID );
108         repoConfig.setLayout( "default" );
109         repoConfig.setLocation( testRepo.getPath() );
110         repoConfig.setName( REPOSITORY_ID );
111         repoConfig.setBlockRedeployments( true );
112         config.addManagedRepository( repoConfig );
113         
114         RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
115         repoScanning.setKnownContentConsumers( new ArrayList<String>() );
116         config.setRepositoryScanning( repoScanning );
117     }
118
119     public void tearDown()
120         throws Exception
121     {
122         File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
123         FileUtils.deleteDirectory( testRepo );
124
125         assertFalse( testRepo.exists() );
126
127         super.tearDown();
128     }
129
130     private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
131                                       boolean generatePom )
132     {
133         uploadAction.setRepositoryId( REPOSITORY_ID );
134         uploadAction.setGroupId( "org.apache.archiva" );
135         uploadAction.setArtifactId( "artifact-upload" );
136         uploadAction.setVersion( version );
137         uploadAction.setPackaging( "jar" );
138
139         uploadAction.setClassifier( classifier );
140         uploadAction.setArtifact( artifact );
141         uploadAction.setPom( pomFile );
142         uploadAction.setGeneratePom( generatePom );
143     }
144
145     private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation )
146     {
147         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
148         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
149         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
150
151         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
152         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
153         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
154
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() );
160     }
161
162     private void verifyChecksums( String repoLocation )
163         throws IOException
164     {
165         // verify checksums of jar file
166         ChecksummedFile checksum =
167             new ChecksummedFile( new File( repoLocation,
168                                            "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
169         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
170         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
171
172         String contents =
173             FileUtils.readFileToString( new File( repoLocation,
174                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
175         assertTrue( StringUtils.contains( contents, sha1 ) );
176
177         contents =
178             FileUtils.readFileToString( new File( repoLocation,
179                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
180         assertTrue( StringUtils.contains( contents, md5 ) );
181
182         // verify checksums of pom file
183         checksum =
184             new ChecksummedFile( new File( repoLocation,
185                                            "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
186         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
187         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
188
189         contents =
190             FileUtils.readFileToString( new File( repoLocation,
191                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
192         assertTrue( StringUtils.contains( contents, sha1 ) );
193
194         contents =
195             FileUtils.readFileToString( new File( repoLocation,
196                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
197         assertTrue( StringUtils.contains( contents, md5 ) );
198
199         // verify checksums of metadata file
200         checksum =
201             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
202                 MetadataTools.MAVEN_METADATA ) );
203         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
204         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
205
206         contents =
207             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
208                 MetadataTools.MAVEN_METADATA + ".sha1" ) );
209         assertTrue( StringUtils.contains( contents, sha1 ) );
210
211         contents =
212             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
213                 MetadataTools.MAVEN_METADATA + ".md5" ) );
214         assertTrue( StringUtils.contains( contents, md5 ) );
215     }
216
217     public void testArtifactUploadWithPomSuccessful()
218         throws Exception
219     {
220         setUploadParameters( "1.0", null,
221                              new File( getBasedir(),
222                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
223                              new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
224
225         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
226         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
227
228         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
229         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
230         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
231
232         archivaConfigControl.replay();
233         repoFactoryControl.replay();
234         auditLogsDaoControl.replay();
235
236         String returnString = uploadAction.doUpload();
237         assertEquals( Action.SUCCESS, returnString );
238
239         archivaConfigControl.verify();
240         repoFactoryControl.verify();
241         auditLogsDaoControl.verify();
242         
243         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
244         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
245
246         verifyChecksums( repoLocation );
247     }
248
249     public void testArtifactUploadWithClassifier()
250         throws Exception
251     {
252         setUploadParameters( "1.0", "tests",
253                              new File( getBasedir(),
254                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
255                              false );
256
257         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
258         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
259
260         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
261         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
262         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
263
264         archivaConfigControl.replay();
265         repoFactoryControl.replay();
266         auditLogsDaoControl.replay();
267         
268         String returnString = uploadAction.doUpload();
269         assertEquals( Action.SUCCESS, returnString );
270
271         archivaConfigControl.verify();
272         repoFactoryControl.verify();
273         auditLogsDaoControl.verify();
274
275         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
276         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
277         assertTrue( new File( repoLocation,
278                               "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
279         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
280
281         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
282         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
283         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
284
285         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
286         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
287             ".sha1" ).exists() );
288         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
289             ".md5" ).exists() );
290
291         // verify checksums of jar file
292         ChecksummedFile checksum =
293             new ChecksummedFile( new File( repoLocation,
294                                            "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) );
295         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
296         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
297
298         String contents =
299             FileUtils.readFileToString( new File( repoLocation,
300                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) );
301         assertTrue( StringUtils.contains( contents, sha1 ) );
302
303         contents =
304             FileUtils.readFileToString( new File( repoLocation,
305                                                   "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) );
306         assertTrue( StringUtils.contains( contents, md5 ) );
307
308         // verify checksums of metadata file
309         checksum =
310             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
311                 MetadataTools.MAVEN_METADATA ) );
312         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
313         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
314
315         contents =
316             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
317                 MetadataTools.MAVEN_METADATA + ".sha1" ) );
318         assertTrue( StringUtils.contains( contents, sha1 ) );
319
320         contents =
321             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
322                 MetadataTools.MAVEN_METADATA + ".md5" ) );
323         assertTrue( StringUtils.contains( contents, md5 ) );
324     }
325
326     public void testArtifactUploadGeneratePomSuccessful()
327         throws Exception
328     {
329         setUploadParameters( "1.0", null,
330                              new File( getBasedir(),
331                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
332                              true );
333
334         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
335         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
336
337         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
338         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
339         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
340
341         archivaConfigControl.replay();
342         repoFactoryControl.replay();
343         auditLogsDaoControl.replay();
344
345         String returnString = uploadAction.doUpload();
346         assertEquals( Action.SUCCESS, returnString );
347
348         archivaConfigControl.verify();
349         repoFactoryControl.verify();
350         auditLogsDaoControl.verify();
351
352         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
353         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
354
355         verifyChecksums( repoLocation );
356     }
357
358     public void testArtifactUploadFailedRepositoryNotFound()
359         throws Exception
360     {        
361         setUploadParameters( "1.0", null,
362                              new File( getBasedir(),
363                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
364                              false );
365
366         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
367         repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
368                                            new RepositoryNotFoundException() );
369
370         archivaConfigControl.replay();
371         repoFactoryControl.replay();
372
373         String returnString = uploadAction.doUpload();
374         assertEquals( Action.ERROR, returnString );
375
376         archivaConfigControl.verify();
377         repoFactoryControl.verify();
378
379         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
380         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
381
382         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
383
384         assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
385     }
386
387     public void testArtifactUploadSnapshots()
388         throws Exception
389     {
390         setUploadParameters( "1.0-SNAPSHOT", null,
391                              new File( getBasedir(),
392                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
393                              true );
394
395         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
396         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
397
398         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
399         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
400         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
401         
402         archivaConfigControl.replay();
403         repoFactoryControl.replay();
404         auditLogsDaoControl.replay();
405        
406         String returnString = uploadAction.doUpload();
407         assertEquals( Action.SUCCESS, returnString );
408
409         archivaConfigControl.verify();
410         repoFactoryControl.verify();
411         auditLogsDaoControl.verify();
412
413         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
414         String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
415
416         assertEquals( 6, artifactsList.length );
417
418         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
419         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
420             ".sha1" ).exists() );
421         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
422             ".md5" ).exists() );
423
424         int startIndex = "artifact-upload-1.0-".length();
425         int endIndex = -1; 
426
427         if ( artifactsList[0].contains( "jar" ) )
428         {
429             endIndex = artifactsList[0].indexOf( ".jar" );
430         }
431         else
432         {
433             endIndex = artifactsList[0].indexOf( ".pom" );
434         }
435
436         String timestamp = artifactsList[0].substring( startIndex, endIndex );
437
438         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar" ).exists()  );
439         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar.md5" ).exists()  );
440         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar.sha1" ).exists()  );
441         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom" ).exists()  );
442         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom.md5" ).exists()  );
443         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom.sha1" ).exists()  );
444
445         // verify checksums of jar file
446         ChecksummedFile checksum =
447             new ChecksummedFile( new File( repoLocation,
448                                            "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar" ) );
449         String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
450         String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
451
452         String contents =
453             FileUtils.readFileToString( new File( repoLocation,
454                                                   "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar.sha1" ) );
455         assertTrue( StringUtils.contains( contents, sha1 ) );
456
457         contents =
458             FileUtils.readFileToString( new File( repoLocation,
459                                                   "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".jar.md5" ) );
460         assertTrue( StringUtils.contains( contents, md5 ) );
461
462         // verify checksums of pom file
463         checksum =
464             new ChecksummedFile( new File( repoLocation,
465                                            "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom" ) );
466         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
467         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
468
469         contents =
470             FileUtils.readFileToString( new File( repoLocation,
471                                                   "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom.sha1" ) );
472         assertTrue( StringUtils.contains( contents, sha1 ) );
473
474         contents =
475             FileUtils.readFileToString( new File( repoLocation,
476                                                   "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + ".pom.md5" ) );
477         assertTrue( StringUtils.contains( contents, md5 ) );
478
479         
480         // verify checksums of metadata file
481         checksum =
482             new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
483                 MetadataTools.MAVEN_METADATA ) );
484         sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
485         md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
486
487         contents =
488             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
489                 MetadataTools.MAVEN_METADATA + ".sha1" ) );
490         assertTrue( StringUtils.contains( contents, sha1 ) );
491
492         contents =
493             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
494                 MetadataTools.MAVEN_METADATA + ".md5" ) );
495         assertTrue( StringUtils.contains( contents, md5 ) );
496     }
497
498     public void testChecksumIsCorrectWhenArtifactIsReUploaded()
499         throws Exception
500     {
501         setUploadParameters( "1.0", null,
502                              new File( getBasedir(),
503                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
504                              true );
505
506         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
507         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
508         repoConfig.setBlockRedeployments( false );        
509         content.setRepository( repoConfig );      
510         
511         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
512         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
513         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
514         
515         archivaConfigControl.replay();
516         repoFactoryControl.replay();
517         auditLogsDaoControl.replay();
518
519         String returnString = uploadAction.doUpload();
520         assertEquals( Action.SUCCESS, returnString );
521
522         archivaConfigControl.verify();
523         repoFactoryControl.verify();
524         auditLogsDaoControl.verify();
525
526         archivaConfigControl.reset();
527         repoFactoryControl.reset();
528         auditLogsDaoControl.reset();
529         auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
530
531         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
532         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
533
534         verifyChecksums( repoLocation );
535
536         // RE-upload artifact
537         setUploadParameters( "1.0", null,
538                              new File( getBasedir(),
539                                        "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
540                              null, true );
541
542         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
543         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
544         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
545         
546         archivaConfigControl.replay();
547         repoFactoryControl.replay();
548         auditLogsDaoControl.replay();
549
550         returnString = uploadAction.doUpload();
551         assertEquals( Action.SUCCESS, returnString );
552
553         archivaConfigControl.verify();
554         repoFactoryControl.verify();
555         auditLogsDaoControl.verify();
556
557         repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
558         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
559
560         verifyChecksums( repoLocation );
561     }
562     
563     public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
564         throws Exception
565     {   
566         setUploadParameters( "1.0", null,
567                              new File( getBasedir(),
568                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
569                              true );
570
571         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
572         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
573
574         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
575         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
576         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
577         
578         archivaConfigControl.replay();
579         repoFactoryControl.replay();
580         auditLogsDaoControl.replay();
581
582         String returnString = uploadAction.doUpload();
583         assertEquals( Action.SUCCESS, returnString );
584         
585         setUploadParameters( "1.0", null,
586                              new File( getBasedir(),
587                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
588                              true );
589         
590         returnString = uploadAction.doUpload();
591         assertEquals( Action.ERROR, returnString );
592
593         archivaConfigControl.verify();
594         repoFactoryControl.verify();
595         auditLogsDaoControl.verify();
596
597         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
598         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
599
600         verifyChecksums( repoLocation );
601     }
602     
603     public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
604         throws Exception
605     {                
606         setUploadParameters( "1.0", null,
607                              new File( getBasedir(),
608                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
609                              true );
610     
611         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
612         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
613         repoConfig.setBlockRedeployments( false );        
614         content.setRepository( repoConfig );        
615     
616         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
617         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
618         auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null, 2 );
619     
620         archivaConfigControl.replay();
621         repoFactoryControl.replay();
622         auditLogsDaoControl.replay();
623     
624         String returnString = uploadAction.doUpload();
625         assertEquals( Action.SUCCESS, returnString );
626     
627         setUploadParameters( "1.0", null,
628                              new File( getBasedir(),
629                                        "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
630                              true );
631         
632         returnString = uploadAction.doUpload();
633         assertEquals( Action.SUCCESS, returnString );
634         
635         archivaConfigControl.verify();
636         repoFactoryControl.verify();
637         auditLogsDaoControl.verify();
638     
639         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
640         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
641     
642         verifyChecksums( repoLocation );
643     }
644 }