1 package org.apache.archiva.consumers.core;
2
3 import org.apache.archiva.admin.model.beans.ManagedRepository;
4 import org.apache.archiva.checksum.ChecksumAlgorithm;
5 import org.apache.archiva.checksum.ChecksummedFile;
6 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
7 import org.apache.commons.io.FileUtils;
8 import org.assertj.core.api.Assertions;
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import java.io.File;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.util.Calendar;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ArtifactMissingChecksumsConsumerTest
38 extends AbstractArtifactConsumerTest
39 {
40 private ManagedRepository repoConfig;
41
42 @Before
43 @Override
44 public void setUp()
45 throws Exception
46 {
47 super.setUp();
48
49 repoConfig = new ManagedRepository();
50 repoConfig.setId( "test-repo" );
51 repoConfig.setName( "Test Repository" );
52 repoConfig.setLayout( "default" );
53 repoConfig.setLocation( new File( "target/test-classes/test-repo/" ).getPath() );
54
55 consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
56 KnownRepositoryContentConsumer.class );
57 }
58
59 @Test
60 public void testNoExistingChecksums()
61 throws Exception
62 {
63 String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
64
65 Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
66 Path md5FilePath = Paths.get( repoConfig.getLocation(), path + ".md5" );
67
68 Files.deleteIfExists( sha1Path );
69 Files.deleteIfExists( md5FilePath );
70
71 Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
72 Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
73
74 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
75
76 consumer.processFile( path );
77
78 Assertions.assertThat( sha1Path.toFile() ).exists();
79 long sha1LastModified = sha1Path.toFile().lastModified();
80 Assertions.assertThat( md5FilePath.toFile() ).exists();
81 long md5LastModified = md5FilePath.toFile().lastModified();
82 Thread.sleep( 1000 );
83 consumer.processFile( path );
84
85 Assertions.assertThat( sha1Path.toFile() ).exists();
86 Assertions.assertThat( md5FilePath.toFile() ).exists();
87
88 Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
89
90 Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
91 }
92
93 @Test
94 public void testExistingIncorrectChecksums()
95 throws Exception
96 {
97 File newLocation = new File( "target/test-repo" );
98 FileUtils.deleteDirectory( newLocation );
99 FileUtils.copyDirectory( new File( repoConfig.getLocation() ), newLocation );
100 repoConfig.setLocation( newLocation.getAbsolutePath() );
101
102 String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
103
104 Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
105
106 Path md5Path = Paths.get( repoConfig.getLocation(), path + ".md5" );
107
108 ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
109
110 Assertions.assertThat( sha1Path.toFile() ).exists();
111 Assertions.assertThat( md5Path.toFile() ).exists();
112 Assertions.assertThat(
113 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) )
114 .isFalse();
115
116 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
117
118 consumer.processFile( path );
119
120 Assertions.assertThat( sha1Path.toFile() ).exists();
121 Assertions.assertThat( md5Path.toFile() ).exists();
122 Assertions.assertThat(
123 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) )
124 .isTrue();
125 }
126 }