1 package org.apache.archiva.consumers.core;
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;
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;
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
29 * http://www.apache.org/licenses/LICENSE-2.0
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
39 public class ArtifactMissingChecksumsConsumerTest
40 extends AbstractArtifactConsumerTest
42 private EditableManagedRepository repoConfig;
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() );
56 consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
57 KnownRepositoryContentConsumer.class );
61 public void testNoExistingChecksums()
64 String path = "no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
66 Path basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
67 Path sha1Path = basePath.resolve(path + ".sha1" );
68 Path md5FilePath = basePath.resolve(path + ".md5" );
70 Files.deleteIfExists( sha1Path );
71 Files.deleteIfExists( md5FilePath );
73 Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
74 Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
76 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
78 consumer.processFile( path );
80 Assertions.assertThat( sha1Path.toFile() ).exists();
81 long sha1LastModified = sha1Path.toFile().lastModified();
82 Assertions.assertThat( md5FilePath.toFile() ).exists();
83 long md5LastModified = md5FilePath.toFile().lastModified();
85 consumer.processFile( path );
87 Assertions.assertThat( sha1Path.toFile() ).exists();
88 Assertions.assertThat( md5FilePath.toFile() ).exists();
90 Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
92 Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
96 public void testExistingIncorrectChecksums()
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() );
105 String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
107 Path sha1Path = basePath.resolve( path + ".sha1" );
109 Path md5Path = basePath.resolve( path + ".md5" );
111 ChecksummedFile checksum = new ChecksummedFile( basePath.resolve( path ) );
113 Assertions.assertThat( sha1Path.toFile() ).exists();
114 Assertions.assertThat( md5Path.toFile() ).exists();
115 Assertions.assertThat(
116 checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 ) ) ) //
119 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
121 consumer.processFile( path );
123 Assertions.assertThat( sha1Path.toFile() ).exists();
124 Assertions.assertThat( md5Path.toFile() ).exists();
125 Assertions.assertThat(
126 checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 )) ) //