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.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.MetadataFacet;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
28 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
29 import org.apache.archiva.reports.RepositoryProblemFacet;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.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;
39 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41 import javax.inject.Inject;
42 import javax.inject.Named;
44 import java.io.FileNotFoundException;
45 import java.util.Arrays;
46 import java.util.Date;
48 import static org.mockito.Mockito.*;
50 @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
51 @RunWith( SpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
53 public class DuplicateArtifactsConsumerTest
57 @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
58 private DuplicateArtifactsConsumer consumer;
60 private ManagedRepositoryConfiguration config;
62 private MetadataRepository metadataRepository;
64 private static final String TEST_REPO = "test-repo";
66 private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
68 private static final String TEST_PROJECT = "test-artifact";
70 private static final String TEST_NAMESPACE = "com.example.test";
72 private static final String TEST_FILE =
73 "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
75 private static final String TEST_VERSION = "1.0-20100308.230825-1";
77 private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
80 @Named( value = "repositoryPathTranslator#maven2" )
81 private RepositoryPathTranslator pathTranslator;
84 ApplicationContext applicationContext;
93 assertNotNull( consumer );
95 config = new ManagedRepositoryConfiguration();
96 config.setId( TEST_REPO );
97 config.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
99 metadataRepository = mock( MetadataRepository.class );
101 RepositorySession session = mock( RepositorySession.class );
102 when( session.getRepository() ).thenReturn( metadataRepository );
104 RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
105 //(RepositorySessionFactory) lookup( RepositorySessionFactory.class );
106 when( factory.createSession() ).thenReturn( session );
108 when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
112 public void testConsumerArtifactNotDuplicated()
115 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
116 Arrays.asList( TEST_METADATA ) );
118 consumer.beginScan( config, new Date() );
119 consumer.processFile( TEST_FILE );
120 consumer.completeScan();
122 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
125 // TODO: Doesn't currently work
126 // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
127 // throws ConsumerException
129 // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
130 // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
132 // consumer.beginScan( config, new Date() );
133 // consumer.processFile( TEST_FILE );
134 // consumer.completeScan();
136 // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
140 public void testConsumerArtifactDuplicated()
143 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
144 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
146 consumer.beginScan( config, new Date() );
147 consumer.processFile( TEST_FILE );
148 consumer.completeScan();
150 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
151 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
152 RepositoryProblemFacet problem = argument.getValue();
153 assertProblem( problem );
157 public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
160 when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
161 Arrays.asList( createMetadata( "1.0" ) ) );
163 consumer.beginScan( config, new Date() );
164 consumer.processFile( TEST_FILE );
165 consumer.completeScan();
167 ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
168 verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
169 RepositoryProblemFacet problem = argument.getValue();
170 assertProblem( problem );
174 public void testConsumerArtifactFileNotExist()
177 consumer.beginScan( config, new Date() );
180 consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
181 fail( "Should have failed to find file" );
183 catch ( ConsumerException e )
185 assertTrue( e.getCause() instanceof FileNotFoundException );
189 consumer.completeScan();
192 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
196 public void testConsumerArtifactNotAnArtifactPathNoResults()
199 consumer.beginScan( config, new Date() );
200 // No exception unnecessarily for something we can't report on
201 consumer.processFile( "com/example/invalid-artifact.txt" );
202 consumer.completeScan();
204 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
208 public void testConsumerArtifactNotAnArtifactPathResults()
211 when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn(
212 Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
214 // override, this feels a little overspecified though
215 when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
216 new IllegalArgumentException() );
218 consumer.beginScan( config, new Date() );
219 // No exception unnecessarily for something we can't report on
220 consumer.processFile( "com/example/invalid-artifact.txt" );
221 consumer.completeScan();
223 verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
226 private static void assertProblem( RepositoryProblemFacet problem )
228 assertEquals( TEST_REPO, problem.getRepositoryId() );
229 assertEquals( TEST_NAMESPACE, problem.getNamespace() );
230 assertEquals( TEST_PROJECT, problem.getProject() );
231 assertEquals( TEST_VERSION, problem.getVersion() );
232 assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
233 assertNotNull( problem.getMessage() );
234 assertEquals( "duplicate-artifact", problem.getProblem() );
237 private static ArtifactMetadata createMetadata( String version )
239 ArtifactMetadata artifact = new ArtifactMetadata();
240 artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
241 artifact.setNamespace( TEST_NAMESPACE );
242 artifact.setProject( TEST_PROJECT );
243 artifact.setProjectVersion( version );
244 artifact.setVersion( version );
245 artifact.setRepositoryId( TEST_REPO );