Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArchivaIndexingTaskExecutorTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package org.apache.archiva.scheduler.indexing;
  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.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  23. import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
  24. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  25. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  26. import org.apache.lucene.search.BooleanClause.Occur;
  27. import org.apache.lucene.search.BooleanQuery;
  28. import org.apache.lucene.search.IndexSearcher;
  29. import org.apache.lucene.search.TopDocs;
  30. import org.apache.maven.index.ArtifactInfo;
  31. import org.apache.maven.index.FlatSearchRequest;
  32. import org.apache.maven.index.FlatSearchResponse;
  33. import org.apache.maven.index.MAVEN;
  34. import org.apache.maven.index.NexusIndexer;
  35. import org.apache.maven.index.context.IndexingContext;
  36. import org.apache.maven.index.expr.SourcedSearchExpression;
  37. import org.apache.maven.index.expr.StringSearchExpression;
  38. import org.junit.After;
  39. import org.junit.Before;
  40. import org.junit.Test;
  41. import org.junit.runner.RunWith;
  42. import org.springframework.test.context.ContextConfiguration;
  43. import javax.inject.Inject;
  44. import java.io.BufferedInputStream;
  45. import java.io.BufferedOutputStream;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.FileOutputStream;
  49. import java.io.FilenameFilter;
  50. import java.io.IOException;
  51. import java.util.Set;
  52. import java.util.zip.ZipEntry;
  53. import java.util.zip.ZipInputStream;
  54. /**
  55. * ArchivaIndexingTaskExecutorTest
  56. */
  57. @RunWith (ArchivaSpringJUnit4ClassRunner.class)
  58. @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
  59. public class ArchivaIndexingTaskExecutorTest
  60. extends TestCase
  61. {
  62. @Inject
  63. private ArchivaIndexingTaskExecutor indexingExecutor;
  64. private ManagedRepository repositoryConfig;
  65. private NexusIndexer indexer;
  66. @Inject
  67. PlexusSisuBridge plexusSisuBridge;
  68. @Inject
  69. MavenIndexerUtils mavenIndexerUtils;
  70. @Inject
  71. ManagedRepositoryAdmin managedRepositoryAdmin;
  72. @Before
  73. @Override
  74. public void setUp()
  75. throws Exception
  76. {
  77. super.setUp();
  78. repositoryConfig = new ManagedRepository();
  79. repositoryConfig.setId( "test-repo" );
  80. repositoryConfig.setLocation(
  81. new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
  82. repositoryConfig.setLayout( "default" );
  83. repositoryConfig.setName( "Test Repository" );
  84. repositoryConfig.setScanned( true );
  85. repositoryConfig.setSnapshots( false );
  86. repositoryConfig.setReleases( true );
  87. indexer = plexusSisuBridge.lookup( NexusIndexer.class );
  88. managedRepositoryAdmin.createIndexContext( repositoryConfig );
  89. }
  90. @After
  91. @Override
  92. public void tearDown()
  93. throws Exception
  94. {
  95. for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
  96. {
  97. indexer.removeIndexingContext( indexingContext, true );
  98. }
  99. /*
  100. removeIndexingContext with true cleanup files.
  101. // delete created index in the repository
  102. File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
  103. FileUtils.deleteDirectory( indexDir );
  104. assertFalse( indexDir.exists() );
  105. indexDir = new File( repositoryConfig.getLocation(), ".index" );
  106. FileUtils.deleteDirectory( indexDir );
  107. assertFalse( indexDir.exists() );
  108. */
  109. super.tearDown();
  110. }
  111. protected IndexingContext getIndexingContext()
  112. {
  113. return indexer.getIndexingContexts().get( repositoryConfig.getId() );
  114. }
  115. @Test
  116. public void testAddArtifactToIndex()
  117. throws Exception
  118. {
  119. File artifactFile = new File( repositoryConfig.getLocation(),
  120. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  121. ArtifactIndexingTask task =
  122. new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
  123. getIndexingContext() );
  124. indexingExecutor.executeTask( task );
  125. BooleanQuery q = new BooleanQuery();
  126. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  127. Occur.SHOULD );
  128. q.add(
  129. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  130. Occur.SHOULD );
  131. if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
  132. {
  133. IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
  134. new File( repositoryConfig.getLocation() ),
  135. new File( repositoryConfig.getLocation(),
  136. ".indexer" ), null, null,
  137. mavenIndexerUtils.getAllIndexCreators() );
  138. context.setSearchable( true );
  139. }
  140. FlatSearchRequest request = new FlatSearchRequest( q );
  141. FlatSearchResponse response = indexer.searchFlat( request );
  142. assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
  143. assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
  144. assertEquals( 1, response.getTotalHits() );
  145. Set<ArtifactInfo> results = response.getResults();
  146. ArtifactInfo artifactInfo = results.iterator().next();
  147. assertEquals( "org.apache.archiva", artifactInfo.groupId );
  148. assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
  149. assertEquals( "test-repo", artifactInfo.repository );
  150. }
  151. @Test
  152. public void testUpdateArtifactInIndex()
  153. throws Exception
  154. {
  155. File artifactFile = new File( repositoryConfig.getLocation(),
  156. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  157. ArtifactIndexingTask task =
  158. new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
  159. getIndexingContext() );
  160. indexingExecutor.executeTask( task );
  161. indexingExecutor.executeTask( task );
  162. BooleanQuery q = new BooleanQuery();
  163. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  164. Occur.SHOULD );
  165. q.add(
  166. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  167. Occur.SHOULD );
  168. IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
  169. IndexSearcher searcher = ctx.acquireIndexSearcher();
  170. TopDocs topDocs = searcher.search( q, null, 10 );
  171. //searcher.close();
  172. ctx.releaseIndexSearcher( searcher );
  173. assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
  174. assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
  175. // should only return 1 hit!
  176. assertEquals( 1, topDocs.totalHits );
  177. }
  178. @Test
  179. public void testRemoveArtifactFromIndex()
  180. throws Exception
  181. {
  182. File artifactFile = new File( repositoryConfig.getLocation(),
  183. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  184. ArtifactIndexingTask task =
  185. new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
  186. getIndexingContext() );
  187. // add artifact to index
  188. indexingExecutor.executeTask( task );
  189. BooleanQuery q = new BooleanQuery();
  190. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
  191. Occur.SHOULD );
  192. //q.add(
  193. // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
  194. // Occur.SHOULD );
  195. FlatSearchRequest flatSearchRequest =
  196. new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
  197. FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
  198. assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
  199. assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
  200. // should return 1 hit
  201. assertEquals( 1, response.getTotalHitsCount() );
  202. // remove added artifact from index
  203. task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
  204. getIndexingContext() );
  205. indexingExecutor.executeTask( task );
  206. task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
  207. getIndexingContext() );
  208. indexingExecutor.executeTask( task );
  209. q = new BooleanQuery();
  210. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
  211. Occur.SHOULD );
  212. q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
  213. new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
  214. Occur.SHOULD );
  215. assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
  216. assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
  217. flatSearchRequest = new FlatSearchRequest( q, 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. File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
  228. indexerDirectory.listFiles( new FilenameFilter()
  229. {
  230. @Override
  231. public boolean accept( File file, String s )
  232. {
  233. if ( s.startsWith( "nexus-maven-repository-index" ) )
  234. {
  235. new File( file, s ).delete();
  236. }
  237. return false;
  238. }
  239. } );
  240. File artifactFile = new File( repositoryConfig.getLocation(),
  241. "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
  242. ArtifactIndexingTask task =
  243. new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
  244. getIndexingContext() );
  245. task.setExecuteOnEntireRepo( false );
  246. indexingExecutor.executeTask( task );
  247. task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
  248. getIndexingContext() );
  249. task.setExecuteOnEntireRepo( false );
  250. indexingExecutor.executeTask( task );
  251. assertTrue( indexerDirectory.exists() );
  252. // test packed index file creation
  253. assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.zip" ).exists() );
  254. assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.properties" ).exists() );
  255. assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.gz" ).exists() );
  256. // unpack .zip index
  257. File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
  258. unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
  259. BooleanQuery q = new BooleanQuery();
  260. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
  261. Occur.SHOULD );
  262. q.add(
  263. indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
  264. Occur.SHOULD );
  265. FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
  266. FlatSearchResponse response = indexer.searchFlat( request );
  267. Set<ArtifactInfo> results = response.getResults();
  268. ArtifactInfo artifactInfo = results.iterator().next();
  269. assertEquals( "org.apache.archiva", artifactInfo.groupId );
  270. assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
  271. assertEquals( "test-repo", artifactInfo.repository );
  272. assertEquals( 1, response.getTotalHits() );
  273. }
  274. private void unzipIndex( String indexDir, String destDir )
  275. throws IOException
  276. {
  277. final int buff = 2048;
  278. new File( destDir ).mkdirs();
  279. BufferedOutputStream out = null;
  280. FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
  281. ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
  282. ZipEntry entry;
  283. while ( ( entry = in.getNextEntry() ) != null )
  284. {
  285. int count;
  286. byte data[] = new byte[buff];
  287. FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
  288. out = new BufferedOutputStream( fout, buff );
  289. while ( ( count = in.read( data, 0, buff ) ) != -1 )
  290. {
  291. out.write( data, 0, count );
  292. }
  293. out.flush();
  294. out.close();
  295. }
  296. in.close();
  297. }
  298. }