]> source.dussan.org Git - archiva.git/blob
7f25503b78e047e14413d71ad40127346fe976be
[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         assertEquals( 6, new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list().length );
530
531         assertTrue(
532             new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
533         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
534             ".sha1" ).exists() );
535         assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
536             ".md5" ).exists() );
537     }
538
539     public void testChecksumIsCorrectWhenArtifactIsReUploaded()
540         throws Exception
541     {
542         setUploadParameters( "1.0", null, new File( getBasedir(),
543                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
544                              null, true );
545
546         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
547         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
548         repoConfig.setBlockRedeployments( false );
549         content.setRepository( repoConfig );
550
551         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
552         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
553
554         archivaConfigControl.replay();
555         repoFactoryControl.replay();
556
557         String returnString = uploadAction.doUpload();
558         assertEquals( Action.SUCCESS, returnString );
559
560         archivaConfigControl.verify();
561         repoFactoryControl.verify();
562
563         archivaConfigControl.reset();
564         repoFactoryControl.reset();
565
566         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
567         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
568
569         verifyChecksums( repoLocation );
570
571         // RE-upload artifact
572         setUploadParameters( "1.0", null, new File( getBasedir(),
573                                                     "target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
574                              null, true );
575
576         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
577         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
578
579         archivaConfigControl.replay();
580         repoFactoryControl.replay();
581
582         // TODO: track modifications?
583 //        MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
584         MockControl control = mockAuditLogs(
585             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
586                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
587
588         returnString = uploadAction.doUpload();
589         assertEquals( Action.SUCCESS, returnString );
590
591         archivaConfigControl.verify();
592         repoFactoryControl.verify();
593         control.verify();
594
595         repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
596         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
597
598         verifyChecksums( repoLocation );
599     }
600
601     public void testUploadArtifactAlreadyExistingRedeploymentsBlocked()
602         throws Exception
603     {
604         setUploadParameters( "1.0", null, new File( getBasedir(),
605                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
606                              null, true );
607
608         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
609         content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
610
611         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
612         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
613
614         archivaConfigControl.replay();
615         repoFactoryControl.replay();
616
617         String returnString = uploadAction.doUpload();
618         assertEquals( Action.SUCCESS, returnString );
619
620         setUploadParameters( "1.0", null, new File( getBasedir(),
621                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
622                              null, true );
623
624         MockControl control = mockAuditLogs( Collections.<String>emptyList() );
625
626         returnString = uploadAction.doUpload();
627         assertEquals( Action.ERROR, returnString );
628
629         archivaConfigControl.verify();
630         repoFactoryControl.verify();
631         control.verify();
632
633         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
634         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
635
636         verifyChecksums( repoLocation );
637     }
638
639     public void testUploadArtifactAlreadyExistingRedeploymentsAllowed()
640         throws Exception
641     {
642         setUploadParameters( "1.0", null, new File( getBasedir(),
643                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
644                              null, true );
645
646         ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
647         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID );
648         repoConfig.setBlockRedeployments( false );
649         content.setRepository( repoConfig );
650
651         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
652         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 );
653
654         archivaConfigControl.replay();
655         repoFactoryControl.replay();
656
657         String returnString = uploadAction.doUpload();
658         assertEquals( Action.SUCCESS, returnString );
659
660         setUploadParameters( "1.0", null, new File( getBasedir(),
661                                                     "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
662                              null, true );
663
664         // TODO: track modifications?
665 //        MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
666         MockControl control = mockAuditLogs(
667             Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
668                            "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
669
670         returnString = uploadAction.doUpload();
671         assertEquals( Action.SUCCESS, returnString );
672
673         archivaConfigControl.verify();
674         repoFactoryControl.verify();
675         control.verify();
676
677         String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
678         assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
679
680         verifyChecksums( repoLocation );
681     }
682
683 }