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

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