1 package org.apache.archiva.consumers.core;
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;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.util.Calendar;
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
27 * http://www.apache.org/licenses/LICENSE-2.0
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
37 public class ArtifactMissingChecksumsConsumerTest
38 extends AbstractArtifactConsumerTest
40 private ManagedRepository repoConfig;
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() );
55 consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
56 KnownRepositoryContentConsumer.class );
60 public void testNoExistingChecksums()
63 String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
65 Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
66 Path md5FilePath = Paths.get( repoConfig.getLocation(), path + ".md5" );
68 Files.deleteIfExists( sha1Path );
69 Files.deleteIfExists( md5FilePath );
71 Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
72 Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
74 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
76 consumer.processFile( path );
78 Assertions.assertThat( sha1Path.toFile() ).exists();
79 long sha1LastModified = sha1Path.toFile().lastModified();
80 Assertions.assertThat( md5FilePath.toFile() ).exists();
81 long md5LastModified = md5FilePath.toFile().lastModified();
83 consumer.processFile( path );
85 Assertions.assertThat( sha1Path.toFile() ).exists();
86 Assertions.assertThat( md5FilePath.toFile() ).exists();
88 Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
90 Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
94 public void testExistingIncorrectChecksums()
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() );
102 String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
104 Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
106 Path md5Path = Paths.get( repoConfig.getLocation(), path + ".md5" );
108 ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
110 Assertions.assertThat( sha1Path.toFile() ).exists();
111 Assertions.assertThat( md5Path.toFile() ).exists();
112 Assertions.assertThat(
113 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
116 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
118 consumer.processFile( path );
120 Assertions.assertThat( sha1Path.toFile() ).exists();
121 Assertions.assertThat( md5Path.toFile() ).exists();
122 Assertions.assertThat(
123 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //