]> source.dussan.org Git - archiva.git/blob
8645ab01ddbf8ecc592385c7fc115a5824bd6ee1
[archiva.git] /
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  * Licensed to the Apache Software Foundation (ASF) under one
20  * or more contributor license agreements.  See the NOTICE file
21  * distributed with this work for additional information
22  * regarding copyright ownership.  The ASF licenses this file
23  * to you under the Apache License, Version 2.0 (the
24  * "License"); you may not use this file except in compliance
25  * with the License.  You may obtain a copy of the License at
26  *
27  *   http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing,
30  * software distributed under the License is distributed on an
31  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32  * KIND, either express or implied.  See the License for the
33  * specific language governing permissions and limitations
34  * under the License.
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(),
66                                    path + ".sha1" );// new File( repoConfig.getLocation(), path + ".sha1" );
67         Path md5FilePath =
68             Paths.get( repoConfig.getLocation(), path + ".md5" );// new File( repoConfig.getLocation(), path + ".md5" );
69
70         Files.deleteIfExists( sha1Path );
71         Files.deleteIfExists( md5FilePath );
72
73         //sha1File.delete();
74         //md5File.delete();
75
76         Assertions.assertThat( sha1Path.toFile() ).doesNotExist();// assertFalse( sha1File.exists() );
77         Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();// assertFalse( md5File.exists() );
78
79         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
80
81         consumer.processFile( path );
82
83         Assertions.assertThat( sha1Path.toFile() ).exists();// assertTrue( sha1File.exists() );
84         Assertions.assertThat( md5FilePath.toFile() ).exists();//assertTrue( md5File.exists() );
85     }
86
87     @Test
88     public void testExistingIncorrectChecksums()
89         throws Exception
90     {
91         File newLocation = new File( "target/test-repo" );
92         FileUtils.deleteDirectory( newLocation );
93         FileUtils.copyDirectory( new File( repoConfig.getLocation() ), newLocation );
94         repoConfig.setLocation( newLocation.getAbsolutePath() );
95
96         String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
97
98         // new File( repoConfig.getLocation(), path + ".sha1" );
99         Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
100
101         //new File( repoConfig.getLocation(), path + ".md5" );
102         Path md5Path = Paths.get( repoConfig.getLocation(), path + ".md5" );
103
104         ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
105
106         Assertions.assertThat( sha1Path.toFile() ).exists();
107         Assertions.assertThat( md5Path.toFile() ).exists();
108         Assertions.assertThat(
109             checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
110             .isFalse();
111
112         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
113
114         consumer.processFile( path );
115
116         Assertions.assertThat( sha1Path.toFile() ).exists();
117         Assertions.assertThat( md5Path.toFile() ).exists();
118         Assertions.assertThat(
119             checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
120             .isTrue();
121     }
122 }