1 package org.apache.archiva.indexer.maven.search;
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.utils.FileUtils;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.ConfigurationListener;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.indexer.ArchivaIndexingContext;
29 import org.apache.archiva.indexer.search.SearchResultHit;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.proxy.ProxyRegistry;
32 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
33 import org.apache.archiva.repository.Repository;
34 import org.apache.archiva.repository.base.ConfigurationHandler;
35 import org.apache.archiva.repository.base.RepositoryGroupHandler;
36 import org.apache.archiva.repository.features.IndexCreationFeature;
37 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
38 import org.apache.commons.lang3.SystemUtils;
39 import org.apache.maven.index.ArtifactContext;
40 import org.apache.maven.index.ArtifactContextProducer;
41 import org.apache.maven.index.ArtifactScanningListener;
42 import org.apache.maven.index.DefaultScannerListener;
43 import org.apache.maven.index.Indexer;
44 import org.apache.maven.index.IndexerEngine;
45 import org.apache.maven.index.QueryCreator;
46 import org.apache.maven.index.Scanner;
47 import org.apache.maven.index.ScanningRequest;
48 import org.apache.maven.index.ScanningResult;
49 import org.apache.maven.index.context.IndexingContext;
50 import org.easymock.EasyMock;
51 import org.easymock.IMocksControl;
52 import org.junit.After;
53 import org.junit.Before;
54 import org.junit.runner.RunWith;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.test.context.ContextConfiguration;
59 import javax.inject.Inject;
60 import java.io.IOException;
61 import java.nio.file.Files;
62 import java.nio.file.Path;
63 import java.nio.file.Paths;
64 import java.util.List;
65 import java.util.Locale;
67 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
70 * @author Olivier Lamy
72 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
73 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
74 public abstract class AbstractMavenRepositorySearch
78 protected Logger log = LoggerFactory.getLogger( getClass() );
80 public static String TEST_REPO_1 = "maven-search-test-repo";
82 public static String TEST_REPO_2 = "maven-search-test-repo-2";
85 public static String REPO_RELEASE = "repo-release";
87 MavenRepositorySearch search;
89 ArchivaConfiguration archivaConfig;
92 ArtifactContextProducer artifactContextProducer;
95 ArchivaRepositoryRegistry repositoryRegistry;
98 RepositoryGroupHandler repositoryGroupHandler;
101 ProxyRegistry proxyRegistry;
104 private IndexerEngine indexerEngine;
106 IMocksControl archivaConfigControl;
108 Configuration config;
117 QueryCreator queryCreator;
126 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
127 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
129 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
130 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
132 archivaConfigControl = EasyMock.createControl();
134 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
136 repositoryRegistry.setArchivaConfiguration( archivaConfig );
138 search = new MavenRepositorySearch( indexer, repositoryRegistry, proxyRegistry,
141 assertNotNull( repositoryRegistry );
143 config = new Configuration();
144 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
145 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
146 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
148 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
149 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
150 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
151 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
152 EasyMock.expectLastCall().anyTimes();
153 archivaConfigControl.replay();
154 repositoryRegistry.reload();
160 public void tearDown()
163 archivaConfigControl.reset();
164 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
165 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
166 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
167 EasyMock.expectLastCall().anyTimes();
168 archivaConfigControl.replay();
169 repositoryRegistry.removeRepository(TEST_REPO_1);
170 repositoryRegistry.removeRepository(TEST_REPO_2);
171 repositoryRegistry.removeRepository(REPO_RELEASE);
172 repositoryRegistry.destroy();
173 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
174 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
176 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
177 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
182 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
184 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
185 repositoryConfig.setId( repository );
186 repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
187 Path f = Paths.get( repositoryConfig.getLocation() );
188 if ( !Files.exists(f) )
192 Files.createDirectories( f );
194 catch ( IOException e )
196 log.error("Could not create directories for {}", f);
199 repositoryConfig.setLayout( "default" );
200 repositoryConfig.setName( repository );
201 repositoryConfig.setScanned( true );
202 repositoryConfig.setSnapshots( false );
203 repositoryConfig.setReleases( true );
204 repositoryConfig.setIndexDir(DEFAULT_INDEX_PATH);
206 return repositoryConfig;
209 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan) throws Exception {
210 createIndex(repository, filesToBeIndexed, scan, null, true);
213 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles)
216 Repository rRepo = repositoryRegistry.getRepository(repository);
217 IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
220 ArchivaIndexingContext archivaCtx = rRepo.getIndexingContext();
221 IndexingContext context = archivaCtx.getBaseContext(IndexingContext.class);
223 if ( archivaCtx != null )
225 archivaCtx.close(true);
228 Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
230 Path indexerDirectory = repoDir.resolve(".indexer" );
232 if ( Files.exists(indexerDirectory) )
234 FileUtils.deleteDirectory( indexerDirectory );
237 assertFalse( Files.exists(indexerDirectory) );
239 Path lockFile = repoDir.resolve(".indexer/write.lock" );
240 if ( Files.exists(lockFile) )
242 Files.delete(lockFile);
244 assertFalse( Files.exists(lockFile) );
245 if (indexDir==null) {
246 Path indexDirectory =
247 Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
248 indexDirectory.toFile().deleteOnExit();
249 FileUtils.deleteDirectory(indexDirectory);
250 icf.setIndexPath(indexDirectory.toUri());
253 icf.setIndexPath(indexDir.toUri());
256 Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
257 assertTrue(Files.exists(repo));
258 org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
264 archivaConfigControl.reset();
265 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
266 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
267 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
268 EasyMock.expectLastCall().anyTimes();
269 archivaConfigControl.replay();
270 repositoryRegistry.reload();
271 archivaConfigControl.reset();
273 rRepo = repositoryRegistry.getRepository(repository);
274 icf = rRepo.getFeature(IndexCreationFeature.class).get();
277 archivaCtx = rRepo.getIndexingContext();
278 context = archivaCtx.getBaseContext(IndexingContext.class);
281 // minimize datas in memory
282 // context.getIndexWriter().setMaxBufferedDocs( -1 );
283 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
284 for ( Path artifactFile : filesToBeIndexed )
286 assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
287 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
289 if ( artifactFile.toString().endsWith( ".pom" ) )
291 ac.getArtifactInfo().setFileExtension( "pom" );
292 ac.getArtifactInfo().setPackaging( "pom" );
293 ac.getArtifactInfo().setClassifier( "pom" );
295 indexer.addArtifactToIndex( ac, context );
296 context.updateTimestamp( true );
301 DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, new ArtifactScanListener());
302 ScanningRequest req = new ScanningRequest(context, listener );
308 // context.getIndexWriter().commit();
309 context.setSearchable( true );
313 static class ArtifactScanListener
314 implements ArtifactScanningListener
316 protected Logger log = LoggerFactory.getLogger( getClass() );
319 public void scanningStarted( IndexingContext ctx )
325 public void scanningFinished( IndexingContext ctx, ScanningResult result )
331 public void artifactError( ArtifactContext ac, Exception e )
333 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
337 public void artifactDiscovered( ArtifactContext ac )
339 log.debug( "artifactDiscovered {}:{}", //
340 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
341 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
345 public String niceDisplay( SearchResults searchResults )
348 StringBuilder sb = new StringBuilder();
349 for ( SearchResultHit hit : searchResults.getHits() )
351 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
353 return sb.toString();