1 package org.apache.maven.archiva.consumers.core;
2
3 import java.io.File;
4 import java.util.Calendar;
5
6 import org.apache.archiva.checksum.ChecksumAlgorithm;
7 import org.apache.archiva.checksum.ChecksummedFile;
8 import org.apache.commons.io.FileUtils;
9 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class ArtifactMissingChecksumsConsumerTest
31 extends AbstractArtifactConsumerTest
32 {
33 private ManagedRepositoryConfiguration repoConfig;
34
35 @Override
36 protected void setUp()
37 throws Exception
38 {
39 super.setUp();
40
41 repoConfig = new ManagedRepositoryConfiguration();
42 repoConfig.setId( "test-repo" );
43 repoConfig.setName( "Test Repository" );
44 repoConfig.setLayout( "default" );
45 repoConfig.setLocation( new File( getBasedir(), "target/test-classes/test-repo/" ).getPath() );
46
47 consumer = (ArtifactMissingChecksumsConsumer) lookup( "artifactMissingChecksumsConsumer" );
48 }
49
50 public void testNoExistingChecksums()
51 throws Exception
52 {
53 String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
54
55 File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
56 File md5File = new File( repoConfig.getLocation(), path + ".md5" );
57
58 sha1File.delete();
59 md5File.delete();
60
61 assertFalse( sha1File.exists() );
62 assertFalse( md5File.exists() );
63
64 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
65
66 consumer.processFile( path );
67
68 assertTrue( sha1File.exists() );
69 assertTrue( md5File.exists() );
70 }
71
72 public void testExistingIncorrectChecksums()
73 throws Exception
74 {
75 File newLocation = getTestFile( "target/test-repo" );
76 FileUtils.deleteDirectory( newLocation );
77 FileUtils.copyDirectory( new File( repoConfig.getLocation() ), newLocation );
78 repoConfig.setLocation( newLocation.getAbsolutePath() );
79
80 String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
81
82 File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
83 File md5File = new File( repoConfig.getLocation(), path + ".md5" );
84
85 ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
86
87 assertTrue( sha1File.exists() );
88 assertTrue( md5File.exists() );
89 assertFalse( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
90
91 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
92
93 consumer.processFile( path );
94
95 assertTrue( sha1File.exists() );
96 assertTrue( md5File.exists() );
97 assertTrue( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
98 }
99 }