1 package org.apache.archiva.scheduler.indexing.maven;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.UnsupportedBaseContextException;
25 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.base.BasicManagedRepository;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.ReleaseScheme;
29 import org.apache.archiva.repository.storage.StorageAsset;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.maven.index.ArtifactInfo;
34 import org.apache.maven.index.FlatSearchRequest;
35 import org.apache.maven.index.FlatSearchResponse;
36 import org.apache.maven.index.Indexer;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.apache.maven.index.expr.SourcedSearchExpression;
40 import org.apache.maven.index.expr.StringSearchExpression;
41 import org.apache.maven.index.updater.DefaultIndexUpdater;
42 import org.apache.maven.index.updater.IndexUpdateRequest;
43 import org.apache.maven.index.updater.IndexUpdater;
44 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
45 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
46 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
47 import org.apache.maven.index_shaded.lucene.search.TopDocs;
48 import org.assertj.core.api.Assertions;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.springframework.test.context.ContextConfiguration;
55 import javax.inject.Inject;
56 import java.io.IOException;
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
63 * ArchivaIndexingTaskExecutorTest
65 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
66 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
67 public class ArchivaIndexingTaskExecutorTest
71 private ArchivaIndexingTaskExecutor indexingExecutor;
74 ArchivaRepositoryRegistry repositoryRegistry;
77 private IndexUpdater indexUpdater;
79 private ManagedRepository repo;
82 private Indexer indexer;
91 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
92 BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
93 Path repoLocation = baseDir.resolve("test-repo" );
94 repositoryConfig.setLocation(repoLocation.toUri() );
95 repositoryConfig.setLayout( "default" );
96 repositoryConfig.setScanned( true );
97 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
98 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
99 repositoryRegistry.putRepository(repositoryConfig);
100 repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
105 public void tearDown()
109 repositoryRegistry.destroy();
111 removeIndexingContext with true cleanup files.
112 // delete created index in the repository
113 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
114 FileUtils.deleteDirectory( indexDir );
115 assertFalse( indexDir.exists() );
117 indexDir = new File( repositoryConfig.getLocation(), ".index" );
118 FileUtils.deleteDirectory( indexDir );
119 assertFalse( indexDir.exists() );
124 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
126 ArchivaIndexingContext ctx = repo.getIndexingContext();
128 return ctx.getBaseContext(IndexingContext.class);
132 public void testAddArtifactToIndex()
135 Path basePath = repo.getLocalPath().getFilePath();
136 Path artifactFile = basePath.resolve(
137 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
139 ArtifactIndexingTask task =
140 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
141 repo.getIndexingContext());
143 indexingExecutor.executeTask( task );
145 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
146 repo.getIndexingContext() );
147 indexingExecutor.executeTask( task );
149 BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
150 queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
151 BooleanClause.Occur.SHOULD );
153 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
154 BooleanClause.Occur.SHOULD );
155 BooleanQuery q = queryBuilder.build();
157 FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
158 FlatSearchResponse response = indexer.searchFlat( request );
160 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
161 assertTrue( Files.exists(basePath.resolve(".index" )) );
162 assertEquals( 1, response.getTotalHitsCount());
164 Set<ArtifactInfo> results = response.getResults();
166 ArtifactInfo artifactInfo = results.iterator().next();
167 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
168 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
169 assertEquals( "test-repo", artifactInfo.getRepository() );
174 public void testUpdateArtifactInIndex()
177 Path basePath = repo.getLocalPath().getFilePath();
178 Path artifactFile = basePath.resolve(
179 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
181 ArtifactIndexingTask task =
182 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
183 repo.getIndexingContext() );
185 indexingExecutor.executeTask( task );
186 indexingExecutor.executeTask( task );
188 BooleanQuery.Builder qb = new BooleanQuery.Builder();
189 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
190 BooleanClause.Occur.SHOULD );
192 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
193 BooleanClause.Occur.SHOULD );
195 IndexingContext ctx = getIndexingContext();
197 IndexSearcher searcher = ctx.acquireIndexSearcher();
198 TopDocs topDocs = searcher.search( qb.build(), 10 );
201 ctx.releaseIndexSearcher( searcher );
203 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
204 assertTrue( Files.exists(basePath.resolve(".index" )) );
206 // should only return 1 hit!
207 assertEquals( 1, topDocs.totalHits );
211 public void testRemoveArtifactFromIndex()
214 Path basePath = repo.getLocalPath().getFilePath();
215 Path artifactFile = basePath.resolve(
216 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
218 ArtifactIndexingTask task =
219 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
220 repo.getIndexingContext() );
222 // add artifact to index
223 indexingExecutor.executeTask( task );
225 BooleanQuery.Builder qb = new BooleanQuery.Builder();
226 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
227 BooleanClause.Occur.SHOULD );
229 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
232 IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
233 FlatSearchRequest flatSearchRequest =
234 new FlatSearchRequest( qb.build(), ctx );
236 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
238 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
239 assertTrue( Files.exists(basePath.resolve( ".index" )) );
241 // should return 1 hit
242 assertEquals( 1, response.getTotalHitsCount() );
244 // remove added artifact from index
245 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
246 repo.getIndexingContext());
247 indexingExecutor.executeTask( task );
249 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
250 repo.getIndexingContext() );
251 indexingExecutor.executeTask( task );
253 qb = new BooleanQuery.Builder();
254 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
255 BooleanClause.Occur.SHOULD );
256 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
257 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
258 BooleanClause.Occur.SHOULD );
260 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
261 assertTrue( Files.exists(basePath.resolve(".index" )) );
263 flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
265 response = indexer.searchFlat( flatSearchRequest );
266 // artifact should have been removed from the index!
267 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
269 // TODO: test it was removed from the packaged index also
273 public void testPackagedIndex()
277 Path basePath = repo.getLocalPath().getFilePath();
278 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
279 StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
280 StorageAsset indexerDirectory = icf.getLocalIndexPath();
282 for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
283 if (dir.getFilePath()!=null)
285 Path localDirPath = dir.getFilePath();
286 Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
291 System.err.println( "Deleting " + path );
292 Files.delete( path );
294 catch ( IOException e )
296 e.printStackTrace( );
305 Path artifactFile = basePath.resolve(
306 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
307 ArtifactIndexingTask task =
308 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
309 repo.getIndexingContext() );
310 task.setExecuteOnEntireRepo( false );
312 indexingExecutor.executeTask( task );
314 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
315 repo.getIndexingContext() );
317 task.setExecuteOnEntireRepo( false );
319 indexingExecutor.executeTask( task );
321 assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
322 assertTrue( Files.exists(indexerDirectory.getFilePath()) );
324 // test packed index file creation
326 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
327 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
328 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
329 assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" ) ));
332 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
334 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
335 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
336 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
337 indexUpdater.fetchAndUpdateIndex( updateRequest );
339 BooleanQuery.Builder qb = new BooleanQuery.Builder();
340 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
341 BooleanClause.Occur.SHOULD );
343 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
344 BooleanClause.Occur.SHOULD );
346 FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
347 FlatSearchResponse response = indexer.searchFlat( request );
349 assertEquals( 1, response.getTotalHitsCount() );
350 Set<ArtifactInfo> results = response.getResults();
352 ArtifactInfo artifactInfo = results.iterator().next();
353 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
354 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
355 assertEquals( "test-repo", artifactInfo.getRepository() );