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

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