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.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.maven.index.ArtifactInfo;
27 import org.apache.maven.index.FlatSearchRequest;
28 import org.apache.maven.index.FlatSearchResponse;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.NexusIndexer;
31 import org.apache.maven.index.context.IndexCreator;
32 import org.apache.maven.index.context.IndexingContext;
33 import org.apache.maven.index.expr.SourcedSearchExpression;
34 import org.apache.maven.index.expr.StringSearchExpression;
35 import org.apache.maven.index.shaded.lucene.search.BooleanClause;
36 import org.apache.maven.index.shaded.lucene.search.BooleanQuery;
37 import org.apache.maven.index.shaded.lucene.search.IndexSearcher;
38 import org.apache.maven.index.shaded.lucene.search.TopDocs;
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.assertj.core.api.Assertions;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.springframework.test.context.ContextConfiguration;
49 import javax.inject.Inject;
51 import java.io.FilenameFilter;
52 import java.util.List;
56 * ArchivaIndexingTaskExecutorTest
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public class ArchivaIndexingTaskExecutorTest
64 private ArchivaIndexingTaskExecutor indexingExecutor;
66 private ManagedRepository repositoryConfig;
69 private NexusIndexer indexer;
72 List<IndexCreator> indexCreators;
75 ManagedRepositoryAdmin managedRepositoryAdmin;
78 private IndexUpdater indexUpdater;
87 repositoryConfig = new ManagedRepository();
88 repositoryConfig.setId( "test-repo" );
89 repositoryConfig.setLocation(
90 new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
91 repositoryConfig.setLayout( "default" );
92 repositoryConfig.setName( "Test Repository" );
93 repositoryConfig.setScanned( true );
94 repositoryConfig.setSnapshots( false );
95 repositoryConfig.setReleases( true );
97 managedRepositoryAdmin.createIndexContext( repositoryConfig );
102 public void tearDown()
106 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
108 indexer.removeIndexingContext( indexingContext, true );
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()
126 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
130 public void testAddArtifactToIndex()
133 File artifactFile = new File( repositoryConfig.getLocation(),
134 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
136 ArtifactIndexingTask task =
137 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
138 getIndexingContext() );
140 indexingExecutor.executeTask( task );
142 BooleanQuery q = new BooleanQuery();
143 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
144 BooleanClause.Occur.SHOULD );
146 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
147 BooleanClause.Occur.SHOULD );
149 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
151 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), //
152 repositoryConfig.getId(), //
153 new File( repositoryConfig.getLocation() ), //
154 new File( repositoryConfig.getLocation(), ".indexer" )
156 , null, null, indexCreators );
157 context.setSearchable( true );
160 FlatSearchRequest request = new FlatSearchRequest( q );
161 FlatSearchResponse response = indexer.searchFlat( request );
163 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
164 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
165 assertEquals( 1, response.getTotalHits() );
167 Set<ArtifactInfo> results = response.getResults();
169 ArtifactInfo artifactInfo = results.iterator().next();
170 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
171 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
172 assertEquals( "test-repo", artifactInfo.getRepository() );
177 public void testUpdateArtifactInIndex()
180 File artifactFile = new File( repositoryConfig.getLocation(),
181 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183 ArtifactIndexingTask task =
184 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
185 getIndexingContext() );
187 indexingExecutor.executeTask( task );
188 indexingExecutor.executeTask( task );
190 BooleanQuery q = new BooleanQuery();
191 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
192 BooleanClause.Occur.SHOULD );
194 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
195 BooleanClause.Occur.SHOULD );
197 IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
199 IndexSearcher searcher = ctx.acquireIndexSearcher();
200 TopDocs topDocs = searcher.search( q, null, 10 );
203 ctx.releaseIndexSearcher( searcher );
205 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
206 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
208 // should only return 1 hit!
209 assertEquals( 1, topDocs.totalHits );
213 public void testRemoveArtifactFromIndex()
216 File artifactFile = new File( repositoryConfig.getLocation(),
217 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
219 ArtifactIndexingTask task =
220 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
221 getIndexingContext() );
223 // add artifact to index
224 indexingExecutor.executeTask( task );
226 BooleanQuery q = new BooleanQuery();
227 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
228 BooleanClause.Occur.SHOULD );
230 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
233 FlatSearchRequest flatSearchRequest =
234 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
236 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
238 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
239 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
241 // should return 1 hit
242 assertEquals( 1, response.getTotalHitsCount() );
244 // remove added artifact from index
245 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
246 getIndexingContext() );
247 indexingExecutor.executeTask( task );
249 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
250 getIndexingContext() );
251 indexingExecutor.executeTask( task );
253 q = new BooleanQuery();
254 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
255 BooleanClause.Occur.SHOULD );
256 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
257 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
258 BooleanClause.Occur.SHOULD );
260 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
261 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
263 flatSearchRequest = new FlatSearchRequest( q, 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 File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
279 indexerDirectory.listFiles( new FilenameFilter()
282 public boolean accept( File file, String s )
284 if ( s.startsWith( "nexus-maven-repository-index" ) )
286 new File( file, s ).delete();
292 File artifactFile = new File( repositoryConfig.getLocation(),
293 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
295 ArtifactIndexingTask task =
296 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
297 getIndexingContext() );
298 task.setExecuteOnEntireRepo( false );
300 indexingExecutor.executeTask( task );
302 task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
303 getIndexingContext() );
305 task.setExecuteOnEntireRepo( false );
307 indexingExecutor.executeTask( task );
309 assertTrue( indexerDirectory.exists() );
311 // test packed index file creation
313 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
314 Assertions.assertThat( new File( indexerDirectory, "nexus-maven-repository-index.properties" ) ).exists();
315 Assertions.assertThat( new File( indexerDirectory, "nexus-maven-repository-index.gz" ) ).exists();
318 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
319 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
321 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( indexerDirectory );
322 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
323 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
324 indexUpdater.fetchAndUpdateIndex( updateRequest );
326 BooleanQuery q = new BooleanQuery();
327 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
328 BooleanClause.Occur.SHOULD );
330 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
331 BooleanClause.Occur.SHOULD );
333 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
334 FlatSearchResponse response = indexer.searchFlat( request );
336 Set<ArtifactInfo> results = response.getResults();
338 ArtifactInfo artifactInfo = results.iterator().next();
339 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
340 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
341 assertEquals( "test-repo", artifactInfo.getRepository() );
343 assertEquals( 1, response.getTotalHits() );