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.base.RepositoryGroupHandler;
30 import org.apache.archiva.repository.storage.StorageAsset;
31 import org.apache.archiva.repository.features.IndexCreationFeature;
32 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.maven.index.ArtifactInfo;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.Indexer;
38 import org.apache.maven.index.MAVEN;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.SourcedSearchExpression;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.apache.maven.index.updater.DefaultIndexUpdater;
43 import org.apache.maven.index.updater.IndexUpdateRequest;
44 import org.apache.maven.index.updater.IndexUpdater;
45 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
46 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
47 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
48 import org.apache.maven.index_shaded.lucene.search.TopDocs;
49 import org.assertj.core.api.Assertions;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.springframework.test.context.ContextConfiguration;
56 import javax.inject.Inject;
57 import java.io.IOException;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.nio.file.Paths;
64 * ArchivaIndexingTaskExecutorTest
66 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
67 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
68 public class ArchivaIndexingTaskExecutorTest
72 private ArchivaIndexingTaskExecutor indexingExecutor;
75 ArchivaRepositoryRegistry repositoryRegistry;
78 RepositoryGroupHandler repositoryGroupHandler;
81 private IndexUpdater indexUpdater;
83 private ManagedRepository repo;
86 private Indexer indexer;
95 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
96 BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
97 Path repoLocation = baseDir.resolve("test-repo" );
98 repositoryConfig.setLocation(repoLocation.toUri() );
99 repositoryConfig.setLayout( "default" );
100 repositoryConfig.setScanned( true );
101 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
102 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
103 repositoryRegistry.putRepository(repositoryConfig);
104 repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
109 public void tearDown()
113 repositoryRegistry.destroy();
115 removeIndexingContext with true cleanup files.
116 // delete created index in the repository
117 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
118 FileUtils.deleteDirectory( indexDir );
119 assertFalse( indexDir.exists() );
121 indexDir = new File( repositoryConfig.getLocation(), ".index" );
122 FileUtils.deleteDirectory( indexDir );
123 assertFalse( indexDir.exists() );
128 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
130 ArchivaIndexingContext ctx = repo.getIndexingContext();
132 return ctx.getBaseContext(IndexingContext.class);
136 public void testAddArtifactToIndex()
139 Path basePath = repo.getRoot().getFilePath();
140 Path artifactFile = basePath.resolve(
141 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
143 ArtifactIndexingTask task =
144 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
145 repo.getIndexingContext());
147 indexingExecutor.executeTask( task );
149 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
150 repo.getIndexingContext() );
151 indexingExecutor.executeTask( task );
153 BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
154 queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
155 BooleanClause.Occur.SHOULD );
157 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
158 BooleanClause.Occur.SHOULD );
159 BooleanQuery q = queryBuilder.build();
161 FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
162 FlatSearchResponse response = indexer.searchFlat( request );
164 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
165 assertTrue( Files.exists(basePath.resolve(".index" )) );
166 assertEquals( 1, response.getTotalHitsCount());
168 Set<ArtifactInfo> results = response.getResults();
170 ArtifactInfo artifactInfo = results.iterator().next();
171 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
172 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
173 assertEquals( "test-repo", artifactInfo.getRepository() );
178 public void testUpdateArtifactInIndex()
181 Path basePath = repo.getRoot().getFilePath();
182 Path artifactFile = basePath.resolve(
183 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185 ArtifactIndexingTask task =
186 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
187 repo.getIndexingContext() );
189 indexingExecutor.executeTask( task );
190 indexingExecutor.executeTask( task );
192 BooleanQuery.Builder qb = new BooleanQuery.Builder();
193 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
194 BooleanClause.Occur.SHOULD );
196 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
197 BooleanClause.Occur.SHOULD );
199 IndexingContext ctx = getIndexingContext();
201 IndexSearcher searcher = ctx.acquireIndexSearcher();
202 TopDocs topDocs = searcher.search( qb.build(), 10 );
205 ctx.releaseIndexSearcher( searcher );
207 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
208 assertTrue( Files.exists(basePath.resolve(".index" )) );
210 // should only return 1 hit!
211 assertEquals( 1, topDocs.totalHits );
215 public void testRemoveArtifactFromIndex()
218 Path basePath = repo.getRoot().getFilePath();
219 Path artifactFile = basePath.resolve(
220 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
222 ArtifactIndexingTask task =
223 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
224 repo.getIndexingContext() );
226 // add artifact to index
227 indexingExecutor.executeTask( task );
229 BooleanQuery.Builder qb = new BooleanQuery.Builder();
230 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
231 BooleanClause.Occur.SHOULD );
233 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
236 IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
237 FlatSearchRequest flatSearchRequest =
238 new FlatSearchRequest( qb.build(), ctx );
240 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
242 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
243 assertTrue( Files.exists(basePath.resolve( ".index" )) );
245 // should return 1 hit
246 assertEquals( 1, response.getTotalHitsCount() );
248 // remove added artifact from index
249 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
250 repo.getIndexingContext());
251 indexingExecutor.executeTask( task );
253 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
254 repo.getIndexingContext() );
255 indexingExecutor.executeTask( task );
257 qb = new BooleanQuery.Builder();
258 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
259 BooleanClause.Occur.SHOULD );
260 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
261 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
262 BooleanClause.Occur.SHOULD );
264 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
265 assertTrue( Files.exists(basePath.resolve(".index" )) );
267 flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
269 response = indexer.searchFlat( flatSearchRequest );
270 // artifact should have been removed from the index!
271 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
273 // TODO: test it was removed from the packaged index also
277 public void testPackagedIndex()
281 Path basePath = repo.getRoot().getFilePath();
282 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
283 StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
284 StorageAsset indexerDirectory = icf.getLocalIndexPath();
286 for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
287 if (dir.getFilePath()!=null)
289 Path localDirPath = dir.getFilePath();
290 Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
295 System.err.println( "Deleting " + path );
296 Files.delete( path );
298 catch ( IOException e )
300 e.printStackTrace( );
309 Path artifactFile = basePath.resolve(
310 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
311 ArtifactIndexingTask task =
312 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
313 repo.getIndexingContext() );
314 task.setExecuteOnEntireRepo( false );
316 indexingExecutor.executeTask( task );
318 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
319 repo.getIndexingContext() );
321 task.setExecuteOnEntireRepo( false );
323 indexingExecutor.executeTask( task );
325 assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
326 assertTrue( Files.exists(indexerDirectory.getFilePath()) );
328 // test packed index file creation
330 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
331 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
332 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
333 assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" ) ));
336 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
338 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
339 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
340 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
341 indexUpdater.fetchAndUpdateIndex( updateRequest );
343 BooleanQuery.Builder qb = new BooleanQuery.Builder();
344 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
345 BooleanClause.Occur.SHOULD );
347 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
348 BooleanClause.Occur.SHOULD );
350 FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
351 FlatSearchResponse response = indexer.searchFlat( request );
353 assertEquals( 1, response.getTotalHitsCount() );
354 Set<ArtifactInfo> results = response.getResults();
356 ArtifactInfo artifactInfo = results.iterator().next();
357 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
358 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
359 assertEquals( "test-repo", artifactInfo.getRepository() );