]> source.dussan.org Git - archiva.git/blob
7f052b6bee8f8d7a80168f08a183513ed4ac5a84
[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.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;
31
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.MockControl;
51 import org.easymock.classextension.MockClassControl;
52
53 /**
54  * UploadActionTest
55  */
56 public class UploadActionTest
57     extends PlexusInSpringTestCase
58 {
59     private UploadAction uploadAction;
60
61     private ArchivaConfiguration archivaConfig;
62
63     private MockControl archivaConfigControl;
64
65     private RepositoryContentFactory repoFactory;
66
67     private MockControl repoFactoryControl;
68
69     private static final String REPOSITORY_ID = "test-repo";
70
71     private Configuration config;
72
73     public void setUp()
74         throws Exception
75     {
76         super.setUp();
77
78         MockControl schedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
79         ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) schedulerControl.getMock();
80
81         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
82         archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
83
84         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
85         repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
86
87         uploadAction = new UploadAction();
88         uploadAction.setScheduler( scheduler );
89         uploadAction.setConfiguration( archivaConfig );
90         uploadAction.setRepositoryFactory( repoFactory );
91
92         File testRepo = new File( getBasedir(), "target/test-classes/test-repo" );
93         testRepo.mkdirs();
94
95         assertTrue( testRepo.exists() );
96
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 );
105
106         RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
107         repoScanning.setKnownContentConsumers( new ArrayList<String>() );
108         config.setRepositoryScanning( repoScanning );
109     }
110
111     public void tearDown()
112         throws Exception
113     {
114         File testRepo = new File( config.findManagedRepositoryById( REPOSITORY_ID ).getLocation() );
115         FileUtils.deleteDirectory( testRepo );
116
117         assertFalse( testRepo.exists() );
118
119         super.tearDown();
120     }
121
122     private void setUploadParameters( String version, String classifier, File artifact, File pomFile,
123                                       boolean generatePom )
124     {
125         uploadAction.setRepositoryId( REPOSITORY_ID );
126         uploadAction.setGroupId( "org.apache.archiva" );
127         uploadAction.setArtifactId( "artifact-upload" );
128         uploadAction.setVersion( version );
129         uploadAction.setPackaging( "jar" );
130
131         uploadAction.setClassifier( classifier );
132         uploadAction.setArtifact( artifact );
133         uploadAction.setPom( pomFile );
134         uploadAction.setGeneratePom( generatePom );
135     }
136
137     private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation )
138     {
139         assertTrue(
140             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
141         assertTrue(
142             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
143         assertTrue(
144             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
145
146         assertTrue(
147             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
148         assertTrue(
149             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
150         assertTrue(
151             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
152
153         assertTrue(
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 +
158             ".md5" ).exists() );
159     }
160
161     private void verifyChecksums( String repoLocation )
162         throws IOException
163     {
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 );
169
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 ) );
173
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 ) );
177
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 );
183
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 ) );
187
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 ) );
191
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 );
197
198         contents = FileUtils.readFileToString(
199             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
200         assertTrue( StringUtils.contains( contents, sha1 ) );
201
202         contents = FileUtils.readFileToString(
203             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
204         assertTrue( StringUtils.contains( contents, md5 ) );
205     }
206
207     public void testArtifactUploadWithPomSuccessful()
208         throws Exception
209     {
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 );
213
214         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
215         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
216
217         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
218         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
219
220         archivaConfigControl.replay();
221         repoFactoryControl.replay();
222
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" ) );
226
227         String returnString = uploadAction.doUpload();
228         assertEquals( Action.SUCCESS, returnString );
229
230         archivaConfigControl.verify();
231         repoFactoryControl.verify();
232         control.verify();
233
234         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
235         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
236
237         verifyChecksums( repoLocation );
238     }
239
240     private MockControl mockAuditLogs( List<String> resources )
241     {
242         return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
243     }
244
245     private MockControl mockAuditLogs( String action, List<String> resources )
246     {
247         MockControl control = MockControl.createControl( AuditListener.class );
248         AuditListener listener = (AuditListener) control.getMock();
249         boolean matcherSet = false;
250         for ( String resource : resources )
251         {
252             listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
253             if ( !matcherSet )
254             {
255                 control.setMatcher( new AuditEventArgumentsMatcher() );
256                 matcherSet = true;
257             }
258         }
259         control.replay();
260
261         uploadAction.setAuditListeners( Collections.singletonList( listener ) );
262         return control;
263     }
264
265     public void testArtifactUploadWithClassifier()
266         throws Exception
267     {
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 );
271
272         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
273         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
274
275         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
276         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
277
278         archivaConfigControl.replay();
279         repoFactoryControl.replay();
280
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" ) );
284
285         String returnString = uploadAction.doUpload();
286         assertEquals( Action.SUCCESS, returnString );
287
288         archivaConfigControl.verify();
289         repoFactoryControl.verify();
290         control.verify();
291
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() );
299
300         assertTrue(
301             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
302         assertTrue(
303             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
304         assertTrue(
305             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
306
307         assertTrue(
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 +
312             ".md5" ).exists() );
313
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 );
319
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 ) );
323
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 ) );
327
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 );
333
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 ) );
337
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 ) );
341
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 );
347
348         contents = FileUtils.readFileToString(
349             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
350         assertTrue( StringUtils.contains( contents, sha1 ) );
351
352         contents = FileUtils.readFileToString(
353             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
354         assertTrue( StringUtils.contains( contents, md5 ) );
355     }
356
357     public void testArtifactUploadGeneratePomSuccessful()
358         throws Exception
359     {
360         setUploadParameters( "1.0", null, new File( getBasedir(),
361                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
362                              null, true );
363
364         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
365         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
366
367         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
368         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
369
370         archivaConfigControl.replay();
371         repoFactoryControl.replay();
372
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" ) );
376
377         String returnString = uploadAction.doUpload();
378         assertEquals( Action.SUCCESS, returnString );
379
380         archivaConfigControl.verify();
381         repoFactoryControl.verify();
382         control.verify();
383
384         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
385         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
386
387         verifyChecksums( repoLocation );
388     }
389
390     public void testArtifactUploadNoPomSuccessful()
391         throws Exception
392     {
393         setUploadParameters( "1.0", null, new File( getBasedir(),
394                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
395                              null, false );
396
397         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
398         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
399
400         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
401         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
402
403         archivaConfigControl.replay();
404         repoFactoryControl.replay();
405
406         MockControl control =
407             mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
408
409         String returnString = uploadAction.doUpload();
410         assertEquals( Action.SUCCESS, returnString );
411
412         archivaConfigControl.verify();
413         repoFactoryControl.verify();
414         control.verify();
415
416         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
417         assertTrue(
418             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
419         assertTrue(
420             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
421         assertTrue(
422             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
423
424         assertFalse(
425             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
426         assertFalse(
427             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
428         assertFalse(
429             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
430
431         assertTrue(
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 +
436             ".md5" ).exists() );
437
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 );
443
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 ) );
447
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 ) );
451
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 );
457
458         contents = FileUtils.readFileToString(
459             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
460         assertTrue( StringUtils.contains( contents, sha1 ) );
461
462         contents = FileUtils.readFileToString(
463             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
464         assertTrue( StringUtils.contains( contents, md5 ) );
465     }
466
467     public void testArtifactUploadFailedRepositoryNotFound()
468         throws Exception
469     {
470         setUploadParameters( "1.0", null, new File( getBasedir(),
471                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
472                              null, false );
473
474         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
475         repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
476                                            new RepositoryNotFoundException() );
477
478         archivaConfigControl.replay();
479         repoFactoryControl.replay();
480
481         String returnString = uploadAction.doUpload();
482         assertEquals( Action.ERROR, returnString );
483
484         archivaConfigControl.verify();
485         repoFactoryControl.verify();
486
487         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
488         assertFalse(
489             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
490
491         assertFalse(
492             new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
493
494         assertFalse(
495             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
496     }
497
498     public void testArtifactUploadSnapshots()
499         throws Exception
500     {
501         setUploadParameters( "1.0-SNAPSHOT", null, new File( getBasedir(),
502                                                              "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
503                              null, true );
504
505         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
506         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
507
508         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
509         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
510
511         archivaConfigControl.replay();
512         repoFactoryControl.replay();
513
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" ) );
520
521         String returnString = uploadAction.doUpload();
522         assertEquals( Action.SUCCESS, returnString );
523
524         archivaConfigControl.verify();
525         repoFactoryControl.verify();
526         control.verify();
527
528         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
529         String[] artifactsList = new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list();
530         
531         assertEquals( 6, artifactsList.length );
532
533         assertTrue(
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 +
538             ".md5" ).exists() );
539         
540         int startIndex = "artifact-upload-1.0-".length();
541         int endIndex = -1; 
542
543         if ( artifactsList[0].contains( "jar" ) )
544         {
545             endIndex = artifactsList[0].indexOf( ".jar" );
546         }
547         else
548         {
549             endIndex = artifactsList[0].indexOf( ".pom" );
550         }
551
552         String actualTimestamp = artifactsList[0].substring( startIndex, endIndex );
553
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()  );
560
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 );
567
568         String contents =
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 ) );
572
573         contents =
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 ) );
577
578         // verify checksums of pom file
579         checksum =
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 );
584
585         contents =
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 ) );
589
590         contents =
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 ) );
594
595         
596         // verify checksums of metadata file
597         checksum =
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 );
602
603         contents =
604             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
605                 MetadataTools.MAVEN_METADATA + ".sha1" ) );
606         assertTrue( StringUtils.contains( contents, sha1 ) );
607
608         contents =
609             FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
610                 MetadataTools.MAVEN_METADATA + ".md5" ) );
611         assertTrue( StringUtils.contains( contents, md5 ) );
612     }
613
614     public void testChecksumIsCorrectWhenArtifactIsReUploaded()
615         throws Exception
616     {
617         setUploadParameters( "1.0", null, new File( getBasedir(),
618                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
619                              null, true );
620
621         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
622         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
623         repoConfig.setBlockRedeployments( false );
624         content.setRepository( repoConfig );
625
626         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
627         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
628
629         archivaConfigControl.replay();
630         repoFactoryControl.replay();
631
632         String returnString = uploadAction.doUpload();
633         assertEquals( Action.SUCCESS, returnString );
634
635         archivaConfigControl.verify();
636         repoFactoryControl.verify();
637
638         archivaConfigControl.reset();
639         repoFactoryControl.reset();
640
641         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
642         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
643
644         verifyChecksums( repoLocation );
645
646         // RE-upload artifact
647         setUploadParameters( "1.0", null, new File( getBasedir(),
648                                                     "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
649                              null, true );
650
651         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
652         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
653
654         archivaConfigControl.replay();
655         repoFactoryControl.replay();
656
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" ) );
662
663         returnString = uploadAction.doUpload();
664         assertEquals( Action.SUCCESS, returnString );
665
666         archivaConfigControl.verify();
667         repoFactoryControl.verify();
668         control.verify();
669
670         repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
671         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
672
673         verifyChecksums( repoLocation );
674     }
675
676     public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
677         throws Exception
678     {
679         setUploadParameters( "1.0", null, new File( getBasedir(),
680                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
681                              null, true );
682
683         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
684         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
685
686         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
687         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
688
689         archivaConfigControl.replay();
690         repoFactoryControl.replay();
691
692         String returnString = uploadAction.doUpload();
693         assertEquals( Action.SUCCESS, returnString );
694
695         setUploadParameters( "1.0", null, new File( getBasedir(),
696                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
697                              null, true );
698
699         MockControl control = mockAuditLogs( Collections.<String>emptyList() );
700
701         returnString = uploadAction.doUpload();
702         assertEquals( Action.ERROR, returnString );
703
704         archivaConfigControl.verify();
705         repoFactoryControl.verify();
706         control.verify();
707
708         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
709         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
710
711         verifyChecksums( repoLocation );
712     }
713
714     public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
715         throws Exception
716     {
717         setUploadParameters( "1.0", null, new File( getBasedir(),
718                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
719                              null, true );
720
721         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
722         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
723         repoConfig.setBlockRedeployments( false );
724         content.setRepository( repoConfig );
725
726         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
727         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
728
729         archivaConfigControl.replay();
730         repoFactoryControl.replay();
731
732         String returnString = uploadAction.doUpload();
733         assertEquals( Action.SUCCESS, returnString );
734
735         setUploadParameters( "1.0", null, new File( getBasedir(),
736                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
737                              null, true );
738
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" ) );
744
745         returnString = uploadAction.doUpload();
746         assertEquals( Action.SUCCESS, returnString );
747
748         archivaConfigControl.verify();
749         repoFactoryControl.verify();
750         control.verify();
751
752         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
753         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
754
755         verifyChecksums( repoLocation );
756     }
757
758 }