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.1KB

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