1 package org.apache.archiva.reports.consumers;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.MetadataFacet;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
26 import org.apache.archiva.reports.RepositoryProblemFacet;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.consumers.ConsumerException;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Matchers;
34 import java.io.FileNotFoundException;
35 import java.util.Arrays;
36 import java.util.Date;
38 import static org.mockito.Mockito.*;
40 @SuppressWarnings( {"ThrowableInstanceNeverThrown"} )
41 public class DuplicateArtifactsConsumerTest
42 extends PlexusInSpringTestCase
44 private DuplicateArtifactsConsumer consumer;
46 private ManagedRepositoryConfiguration config;
48 private MetadataRepository metadataRepository;
50 private static final String TEST_REPO = "test-repo";
52 private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
54 private static final String TEST_PROJECT = "test-artifact";
56 private static final String TEST_NAMESPACE = "com.example.test";
58 private static final String TEST_FILE =
59 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
61 private static final String TEST_VERSION = "1.0-20100308.230825-1";
63 private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
65 private RepositoryPathTranslator pathTranslator;
72 consumer = (DuplicateArtifactsConsumer) lookup( KnownRepositoryContentConsumer.class, "duplicate-artifacts" );
73 assertNotNull( consumer );
75 config = new ManagedRepositoryConfiguration();
76 config.setId( TEST_REPO );
77 config.setLocation( getTestFile( "target/test-repository" ).getAbsolutePath() );
79 metadataRepository = (MetadataRepository) lookup( MetadataRepository.class );
81 pathTranslator = (RepositoryPathTranslator) lookup( RepositoryPathTranslator.class, "maven2" );
82 when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
85 public void testConsumerArtifactNotDuplicated()
86 throws ConsumerException
88 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
91 consumer.beginScan( config, new Date() );
92 consumer.processFile( TEST_FILE );
93 consumer.completeScan();
95 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
98 // TODO: Doesn't currently work
99 // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
100 // throws ConsumerException
102 // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
103 // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
105 // consumer.beginScan( config, new Date() );
106 // consumer.processFile( TEST_FILE );
107 // consumer.completeScan();
109 // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
112 public void testConsumerArtifactDuplicated()
113 throws ConsumerException
115 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
116 TEST_METADATA, createMetadata( "1.0" ) ) );
118 consumer.beginScan( config, new Date() );
119 consumer.processFile( TEST_FILE );
120 consumer.completeScan();
122 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
123 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
124 RepositoryProblemFacet problem = argument.getValue();
125 assertProblem( problem );
128 public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
129 throws ConsumerException
131 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
132 createMetadata( "1.0" ) ) );
134 consumer.beginScan( config, new Date() );
135 consumer.processFile( TEST_FILE );
136 consumer.completeScan();
138 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
139 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
140 RepositoryProblemFacet problem = argument.getValue();
141 assertProblem( problem );
144 public void testConsumerArtifactFileNotExist()
145 throws ConsumerException
147 consumer.beginScan( config, new Date() );
150 consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
151 fail( "Should have failed to find file" );
153 catch ( ConsumerException e )
155 assertTrue( e.getCause() instanceof FileNotFoundException );
159 consumer.completeScan();
162 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
165 public void testConsumerArtifactNotAnArtifactPathNoResults()
166 throws ConsumerException
168 consumer.beginScan( config, new Date() );
169 // No exception unnecessarily for something we can't report on
170 consumer.processFile( "com/example/invalid-artifact.txt" );
171 consumer.completeScan();
173 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
176 public void testConsumerArtifactNotAnArtifactPathResults()
177 throws ConsumerException
179 when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn( Arrays.asList(
180 TEST_METADATA, createMetadata( "1.0" ) ) );
182 // override, this feels a little overspecified though
183 when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
184 new IllegalArgumentException() );
186 consumer.beginScan( config, new Date() );
187 // No exception unnecessarily for something we can't report on
188 consumer.processFile( "com/example/invalid-artifact.txt" );
189 consumer.completeScan();
191 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
194 private static void assertProblem( RepositoryProblemFacet problem )
196 assertEquals( TEST_REPO, problem.getRepositoryId() );
197 assertEquals( TEST_NAMESPACE, problem.getNamespace() );
198 assertEquals( TEST_PROJECT, problem.getProject() );
199 assertEquals( TEST_VERSION, problem.getVersion() );
200 assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
201 assertNotNull( problem.getMessage() );
202 assertEquals( "duplicate-artifact", problem.getProblem() );
205 private static ArtifactMetadata createMetadata( String version )
207 ArtifactMetadata artifact = new ArtifactMetadata();
208 artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
209 artifact.setNamespace( TEST_NAMESPACE );
210 artifact.setProject( TEST_PROJECT );
211 artifact.setVersion( version );
212 artifact.setRepositoryId( TEST_REPO );