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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.model.MetadataFacet;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
30 import org.apache.archiva.reports.RepositoryProblemFacet;
31 import org.apache.archiva.consumers.ConsumerException;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Matchers;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.test.context.ContextConfiguration;
40 import javax.inject.Inject;
41 import javax.inject.Named;
43 import java.io.FileNotFoundException;
44 import java.util.Arrays;
45 import java.util.Date;
46 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
48 import static org.mockito.Mockito.*;
49 import org.springframework.test.annotation.DirtiesContext;
51 @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
52 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
54 @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
55 public class DuplicateArtifactsConsumerTest
59 @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
60 private DuplicateArtifactsConsumer consumer;
62 private ManagedRepository config;
64 private MetadataRepository metadataRepository;
66 private static final String TEST_REPO = "test-repo";
68 private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
70 private static final String TEST_PROJECT = "test-artifact";
72 private static final String TEST_NAMESPACE = "com.example.test";
74 private static final String TEST_FILE =
75 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
77 private static final String TEST_VERSION = "1.0-20100308.230825-1";
79 private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
82 @Named( value = "repositoryPathTranslator#maven2" )
83 private RepositoryPathTranslator pathTranslator;
86 ApplicationContext applicationContext;
96 assertNotNull( consumer );
98 config = new ManagedRepository();
99 config.setId( TEST_REPO );
100 config.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
102 metadataRepository = mock( MetadataRepository.class );
104 RepositorySession session = mock( RepositorySession.class );
105 when( session.getRepository() ).thenReturn( metadataRepository );
107 RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
108 //(RepositorySessionFactory) lookup( RepositorySessionFactory.class );
109 when( factory.createSession() ).thenReturn( session );
111 when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
115 public void testConsumerArtifactNotDuplicated()
118 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
119 Arrays.asList( TEST_METADATA ) );
121 consumer.beginScan( config, new Date() );
122 consumer.processFile( TEST_FILE );
123 consumer.completeScan();
125 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
128 // TODO: Doesn't currently work
129 // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
130 // throws ConsumerException
132 // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
133 // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
135 // consumer.beginScan( config, new Date() );
136 // consumer.processFile( TEST_FILE );
137 // consumer.completeScan();
139 // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
143 public void testConsumerArtifactDuplicated()
146 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
147 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
149 consumer.beginScan( config, new Date() );
150 consumer.processFile( TEST_FILE );
151 consumer.completeScan();
153 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
154 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
155 RepositoryProblemFacet problem = argument.getValue();
156 assertProblem( problem );
160 public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
163 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
164 Arrays.asList( createMetadata( "1.0" ) ) );
166 consumer.beginScan( config, new Date() );
167 consumer.processFile( TEST_FILE );
168 consumer.completeScan();
170 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
171 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
172 RepositoryProblemFacet problem = argument.getValue();
173 assertProblem( problem );
177 public void testConsumerArtifactFileNotExist()
180 consumer.beginScan( config, new Date() );
183 consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
184 fail( "Should have failed to find file" );
186 catch ( ConsumerException e )
188 assertTrue( e.getCause() instanceof FileNotFoundException );
192 consumer.completeScan();
195 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
199 public void testConsumerArtifactNotAnArtifactPathNoResults()
202 consumer.beginScan( config, new Date() );
203 // No exception unnecessarily for something we can't report on
204 consumer.processFile( "com/example/invalid-artifact.txt" );
205 consumer.completeScan();
207 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
211 public void testConsumerArtifactNotAnArtifactPathResults()
214 when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn(
215 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
217 // override, this feels a little overspecified though
218 when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
219 new IllegalArgumentException() );
221 consumer.beginScan( config, new Date() );
222 // No exception unnecessarily for something we can't report on
223 consumer.processFile( "com/example/invalid-artifact.txt" );
224 consumer.completeScan();
226 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
229 private static void assertProblem( RepositoryProblemFacet problem )
231 assertEquals( TEST_REPO, problem.getRepositoryId() );
232 assertEquals( TEST_NAMESPACE, problem.getNamespace() );
233 assertEquals( TEST_PROJECT, problem.getProject() );
234 assertEquals( TEST_VERSION, problem.getVersion() );
235 assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
236 assertNotNull( problem.getMessage() );
237 assertEquals( "duplicate-artifact", problem.getProblem() );
240 private static ArtifactMetadata createMetadata( String version )
242 ArtifactMetadata artifact = new ArtifactMetadata();
243 artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
244 artifact.setNamespace( TEST_NAMESPACE );
245 artifact.setProject( TEST_PROJECT );
246 artifact.setProjectVersion( version );
247 artifact.setVersion( version );
248 artifact.setRepositoryId( TEST_REPO );