You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DuplicateArtifactsConsumerTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package org.apache.archiva.reports.consumers;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.consumers.ConsumerException;
  22. import org.apache.archiva.metadata.model.ArtifactMetadata;
  23. import org.apache.archiva.metadata.model.MetadataFacet;
  24. import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
  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.repository.BasicManagedRepository;
  30. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.ArgumentCaptor;
  35. import org.mockito.Matchers;
  36. import org.springframework.context.ApplicationContext;
  37. import org.springframework.test.annotation.DirtiesContext;
  38. import org.springframework.test.context.ContextConfiguration;
  39. import javax.inject.Inject;
  40. import javax.inject.Named;
  41. import java.nio.file.NoSuchFileException;
  42. import java.nio.file.Paths;
  43. import java.util.Arrays;
  44. import java.util.Date;
  45. import static org.mockito.Mockito.*;
  46. @SuppressWarnings( { "ThrowableInstanceNeverThrown" } )
  47. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  48. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  49. @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
  50. public class DuplicateArtifactsConsumerTest
  51. extends TestCase
  52. {
  53. @Inject
  54. @Named( value = "knownRepositoryContentConsumer#duplicate-artifacts" )
  55. private DuplicateArtifactsConsumer consumer;
  56. private BasicManagedRepository config;
  57. private MetadataRepository metadataRepository;
  58. private static final String TEST_REPO = "test-repo";
  59. private static final String TEST_CHECKSUM = "edf5938e646956f445c6ecb719d44579cdeed974";
  60. private static final String TEST_PROJECT = "test-artifact";
  61. private static final String TEST_NAMESPACE = "com.example.test";
  62. private static final String TEST_FILE =
  63. "com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar";
  64. private static final String TEST_VERSION = "1.0-20100308.230825-1";
  65. private static final ArtifactMetadata TEST_METADATA = createMetadata( TEST_VERSION );
  66. @Inject
  67. @Named( value = "repositoryPathTranslator#maven2" )
  68. private RepositoryPathTranslator pathTranslator;
  69. @Inject
  70. ApplicationContext applicationContext;
  71. @Before
  72. @Override
  73. public void setUp()
  74. throws Exception
  75. {
  76. super.setUp();
  77. assertNotNull( consumer );
  78. config = BasicManagedRepository.newFilesystemInstance(TEST_REPO, TEST_REPO, Paths.get("target").resolve(TEST_REPO));
  79. config.setLocation( Paths.get( "target/test-repository" ).toAbsolutePath().toUri() );
  80. metadataRepository = mock( MetadataRepository.class );
  81. RepositorySession session = mock( RepositorySession.class );
  82. when( session.getRepository() ).thenReturn( metadataRepository );
  83. RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
  84. //(RepositorySessionFactory) lookup( RepositorySessionFactory.class );
  85. when( factory.createSession() ).thenReturn( session );
  86. when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
  87. }
  88. @Test
  89. public void testConsumerArtifactNotDuplicated()
  90. throws Exception
  91. {
  92. when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
  93. Arrays.asList( TEST_METADATA ) );
  94. consumer.beginScan( config, new Date() );
  95. consumer.processFile( TEST_FILE );
  96. consumer.completeScan();
  97. verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  98. }
  99. // TODO: Doesn't currently work
  100. // public void testConsumerArtifactNotDuplicatedForOtherSnapshots()
  101. // throws ConsumerException
  102. // {
  103. // when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn( Arrays.asList(
  104. // TEST_METADATA, createMetadata( "1.0-20100309.002023-2" ) ) );
  105. //
  106. // consumer.beginScan( config, new Date() );
  107. // consumer.processFile( TEST_FILE );
  108. // consumer.completeScan();
  109. //
  110. // verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  111. // }
  112. @Test
  113. public void testConsumerArtifactDuplicated()
  114. throws Exception
  115. {
  116. when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
  117. Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
  118. consumer.beginScan( config, new Date() );
  119. consumer.processFile( TEST_FILE );
  120. consumer.completeScan();
  121. ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
  122. verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
  123. RepositoryProblemFacet problem = argument.getValue();
  124. assertProblem( problem );
  125. }
  126. @Test
  127. public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
  128. throws Exception
  129. {
  130. when( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
  131. Arrays.asList( createMetadata( "1.0" ) ) );
  132. consumer.beginScan( config, new Date() );
  133. consumer.processFile( TEST_FILE );
  134. consumer.completeScan();
  135. ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
  136. verify( metadataRepository ).addMetadataFacet( eq( TEST_REPO ), argument.capture() );
  137. RepositoryProblemFacet problem = argument.getValue();
  138. assertProblem( problem );
  139. }
  140. @Test
  141. public void testConsumerArtifactFileNotExist()
  142. throws Exception
  143. {
  144. consumer.beginScan( config, new Date() );
  145. try
  146. {
  147. consumer.processFile( "com/example/test/test-artifact/2.0/test-artifact-2.0.jar" );
  148. fail( "Should have failed to find file" );
  149. }
  150. catch ( ConsumerException e )
  151. {
  152. assertTrue( e.getCause() instanceof NoSuchFileException );
  153. }
  154. finally
  155. {
  156. consumer.completeScan();
  157. }
  158. verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  159. }
  160. @Test
  161. public void testConsumerArtifactNotAnArtifactPathNoResults()
  162. throws Exception
  163. {
  164. consumer.beginScan( config, new Date() );
  165. // No exception unnecessarily for something we can't report on
  166. consumer.processFile( "com/example/invalid-artifact.txt" );
  167. consumer.completeScan();
  168. verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  169. }
  170. @Test
  171. public void testConsumerArtifactNotAnArtifactPathResults()
  172. throws Exception
  173. {
  174. when( metadataRepository.getArtifactsByChecksum( eq( TEST_REPO ), anyString() ) ).thenReturn(
  175. Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
  176. // override, this feels a little overspecified though
  177. when( pathTranslator.getArtifactForPath( TEST_REPO, "com/example/invalid-artifact.txt" ) ).thenThrow(
  178. new IllegalArgumentException() );
  179. consumer.beginScan( config, new Date() );
  180. // No exception unnecessarily for something we can't report on
  181. consumer.processFile( "com/example/invalid-artifact.txt" );
  182. consumer.completeScan();
  183. verify( metadataRepository, never() ).addMetadataFacet( eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
  184. }
  185. private static void assertProblem( RepositoryProblemFacet problem )
  186. {
  187. assertEquals( TEST_REPO, problem.getRepositoryId() );
  188. assertEquals( TEST_NAMESPACE, problem.getNamespace() );
  189. assertEquals( TEST_PROJECT, problem.getProject() );
  190. assertEquals( TEST_VERSION, problem.getVersion() );
  191. assertEquals( TEST_PROJECT + "-" + TEST_VERSION + ".jar", problem.getId() );
  192. assertNotNull( problem.getMessage() );
  193. assertEquals( "duplicate-artifact", problem.getProblem() );
  194. }
  195. private static ArtifactMetadata createMetadata( String version )
  196. {
  197. ArtifactMetadata artifact = new ArtifactMetadata();
  198. artifact.setId( TEST_PROJECT + "-" + version + ".jar" );
  199. artifact.setNamespace( TEST_NAMESPACE );
  200. artifact.setProject( TEST_PROJECT );
  201. artifact.setProjectVersion( version );
  202. artifact.setVersion( version );
  203. artifact.setRepositoryId( TEST_REPO );
  204. return artifact;
  205. }
  206. }