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