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.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.Calendar;
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
28 * http://www.apache.org/licenses/LICENSE-2.0
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
38 public class ArtifactMissingChecksumsConsumerTest
39 extends AbstractArtifactConsumerTest
41 private EditableManagedRepository repoConfig;
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() );
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 basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
66 Path sha1Path = basePath.resolve(path + ".sha1" );
67 Path md5FilePath = basePath.resolve(path + ".md5" );
69 Files.deleteIfExists( sha1Path );
70 Files.deleteIfExists( md5FilePath );
72 Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
73 Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
75 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
77 consumer.processFile( path );
79 Assertions.assertThat( sha1Path.toFile() ).exists();
80 long sha1LastModified = sha1Path.toFile().lastModified();
81 Assertions.assertThat( md5FilePath.toFile() ).exists();
82 long md5LastModified = md5FilePath.toFile().lastModified();
84 consumer.processFile( path );
86 Assertions.assertThat( sha1Path.toFile() ).exists();
87 Assertions.assertThat( md5FilePath.toFile() ).exists();
89 Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
91 Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
95 public void testExistingIncorrectChecksums()
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() );
104 String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
106 Path sha1Path = basePath.resolve( path + ".sha1" );
108 Path md5Path = basePath.resolve( path + ".md5" );
110 ChecksummedFile checksum = new ChecksummedFile( basePath.resolve( path ) );
112 Assertions.assertThat( sha1Path.toFile() ).exists();
113 Assertions.assertThat( md5Path.toFile() ).exists();
114 Assertions.assertThat(
115 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
118 consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
120 consumer.processFile( path );
122 Assertions.assertThat( sha1Path.toFile() ).exists();
123 Assertions.assertThat( md5Path.toFile() ).exists();
124 Assertions.assertThat(
125 checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //