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.common.plexusbridge.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.apache.lucene.search.BooleanClause.Occur;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.TopDocs;
32 import org.apache.maven.index.ArtifactInfo;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.apache.maven.index.expr.SourcedSearchExpression;
39 import org.apache.maven.index.expr.StringSearchExpression;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.test.context.ContextConfiguration;
46 import javax.inject.Inject;
47 import java.io.BufferedInputStream;
48 import java.io.BufferedOutputStream;
50 import java.io.FileInputStream;
51 import java.io.FileOutputStream;
52 import java.io.FilenameFilter;
53 import java.io.IOException;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
59 * ArchivaIndexingTaskExecutorTest
61 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
62 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
63 public class ArchivaIndexingTaskExecutorTest
67 private ArchivaIndexingTaskExecutor indexingExecutor;
69 private ManagedRepository repositoryConfig;
71 private NexusIndexer indexer;
74 PlexusSisuBridge plexusSisuBridge;
77 MavenIndexerUtils mavenIndexerUtils;
80 ManagedRepositoryAdmin managedRepositoryAdmin;
89 repositoryConfig = new ManagedRepository();
90 repositoryConfig.setId( "test-repo" );
91 repositoryConfig.setLocation(
92 new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
93 repositoryConfig.setLayout( "default" );
94 repositoryConfig.setName( "Test Repository" );
95 repositoryConfig.setScanned( true );
96 repositoryConfig.setSnapshots( false );
97 repositoryConfig.setReleases( true );
99 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
101 managedRepositoryAdmin.createIndexContext( repositoryConfig );
106 public void tearDown()
110 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
112 indexer.removeIndexingContext( indexingContext, true );
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()
130 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
134 public void testAddArtifactToIndex()
137 File artifactFile = new File( repositoryConfig.getLocation(),
138 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
140 ArtifactIndexingTask task =
141 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
142 getIndexingContext() );
144 indexingExecutor.executeTask( task );
146 BooleanQuery q = new BooleanQuery();
147 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
150 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
153 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
155 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
156 new File( repositoryConfig.getLocation() ),
157 new File( repositoryConfig.getLocation(),
158 ".indexer" ), null, null,
159 mavenIndexerUtils.getAllIndexCreators() );
160 context.setSearchable( true );
163 FlatSearchRequest request = new FlatSearchRequest( q );
164 FlatSearchResponse response = indexer.searchFlat( request );
166 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
167 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
168 assertEquals( 1, response.getTotalHits() );
170 Set<ArtifactInfo> results = response.getResults();
172 ArtifactInfo artifactInfo = results.iterator().next();
173 assertEquals( "org.apache.archiva", artifactInfo.groupId );
174 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
175 assertEquals( "test-repo", artifactInfo.repository );
180 public void testUpdateArtifactInIndex()
183 File artifactFile = new File( repositoryConfig.getLocation(),
184 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
186 ArtifactIndexingTask task =
187 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
188 getIndexingContext() );
190 indexingExecutor.executeTask( task );
191 indexingExecutor.executeTask( task );
193 BooleanQuery q = new BooleanQuery();
194 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
197 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
200 IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
202 IndexSearcher searcher = ctx.acquireIndexSearcher();
203 TopDocs topDocs = searcher.search( q, null, 10 );
206 ctx.releaseIndexSearcher( searcher );
208 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
209 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
211 // should only return 1 hit!
212 assertEquals( 1, topDocs.totalHits );
216 public void testRemoveArtifactFromIndex()
219 File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
224 getIndexingContext() );
226 // add artifact to index
227 indexingExecutor.executeTask( task );
229 BooleanQuery q = new BooleanQuery();
230 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
233 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
236 FlatSearchRequest flatSearchRequest =
237 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
239 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
241 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
242 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
244 // should return 1 hit
245 assertEquals( 1, response.getTotalHitsCount() );
247 // remove added artifact from index
248 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
249 getIndexingContext() );
250 indexingExecutor.executeTask( task );
252 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
253 getIndexingContext() );
254 indexingExecutor.executeTask( task );
256 q = new BooleanQuery();
257 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
259 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
260 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
263 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
264 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
266 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
268 response = indexer.searchFlat( flatSearchRequest );
269 // artifact should have been removed from the index!
270 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
272 // TODO: test it was removed from the packaged index also
276 public void testPackagedIndex()
280 File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
282 indexerDirectory.listFiles( new FilenameFilter()
285 public boolean accept( File file, String s )
287 if ( s.startsWith( "nexus-maven-repository-index" ) )
289 new File( file, s ).delete();
295 File artifactFile = new File( repositoryConfig.getLocation(),
296 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
298 ArtifactIndexingTask task =
299 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
300 getIndexingContext() );
301 task.setExecuteOnEntireRepo( false );
303 indexingExecutor.executeTask( task );
305 task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
306 getIndexingContext() );
308 task.setExecuteOnEntireRepo( false );
310 indexingExecutor.executeTask( task );
312 assertTrue( indexerDirectory.exists() );
314 // test packed index file creation
315 assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.zip" ).exists() );
316 assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.properties" ).exists() );
317 assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.gz" ).exists() );
320 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
321 unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
323 BooleanQuery q = new BooleanQuery();
324 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
327 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
330 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
331 FlatSearchResponse response = indexer.searchFlat( request );
333 Set<ArtifactInfo> results = response.getResults();
335 ArtifactInfo artifactInfo = results.iterator().next();
336 assertEquals( "org.apache.archiva", artifactInfo.groupId );
337 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
338 assertEquals( "test-repo", artifactInfo.repository );
340 assertEquals( 1, response.getTotalHits() );
343 private void unzipIndex( String indexDir, String destDir )
346 final int buff = 2048;
348 new File( destDir ).mkdirs();
350 BufferedOutputStream out = null;
351 FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
352 ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
355 while ( ( entry = in.getNextEntry() ) != null )
358 byte data[] = new byte[buff];
359 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
360 out = new BufferedOutputStream( fout, buff );
362 while ( ( count = in.read( data, 0, buff ) ) != -1 )
364 out.write( data, 0, count );