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 8.9KB

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