]> source.dussan.org Git - archiva.git/blob
27ad9680b471d7e8ae4fe7671fed343d5582e9b3
[archiva.git] /
1 package org.apache.archiva.consumers.core;
2
3 import org.apache.archiva.checksum.ChecksumAlgorithm;
4 import org.apache.archiva.checksum.ChecksummedFile;
5 import org.apache.archiva.common.utils.PathUtil;
6 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
7 import org.apache.archiva.repository.base.BasicManagedRepository;
8 import org.apache.archiva.repository.EditableManagedRepository;
9 import org.apache.commons.io.FileUtils;
10 import org.assertj.core.api.Assertions;
11 import org.junit.Before;
12 import org.junit.Test;
13
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.Arrays;
18 import java.util.Calendar;
19
20 /*
21  * Licensed to the Apache Software Foundation (ASF) under one
22  * or more contributor license agreements.  See the NOTICE file
23  * distributed with this work for additional information
24  * regarding copyright ownership.  The ASF licenses this file
25  * to you under the Apache License, Version 2.0 (the
26  * "License"); you may not use this file except in compliance
27  * with the License.  You may obtain a copy of the License at
28  *
29  *   http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing,
32  * software distributed under the License is distributed on an
33  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
34  * KIND, either express or implied.  See the License for the
35  * specific language governing permissions and limitations
36  * under the License.
37  */
38
39 public class ArtifactMissingChecksumsConsumerTest
40     extends AbstractArtifactConsumerTest
41 {
42     private EditableManagedRepository repoConfig;
43
44     @Before
45     @Override
46     public void setUp()
47         throws Exception
48     {
49         super.setUp();
50
51         Path basePath = Paths.get("target/test-classes");
52         repoConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", basePath.resolve("test-repo"));
53         repoConfig.setLayout( "default" );
54         repoConfig.setLocation(basePath.resolve("test-repo/" ).toUri() );
55
56         consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
57                                                KnownRepositoryContentConsumer.class );
58     }
59
60     @Test
61     public void testNoExistingChecksums()
62         throws Exception
63     {
64         String path = "no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
65
66         Path basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
67         Path sha1Path = basePath.resolve(path + ".sha1" );
68         Path md5FilePath = basePath.resolve(path + ".md5" );
69
70         Files.deleteIfExists( sha1Path );
71         Files.deleteIfExists( md5FilePath );
72
73         Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
74         Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
75
76         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
77
78         consumer.processFile( path );
79
80         Assertions.assertThat( sha1Path.toFile() ).exists();
81         long sha1LastModified = sha1Path.toFile().lastModified();
82         Assertions.assertThat( md5FilePath.toFile() ).exists();
83         long md5LastModified = md5FilePath.toFile().lastModified();
84         Thread.sleep( 1000 );
85         consumer.processFile( path );
86
87         Assertions.assertThat( sha1Path.toFile() ).exists();
88         Assertions.assertThat( md5FilePath.toFile() ).exists();
89
90         Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
91
92         Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
93     }
94
95     @Test
96     public void testExistingIncorrectChecksums()
97         throws Exception
98     {
99         Path newLocation = Paths.get( "target/test-repo" );
100         org.apache.archiva.common.utils.FileUtils.deleteDirectory( newLocation );
101         FileUtils.copyDirectory( Paths.get(repoConfig.getLocation() ).toFile(), newLocation.toFile() );
102         repoConfig.setLocation( newLocation.toAbsolutePath().toUri() );
103         Path basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
104
105         String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
106
107         Path sha1Path = basePath.resolve( path + ".sha1" );
108
109         Path md5Path = basePath.resolve( path + ".md5" );
110
111         ChecksummedFile checksum = new ChecksummedFile( basePath.resolve( path ) );
112
113         Assertions.assertThat( sha1Path.toFile() ).exists();
114         Assertions.assertThat( md5Path.toFile() ).exists();
115         Assertions.assertThat(
116             checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 ) ) ) //
117             .isFalse();
118
119         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
120
121         consumer.processFile( path );
122
123         Assertions.assertThat( sha1Path.toFile() ).exists();
124         Assertions.assertThat( md5Path.toFile() ).exists();
125         Assertions.assertThat(
126             checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 )) ) //
127             .isTrue();
128     }
129 }