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.nio.file.NoSuchFileException;
45 import java.util.Arrays;
46 import java.util.Date;
47 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
49 import static org.mockito.Mockito.*;
50 import org.springframework.test.annotation.DirtiesContext;
52 @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
53 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
55 @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
56 public class DuplicateArtifactsConsumerTest
60 @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
61 private DuplicateArtifactsConsumer consumer;
63 private ManagedRepository config;
65 private MetadataRepository metadataRepository;
67 private static final String TEST_REPO = "test-repo";
69 private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
71 private static final String TEST_PROJECT = "test-artifact";
73 private static final String TEST_NAMESPACE = "com.example.test";
75 private static final String TEST_FILE =
76 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
78 private static final String TEST_VERSION = "1.0-20100308.230825-1";
80 private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
83 @Named( value = "repositoryPathTranslator#maven2" )
84 private RepositoryPathTranslator pathTranslator;
87 ApplicationContext applicationContext;
97 assertNotNull( consumer );
99 config = new ManagedRepository();
100 config.setId( TEST_REPO );
101 config.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
103 metadataRepository = mock( MetadataRepository.class );
105 RepositorySession session = mock( RepositorySession.class );
106 when( session.getRepository() ).thenReturn( metadataRepository );
108 RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
109 //(RepositorySessionFactory) lookup( RepositorySessionFactory.class );
110 when( factory.createSession() ).thenReturn( session );
112 when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
116 public void testConsumerArtifactNotDuplicated()
119 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
120 Arrays.asList( TEST_METADATA ) );
122 consumer.beginScan( config, new Date() );
123 consumer.processFile( TEST_FILE );
124 consumer.completeScan();
126 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
129 // TODO: Doesn't currently work
130 // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
131 // throws ConsumerException
133 // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
134 // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
136 // consumer.beginScan( config, new Date() );
137 // consumer.processFile( TEST_FILE );
138 // consumer.completeScan();
140 // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
144 public void testConsumerArtifactDuplicated()
147 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
148 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
150 consumer.beginScan( config, new Date() );
151 consumer.processFile( TEST_FILE );
152 consumer.completeScan();
154 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
155 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
156 RepositoryProblemFacet problem = argument.getValue();
157 assertProblem( problem );
161 public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
164 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
165 Arrays.asList( createMetadata( "1.0" ) ) );
167 consumer.beginScan( config, new Date() );
168 consumer.processFile( TEST_FILE );
169 consumer.completeScan();
171 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
172 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
173 RepositoryProblemFacet problem = argument.getValue();
174 assertProblem( problem );
178 public void testConsumerArtifactFileNotExist()
181 consumer.beginScan( config, new Date() );
184 consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
185 fail( "Should have failed to find file" );
187 catch ( ConsumerException e )
189 assertTrue( e.getCause() instanceof NoSuchFileException );
193 consumer.completeScan();
196 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
200 public void testConsumerArtifactNotAnArtifactPathNoResults()
203 consumer.beginScan( config, new Date() );
204 // No exception unnecessarily for something we can't report on
205 consumer.processFile( "com/example/invalid-artifact.txt" );
206 consumer.completeScan();
208 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
212 public void testConsumerArtifactNotAnArtifactPathResults()
215 when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn(
216 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
218 // override, this feels a little overspecified though
219 when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
220 new IllegalArgumentException() );
222 consumer.beginScan( config, new Date() );
223 // No exception unnecessarily for something we can't report on
224 consumer.processFile( "com/example/invalid-artifact.txt" );
225 consumer.completeScan();
227 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
230 private static void assertProblem( RepositoryProblemFacet problem )
232 assertEquals( TEST_REPO, problem.getRepositoryId() );
233 assertEquals( TEST_NAMESPACE, problem.getNamespace() );
234 assertEquals( TEST_PROJECT, problem.getProject() );
235 assertEquals( TEST_VERSION, problem.getVersion() );
236 assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
237 assertNotNull( problem.getMessage() );
238 assertEquals( "duplicate-artifact", problem.getProblem() );
241 private static ArtifactMetadata createMetadata( String version )
243 ArtifactMetadata artifact = new ArtifactMetadata();
244 artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
245 artifact.setNamespace( TEST_NAMESPACE );
246 artifact.setProject( TEST_PROJECT );
247 artifact.setProjectVersion( version );
248 artifact.setVersion( version );
249 artifact.setRepositoryId( TEST_REPO );