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