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