1 package org.apache.archiva.scheduler.indexing;
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.BasicManagedRepository;
26 import org.apache.archiva.repository.ManagedRepository;
27 import org.apache.archiva.repository.ReleaseScheme;
28 import org.apache.archiva.repository.RepositoryRegistry;
29 import org.apache.archiva.repository.features.IndexCreationFeature;
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;
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;
61 * ArchivaIndexingTaskExecutorTest
63 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
64 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
65 public class ArchivaIndexingTaskExecutorTest
69 private ArchivaIndexingTaskExecutor indexingExecutor;
72 RepositoryRegistry repositoryRegistry;
75 private IndexUpdater indexUpdater;
77 private ManagedRepository repo;
80 private Indexer indexer;
89 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
90 BasicManagedRepository repositoryConfig = new BasicManagedRepository( "test-repo", "Test Repository", baseDir);
91 Path repoLocation = baseDir.resolve("test-repo" );
92 repositoryConfig.setLocation(repoLocation.toUri() );
93 repositoryConfig.setLayout( "default" );
94 repositoryConfig.setScanned( true );
95 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
96 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
97 repositoryRegistry.putRepository(repositoryConfig);
98 repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
103 public void tearDown()
107 repositoryRegistry.destroy();
109 removeIndexingContext with true cleanup files.
110 // delete created index in the repository
111 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
112 FileUtils.deleteDirectory( indexDir );
113 assertFalse( indexDir.exists() );
115 indexDir = new File( repositoryConfig.getLocation(), ".index" );
116 FileUtils.deleteDirectory( indexDir );
117 assertFalse( indexDir.exists() );
122 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
124 ArchivaIndexingContext ctx = repo.getIndexingContext();
126 return ctx.getBaseContext(IndexingContext.class);
130 public void testAddArtifactToIndex()
133 Path basePath = repo.getLocalPath();
134 Path artifactFile = basePath.resolve(
135 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
137 ArtifactIndexingTask task =
138 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
139 repo.getIndexingContext());
141 indexingExecutor.executeTask( task );
143 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
144 repo.getIndexingContext() );
145 indexingExecutor.executeTask( task );
147 BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
148 queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
149 BooleanClause.Occur.SHOULD );
151 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
152 BooleanClause.Occur.SHOULD );
153 BooleanQuery q = queryBuilder.build();
155 FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
156 FlatSearchResponse response = indexer.searchFlat( request );
158 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
159 assertTrue( Files.exists(basePath.resolve(".index" )) );
160 assertEquals( 1, response.getTotalHits() );
162 Set<ArtifactInfo> results = response.getResults();
164 ArtifactInfo artifactInfo = results.iterator().next();
165 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
166 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
167 assertEquals( "test-repo", artifactInfo.getRepository() );
172 public void testUpdateArtifactInIndex()
175 Path basePath = repo.getLocalPath();
176 Path artifactFile = basePath.resolve(
177 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
179 ArtifactIndexingTask task =
180 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
181 repo.getIndexingContext() );
183 indexingExecutor.executeTask( task );
184 indexingExecutor.executeTask( task );
186 BooleanQuery.Builder qb = new BooleanQuery.Builder();
187 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
188 BooleanClause.Occur.SHOULD );
190 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
191 BooleanClause.Occur.SHOULD );
193 IndexingContext ctx = getIndexingContext();
195 IndexSearcher searcher = ctx.acquireIndexSearcher();
196 TopDocs topDocs = searcher.search( qb.build(), 10 );
199 ctx.releaseIndexSearcher( searcher );
201 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
202 assertTrue( Files.exists(basePath.resolve(".index" )) );
204 // should only return 1 hit!
205 assertEquals( 1, topDocs.totalHits );
209 public void testRemoveArtifactFromIndex()
212 Path basePath = repo.getLocalPath();
213 Path artifactFile = basePath.resolve(
214 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
216 ArtifactIndexingTask task =
217 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
218 repo.getIndexingContext() );
220 // add artifact to index
221 indexingExecutor.executeTask( task );
223 BooleanQuery.Builder qb = new BooleanQuery.Builder();
224 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
225 BooleanClause.Occur.SHOULD );
227 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
230 IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
231 FlatSearchRequest flatSearchRequest =
232 new FlatSearchRequest( qb.build(), ctx );
234 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
236 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
237 assertTrue( Files.exists(basePath.resolve( ".index" )) );
239 // should return 1 hit
240 assertEquals( 1, response.getTotalHitsCount() );
242 // remove added artifact from index
243 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
244 repo.getIndexingContext());
245 indexingExecutor.executeTask( task );
247 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
248 repo.getIndexingContext() );
249 indexingExecutor.executeTask( task );
251 qb = new BooleanQuery.Builder();
252 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
253 BooleanClause.Occur.SHOULD );
254 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
255 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
256 BooleanClause.Occur.SHOULD );
258 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
259 assertTrue( Files.exists(basePath.resolve(".index" )) );
261 flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
263 response = indexer.searchFlat( flatSearchRequest );
264 // artifact should have been removed from the index!
265 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
267 // TODO: test it was removed from the packaged index also
271 public void testPackagedIndex()
275 Path basePath = repo.getLocalPath();
276 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
277 Path packedIndexDirectory = icf.getLocalPackedIndexPath();
278 Path indexerDirectory = icf.getLocalIndexPath();
280 Files.list(packedIndexDirectory).filter( path -> path.getFileName().toString().startsWith("nexus-maven-repository-index") )
285 System.err.println("Deleting "+path);
286 Files.delete( path );
288 catch ( IOException e )
290 e.printStackTrace( );
295 Path artifactFile = basePath.resolve(
296 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
297 ArtifactIndexingTask task =
298 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
299 repo.getIndexingContext() );
300 task.setExecuteOnEntireRepo( false );
302 indexingExecutor.executeTask( task );
304 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
305 repo.getIndexingContext() );
307 task.setExecuteOnEntireRepo( false );
309 indexingExecutor.executeTask( task );
311 assertTrue( Files.exists(packedIndexDirectory) );
312 assertTrue( Files.exists(indexerDirectory) );
314 // test packed index file creation
316 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
317 Assertions.assertThat( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.properties" ) ));
318 Assertions.assertThat( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.gz" ) ));
319 assertFalse( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.1.gz" ) ));
322 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
324 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.toFile() );
325 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
326 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
327 indexUpdater.fetchAndUpdateIndex( updateRequest );
329 BooleanQuery.Builder qb = new BooleanQuery.Builder();
330 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
331 BooleanClause.Occur.SHOULD );
333 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
334 BooleanClause.Occur.SHOULD );
336 FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
337 FlatSearchResponse response = indexer.searchFlat( request );
339 assertEquals( 1, response.getTotalHitsCount() );
340 Set<ArtifactInfo> results = response.getResults();
342 ArtifactInfo artifactInfo = results.iterator().next();
343 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
344 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
345 assertEquals( "test-repo", artifactInfo.getRepository() );