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.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.lucene.search.BooleanClause.Occur;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.lucene.search.IndexSearcher;
29 import org.apache.lucene.search.TopDocs;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
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;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import javax.inject.Inject;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.FileOutputStream;
54 import java.io.IOException;
56 import java.util.zip.ZipEntry;
57 import java.util.zip.ZipInputStream;
60 * ArchivaIndexingTaskExecutorTest
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
64 public class ArchivaIndexingTaskExecutorTest
68 private ArchivaIndexingTaskExecutor indexingExecutor;
70 private ManagedRepositoryConfiguration repositoryConfig;
72 private Configuration configuration;
74 private NexusIndexer indexer;
77 PlexusSisuBridge plexusSisuBridge;
80 MavenIndexerUtils mavenIndexerUtils;
88 repositoryConfig = new ManagedRepositoryConfiguration();
89 repositoryConfig.setId( "test-repo" );
90 repositoryConfig.setLocation( "target/test-classes/test-repo" );
91 repositoryConfig.setLayout( "default" );
92 repositoryConfig.setName( "Test Repository" );
93 repositoryConfig.setScanned( true );
94 repositoryConfig.setSnapshots( false );
95 repositoryConfig.setReleases( true );
97 configuration = new Configuration();
98 configuration.addManagedRepository( repositoryConfig );
100 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
102 ArtifactIndexingTask.createContext( repositoryConfig, indexer, mavenIndexerUtils.getAllIndexCreators() );
106 public void tearDown()
110 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
112 indexer.removeIndexingContext( indexingContext, true );
115 // delete created index in the repository
116 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
117 FileUtils.deleteDirectory( indexDir );
118 assertFalse( indexDir.exists() );
120 indexDir = new File( repositoryConfig.getLocation(), ".index" );
121 FileUtils.deleteDirectory( indexDir );
122 assertFalse( indexDir.exists() );
127 protected IndexingContext getIndexingContext()
129 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
133 public void testAddArtifactToIndex()
136 File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
141 getIndexingContext() );
143 indexingExecutor.executeTask( task );
145 BooleanQuery q = new BooleanQuery();
146 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
149 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
152 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
154 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
155 new File( repositoryConfig.getLocation() ),
156 new File( repositoryConfig.getLocation(),
157 ".indexer" ), null, null,
158 mavenIndexerUtils.getAllIndexCreators() );
159 context.setSearchable( true );
162 FlatSearchRequest request = new FlatSearchRequest( q );
163 FlatSearchResponse response = indexer.searchFlat( request );
165 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
166 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
167 assertEquals( 1, response.getTotalHits() );
169 Set<ArtifactInfo> results = response.getResults();
171 ArtifactInfo artifactInfo = results.iterator().next();
172 assertEquals( "org.apache.archiva", artifactInfo.groupId );
173 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
174 assertEquals( "test-repo", artifactInfo.repository );
179 public void testUpdateArtifactInIndex()
182 File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
187 getIndexingContext() );
189 indexingExecutor.executeTask( task );
190 indexingExecutor.executeTask( task );
192 BooleanQuery q = new BooleanQuery();
193 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
196 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
199 IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
200 TopDocs topDocs = searcher.search( q, null, 10 );
204 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
205 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
207 // should only return 1 hit!
208 assertEquals( 1, topDocs.totalHits );
212 public void testRemoveArtifactFromIndex()
215 File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
220 getIndexingContext() );
222 // add artifact to index
223 indexingExecutor.executeTask( task );
225 BooleanQuery q = new BooleanQuery();
226 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
229 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
232 FlatSearchRequest flatSearchRequest =
233 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
235 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
237 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
238 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
240 // should return 1 hit
241 assertEquals( 1, response.getTotalHitsCount() );
243 // remove added artifact from index
244 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
245 getIndexingContext() );
246 indexingExecutor.executeTask( task );
248 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
249 getIndexingContext() );
250 indexingExecutor.executeTask( task );
252 q = new BooleanQuery();
253 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
255 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
256 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
259 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
260 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
262 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
264 response = indexer.searchFlat( flatSearchRequest );
265 // artifact should have been removed from the index!
266 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
268 // TODO: test it was removed from the packaged index also
272 public void testPackagedIndex()
275 File artifactFile = new File( repositoryConfig.getLocation(),
276 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
278 ArtifactIndexingTask task =
279 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
280 getIndexingContext() );
281 task.setExecuteOnEntireRepo( false );
283 indexingExecutor.executeTask( task );
285 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
286 getIndexingContext() );
288 task.setExecuteOnEntireRepo( false );
290 indexingExecutor.executeTask( task );
292 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
293 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
296 File destDir = new File( repositoryConfig.getLocation(), ".index/tmp" );
297 unzipIndex( new File( repositoryConfig.getLocation(), ".index" ).getPath(), destDir.getPath() );
299 BooleanQuery q = new BooleanQuery();
300 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
303 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
306 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
307 FlatSearchResponse response = indexer.searchFlat( request );
309 Set<ArtifactInfo> results = response.getResults();
311 ArtifactInfo artifactInfo = results.iterator().next();
312 assertEquals( "org.apache.archiva", artifactInfo.groupId );
313 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
314 assertEquals( "test-repo", artifactInfo.repository );
316 assertEquals( 1, response.getTotalHits() );
319 private void unzipIndex( String indexDir, String destDir )
320 throws FileNotFoundException, IOException
322 final int buff = 2048;
324 new File( destDir ).mkdirs();
326 BufferedOutputStream out = null;
327 FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
328 ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
331 while ( ( entry = in.getNextEntry() ) != null )
334 byte data[] = new byte[buff];
335 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
336 out = new BufferedOutputStream( fout, buff );
338 while ( ( count = in.read( data, 0, buff ) ) != -1 )
340 out.write( data, 0, count );