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.

NexusIndexerConsumerTest.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package org.apache.archiva.consumers.lucene;
  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.common.utils.PathUtil;
  22. import org.apache.archiva.components.taskqueue.TaskQueueException;
  23. import org.apache.archiva.configuration.provider.ArchivaConfiguration;
  24. import org.apache.archiva.configuration.provider.FileTypes;
  25. import org.apache.archiva.repository.ReleaseScheme;
  26. import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
  27. import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
  28. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  29. import org.apache.archiva.scheduler.ArchivaTaskScheduler;
  30. import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
  31. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  32. import org.junit.After;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.springframework.context.ApplicationContext;
  37. import org.springframework.test.context.ContextConfiguration;
  38. import javax.inject.Inject;
  39. import java.io.IOException;
  40. import java.net.URI;
  41. import java.nio.file.Files;
  42. import java.nio.file.Path;
  43. import java.nio.file.Paths;
  44. import java.util.Calendar;
  45. import java.util.Date;
  46. import java.util.HashSet;
  47. import java.util.List;
  48. import java.util.Set;
  49. /**
  50. * NexusIndexerConsumerTest
  51. */
  52. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  53. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  54. public class NexusIndexerConsumerTest
  55. extends TestCase
  56. {
  57. private final class ArchivaTaskSchedulerStub
  58. implements ArchivaTaskScheduler<ArtifactIndexingTask>
  59. {
  60. Set<Path> indexed = new HashSet<>();
  61. @Override
  62. public void queueTask( ArtifactIndexingTask task )
  63. throws TaskQueueException
  64. {
  65. switch ( task.getAction() )
  66. {
  67. case ADD:
  68. indexed.add( task.getResourceFile() );
  69. break;
  70. case DELETE:
  71. indexed.remove( task.getResourceFile() );
  72. break;
  73. case FINISH:
  74. try
  75. {
  76. task.getContext().close( false );
  77. }
  78. catch ( IOException e )
  79. {
  80. throw new TaskQueueException( e.getMessage() );
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. private NexusIndexerConsumer nexusIndexerConsumer;
  87. private BasicManagedRepository repositoryConfig;
  88. private ArchivaTaskSchedulerStub scheduler;
  89. @Inject
  90. private ApplicationContext applicationContext;
  91. @Inject
  92. ArchivaRepositoryRegistry repositoryRegistry;
  93. @SuppressWarnings( "unused" )
  94. @Inject
  95. RepositoryHandlerDependencies repositoryHandlerDependencies;
  96. @Override
  97. @Before
  98. public void setUp()
  99. throws Exception
  100. {
  101. super.setUp();
  102. scheduler = new ArchivaTaskSchedulerStub();
  103. ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
  104. FileTypes filetypes = applicationContext.getBean( FileTypes.class );
  105. nexusIndexerConsumer =
  106. new NexusIndexerConsumer( scheduler, configuration, filetypes);
  107. // initialize to set the file types to be processed
  108. nexusIndexerConsumer.initialize();
  109. repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
  110. repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
  111. repositoryConfig.setLayout( "default" );
  112. repositoryConfig.setScanned( true );
  113. repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  114. repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  115. repositoryRegistry.putRepository(repositoryConfig);
  116. }
  117. @Override
  118. @After
  119. public void tearDown()
  120. throws Exception
  121. {
  122. // delete created index in the repository
  123. Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
  124. Path indexDir = basePath.resolve( ".indexer" );
  125. org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
  126. assertFalse( Files.exists(indexDir) );
  127. indexDir = basePath.resolve( ".index" );
  128. org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
  129. assertFalse( Files.exists(indexDir) );
  130. repositoryRegistry.destroy();
  131. super.tearDown();
  132. }
  133. @Test
  134. public void testIndexerIndexArtifact()
  135. throws Exception
  136. {
  137. Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
  138. Path artifactFile = basePath.resolve(
  139. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  140. // begin scan
  141. Date now = Calendar.getInstance().getTime();
  142. nexusIndexerConsumer.beginScan( repositoryConfig, now );
  143. nexusIndexerConsumer.processFile(
  144. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  145. nexusIndexerConsumer.completeScan();
  146. assertTrue( scheduler.indexed.contains( artifactFile ) );
  147. }
  148. @Test
  149. public void testIndexerArtifactAlreadyIndexed()
  150. throws Exception
  151. {
  152. Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
  153. Path artifactFile = basePath.resolve(
  154. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  155. // begin scan
  156. Date now = Calendar.getInstance().getTime();
  157. nexusIndexerConsumer.beginScan( repositoryConfig, now );
  158. nexusIndexerConsumer.processFile(
  159. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  160. nexusIndexerConsumer.completeScan();
  161. assertTrue( scheduler.indexed.contains( artifactFile ) );
  162. // scan and index again
  163. now = Calendar.getInstance().getTime();
  164. nexusIndexerConsumer.beginScan( repositoryConfig, now );
  165. nexusIndexerConsumer.processFile(
  166. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  167. nexusIndexerConsumer.completeScan();
  168. assertTrue( scheduler.indexed.contains( artifactFile ) );
  169. }
  170. @Test
  171. public void testIndexerIndexArtifactThenPom()
  172. throws Exception
  173. {
  174. Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
  175. Path artifactFile = basePath.resolve(
  176. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  177. // begin scan
  178. Date now = Calendar.getInstance().getTime();
  179. nexusIndexerConsumer.beginScan( repositoryConfig, now );
  180. nexusIndexerConsumer.processFile(
  181. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  182. nexusIndexerConsumer.completeScan();
  183. assertTrue( scheduler.indexed.contains( artifactFile ) );
  184. artifactFile =
  185. basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
  186. // scan and index again
  187. now = Calendar.getInstance().getTime();
  188. nexusIndexerConsumer.beginScan( repositoryConfig, now );
  189. nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
  190. nexusIndexerConsumer.completeScan();
  191. assertTrue( scheduler.indexed.contains( artifactFile ) );
  192. }
  193. // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
  194. @Test
  195. public void testIncludedFileTypes()
  196. throws Exception
  197. {
  198. List<String> includes = nexusIndexerConsumer.getIncludes();
  199. assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
  200. assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
  201. assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
  202. assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
  203. assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
  204. assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
  205. }
  206. }