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.

ArchivaIndexingTaskExecutorTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package org.apache.archiva.scheduler.indexing.maven;
  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.indexer.ArchivaIndexingContext;
  22. import org.apache.archiva.indexer.UnsupportedBaseContextException;
  23. import org.apache.archiva.repository.ManagedRepository;
  24. import org.apache.archiva.repository.ReleaseScheme;
  25. import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
  26. import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
  27. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  28. import org.apache.archiva.repository.features.IndexCreationFeature;
  29. import org.apache.archiva.repository.storage.StorageAsset;
  30. import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
  31. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  32. import org.apache.maven.index.ArtifactInfo;
  33. import org.apache.maven.index.FlatSearchRequest;
  34. import org.apache.maven.index.FlatSearchResponse;
  35. import org.apache.maven.index.Indexer;
  36. import org.apache.maven.index.MAVEN;
  37. import org.apache.maven.index.context.IndexingContext;
  38. import org.apache.maven.index.expr.SourcedSearchExpression;
  39. import org.apache.maven.index.expr.StringSearchExpression;
  40. import org.apache.maven.index.updater.DefaultIndexUpdater;
  41. import org.apache.maven.index.updater.IndexUpdateRequest;
  42. import org.apache.maven.index.updater.IndexUpdater;
  43. import org.apache.maven.index_shaded.lucene.search.BooleanClause;
  44. import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
  45. import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
  46. import org.apache.maven.index_shaded.lucene.search.TopDocs;
  47. import org.assertj.core.api.Assertions;
  48. import org.junit.After;
  49. import org.junit.Before;
  50. import org.junit.Test;
  51. import org.junit.runner.RunWith;
  52. import org.springframework.test.context.ContextConfiguration;
  53. import javax.inject.Inject;
  54. import java.io.IOException;
  55. import java.nio.file.Files;
  56. import java.nio.file.Path;
  57. import java.nio.file.Paths;
  58. import java.util.Set;
  59. /**
  60. * ArchivaIndexingTaskExecutorTest
  61. */
  62. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  63. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
  64. public class ArchivaIndexingTaskExecutorTest
  65. extends TestCase
  66. {
  67. @Inject
  68. private ArchivaIndexingTaskExecutor indexingExecutor;
  69. @Inject
  70. ArchivaRepositoryRegistry repositoryRegistry;
  71. @SuppressWarnings( "unused" )
  72. @Inject
  73. RepositoryHandlerDependencies repositoryHandlerDependencies;
  74. @Inject
  75. private IndexUpdater indexUpdater;
  76. private ManagedRepository repo;
  77. @Inject
  78. private Indexer indexer;
  79. @Before
  80. @Override
  81. public void setUp()
  82. throws Exception
  83. {
  84. super.setUp();
  85. Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
  86. BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
  87. Path repoLocation = baseDir.resolve("test-repo" );
  88. repositoryConfig.setLocation(repoLocation.toUri() );
  89. repositoryConfig.setLayout( "default" );
  90. repositoryConfig.setScanned( true );
  91. repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  92. repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  93. repositoryRegistry.putRepository(repositoryConfig);
  94. repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
  95. }
  96. @After
  97. @Override
  98. public void tearDown()
  99. throws Exception
  100. {
  101. repositoryRegistry.destroy();
  102. /*
  103. removeIndexingContext with true cleanup files.
  104. // delete created index in the repository
  105. File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
  106. FileUtils.deleteDirectory( indexDir );
  107. assertFalse( indexDir.exists() );
  108. indexDir = new File( repositoryConfig.getLocation(), ".index" );
  109. FileUtils.deleteDirectory( indexDir );
  110. assertFalse( indexDir.exists() );
  111. */
  112. super.tearDown();
  113. }
  114. protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
  115. assert repo != null;
  116. ArchivaIndexingContext ctx = repo.getIndexingContext();
  117. assert ctx != null;
  118. return ctx.getBaseContext(IndexingContext.class);
  119. }
  120. @Test
  121. public void testAddArtifactToIndex()
  122. throws Exception
  123. {
  124. Path basePath = repo.getRoot().getFilePath();
  125. Path artifactFile = basePath.resolve(
  126. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  127. ArtifactIndexingTask task =
  128. new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
  129. repo.getIndexingContext());
  130. indexingExecutor.executeTask( task );
  131. task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
  132. repo.getIndexingContext() );
  133. indexingExecutor.executeTask( task );
  134. BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
  135. queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  136. BooleanClause.Occur.SHOULD );
  137. queryBuilder.add(
  138. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  139. BooleanClause.Occur.SHOULD );
  140. BooleanQuery q = queryBuilder.build();
  141. FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
  142. FlatSearchResponse response = indexer.searchFlat( request );
  143. assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
  144. assertTrue( Files.exists(basePath.resolve(".index" )) );
  145. assertEquals( 1, response.getTotalHitsCount());
  146. Set<ArtifactInfo> results = response.getResults();
  147. ArtifactInfo artifactInfo = results.iterator().next();
  148. assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
  149. assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
  150. assertEquals( "test-repo", artifactInfo.getRepository() );
  151. }
  152. @Test
  153. public void testUpdateArtifactInIndex()
  154. throws Exception
  155. {
  156. Path basePath = repo.getRoot().getFilePath();
  157. Path artifactFile = basePath.resolve(
  158. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  159. ArtifactIndexingTask task =
  160. new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
  161. repo.getIndexingContext() );
  162. indexingExecutor.executeTask( task );
  163. indexingExecutor.executeTask( task );
  164. BooleanQuery.Builder qb = new BooleanQuery.Builder();
  165. qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  166. BooleanClause.Occur.SHOULD );
  167. qb.add(
  168. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  169. BooleanClause.Occur.SHOULD );
  170. IndexingContext ctx = getIndexingContext();
  171. IndexSearcher searcher = ctx.acquireIndexSearcher();
  172. TopDocs topDocs = searcher.search( qb.build(), 10 );
  173. //searcher.close();
  174. ctx.releaseIndexSearcher( searcher );
  175. assertTrue( Files.exists(basePath.resolve(".indexer" )) );
  176. assertTrue( Files.exists(basePath.resolve(".index" )) );
  177. // should only return 1 hit!
  178. assertEquals( 1, topDocs.totalHits );
  179. }
  180. @Test
  181. public void testRemoveArtifactFromIndex()
  182. throws Exception
  183. {
  184. Path basePath = repo.getRoot().getFilePath();
  185. Path artifactFile = basePath.resolve(
  186. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  187. ArtifactIndexingTask task =
  188. new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
  189. repo.getIndexingContext() );
  190. // add artifact to index
  191. indexingExecutor.executeTask( task );
  192. BooleanQuery.Builder qb = new BooleanQuery.Builder();
  193. qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
  194. BooleanClause.Occur.SHOULD );
  195. //q.add(
  196. // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
  197. // Occur.SHOULD );
  198. IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
  199. FlatSearchRequest flatSearchRequest =
  200. new FlatSearchRequest( qb.build(), ctx );
  201. FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
  202. assertTrue( Files.exists(basePath.resolve(".indexer" )) );
  203. assertTrue( Files.exists(basePath.resolve( ".index" )) );
  204. // should return 1 hit
  205. assertEquals( 1, response.getTotalHitsCount() );
  206. // remove added artifact from index
  207. task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
  208. repo.getIndexingContext());
  209. indexingExecutor.executeTask( task );
  210. task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
  211. repo.getIndexingContext() );
  212. indexingExecutor.executeTask( task );
  213. qb = new BooleanQuery.Builder();
  214. qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
  215. BooleanClause.Occur.SHOULD );
  216. qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
  217. new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
  218. BooleanClause.Occur.SHOULD );
  219. assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
  220. assertTrue( Files.exists(basePath.resolve(".index" )) );
  221. flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
  222. response = indexer.searchFlat( flatSearchRequest );
  223. // artifact should have been removed from the index!
  224. assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
  225. // TODO: test it was removed from the packaged index also
  226. }
  227. @Test
  228. public void testPackagedIndex()
  229. throws Exception
  230. {
  231. Path basePath = repo.getRoot().getFilePath();
  232. IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
  233. StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
  234. StorageAsset indexerDirectory = icf.getLocalIndexPath();
  235. for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
  236. if (dir.getFilePath()!=null)
  237. {
  238. Path localDirPath = dir.getFilePath();
  239. Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
  240. .forEach( path ->
  241. {
  242. try
  243. {
  244. System.err.println( "Deleting " + path );
  245. Files.delete( path );
  246. }
  247. catch ( IOException e )
  248. {
  249. e.printStackTrace( );
  250. }
  251. } );
  252. }
  253. }
  254. Path artifactFile = basePath.resolve(
  255. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  256. ArtifactIndexingTask task =
  257. new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
  258. repo.getIndexingContext() );
  259. task.setExecuteOnEntireRepo( false );
  260. indexingExecutor.executeTask( task );
  261. task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
  262. repo.getIndexingContext() );
  263. task.setExecuteOnEntireRepo( false );
  264. indexingExecutor.executeTask( task );
  265. assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
  266. assertTrue( Files.exists(indexerDirectory.getFilePath()) );
  267. // test packed index file creation
  268. //no more zip
  269. //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
  270. Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
  271. Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
  272. assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" ) ));
  273. // unpack .zip index
  274. //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
  275. DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
  276. IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
  277. //updateRequest.setLocalIndexCacheDir( indexerDirectory );
  278. indexUpdater.fetchAndUpdateIndex( updateRequest );
  279. BooleanQuery.Builder qb = new BooleanQuery.Builder();
  280. qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  281. BooleanClause.Occur.SHOULD );
  282. qb.add(
  283. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  284. BooleanClause.Occur.SHOULD );
  285. FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
  286. FlatSearchResponse response = indexer.searchFlat( request );
  287. assertEquals( 1, response.getTotalHitsCount() );
  288. Set<ArtifactInfo> results = response.getResults();
  289. ArtifactInfo artifactInfo = results.iterator().next();
  290. assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
  291. assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
  292. assertEquals( "test-repo", artifactInfo.getRepository() );
  293. }
  294. }