1 package org.apache.archiva.maven.indexer.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.ConfigurationListener;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.indexer.ArchivaIndexingContext;
28 import org.apache.archiva.indexer.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.maven.indexer.search.MavenRepositorySearch;
31 import org.apache.archiva.proxy.ProxyRegistry;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
34 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
35 import org.apache.archiva.repository.features.IndexCreationFeature;
36 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import org.apache.commons.lang3.SystemUtils;
38 import org.apache.maven.index.ArtifactContext;
39 import org.apache.maven.index.ArtifactContextProducer;
40 import org.apache.maven.index.ArtifactScanningListener;
41 import org.apache.maven.index.DefaultScannerListener;
42 import org.apache.maven.index.Indexer;
43 import org.apache.maven.index.IndexerEngine;
44 import org.apache.maven.index.QueryCreator;
45 import org.apache.maven.index.Scanner;
46 import org.apache.maven.index.ScanningRequest;
47 import org.apache.maven.index.ScanningResult;
48 import org.apache.maven.index.context.IndexingContext;
49 import org.apache.maven.index_shaded.lucene.index.IndexUpgrader;
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;
97 @SuppressWarnings( "unused" )
99 RepositoryHandlerDependencies repositoryHandlerDependencies;
102 ProxyRegistry proxyRegistry;
105 private IndexerEngine indexerEngine;
107 IMocksControl archivaConfigControl;
109 Configuration config;
118 QueryCreator queryCreator;
127 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
128 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
129 Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
131 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
132 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
133 Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
135 archivaConfigControl = EasyMock.createControl();
137 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
139 repositoryRegistry.setArchivaConfiguration( archivaConfig );
141 search = new MavenRepositorySearch( indexer, repositoryRegistry, proxyRegistry,
144 assertNotNull( repositoryRegistry );
146 config = new Configuration();
147 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
148 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
149 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
151 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
152 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
153 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
154 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
155 EasyMock.expectLastCall().anyTimes();
156 archivaConfigControl.replay();
157 repositoryRegistry.reload();
163 public void tearDown()
166 archivaConfigControl.reset();
167 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
168 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
169 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
170 EasyMock.expectLastCall().anyTimes();
171 archivaConfigControl.replay();
172 repositoryRegistry.removeRepository(TEST_REPO_1);
173 repositoryRegistry.removeRepository(TEST_REPO_2);
174 repositoryRegistry.removeRepository(REPO_RELEASE);
175 repositoryRegistry.destroy();
176 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
177 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
179 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
180 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
185 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
187 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
188 repositoryConfig.setId( repository );
189 repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
190 Path f = Paths.get( repositoryConfig.getLocation() );
191 if ( !Files.exists(f) )
195 Files.createDirectories( f );
197 catch ( IOException e )
199 log.error("Could not create directories for {}", f);
202 repositoryConfig.setLayout( "default" );
203 repositoryConfig.setName( repository );
204 repositoryConfig.setScanned( true );
205 repositoryConfig.setSnapshots( false );
206 repositoryConfig.setReleases( true );
207 repositoryConfig.setIndexDir(DEFAULT_INDEX_PATH);
209 return repositoryConfig;
212 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan) throws Exception {
213 createIndex(repository, filesToBeIndexed, scan, null, true);
216 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles)
219 final Repository rRepo = repositoryRegistry.getRepository(repository);
220 IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
223 ArchivaIndexingContext archivaCtx = rRepo.getIndexingContext();
224 IndexingContext context = archivaCtx.getBaseContext(IndexingContext.class);
226 if ( archivaCtx != null )
228 archivaCtx.close(true);
231 Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
233 Path indexerDirectory = repoDir.resolve(".indexer" );
235 if ( indexDir == null && Files.exists(indexerDirectory) )
237 FileUtils.deleteDirectory( indexerDirectory );
238 assertFalse( Files.exists(indexerDirectory) );
242 Path lockFile = repoDir.resolve(".indexer/write.lock" );
243 if ( Files.exists(lockFile) )
245 Files.delete(lockFile);
247 assertFalse( Files.exists(lockFile) );
248 if (indexDir==null) {
249 Path indexDirectory =
250 Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
251 indexDirectory.toFile().deleteOnExit();
252 FileUtils.deleteDirectory(indexDirectory);
253 icf.setIndexPath(indexDirectory.toUri());
254 config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r ->
255 r.setIndexDir( indexDirectory.toAbsolutePath( ).toString( ) )
257 // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString()});
260 icf.setIndexPath(indexDir.toUri());
261 Files.createDirectories( indexDir );
262 config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r ->
263 r.setIndexDir( indexDir.toAbsolutePath( ).toString( ) )
265 IndexUpgrader.main(new String[]{indexDir.toAbsolutePath().toString()});
270 Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
271 assertTrue(Files.exists(repo));
272 org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
278 archivaConfigControl.reset();
279 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
280 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
281 archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
282 EasyMock.expectLastCall().anyTimes();
283 archivaConfigControl.replay();
284 repositoryRegistry.reload();
285 archivaConfigControl.reset();
287 Repository rRepo2 = repositoryRegistry.getRepository( repository );
288 icf = rRepo2.getFeature(IndexCreationFeature.class).get();
291 archivaCtx = rRepo2.getIndexingContext();
292 context = archivaCtx.getBaseContext(IndexingContext.class);
295 // minimize datas in memory
296 // context.getIndexWriter().setMaxBufferedDocs( -1 );
297 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
298 for ( Path artifactFile : filesToBeIndexed )
300 assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
301 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
303 if ( artifactFile.toString().endsWith( ".pom" ) )
305 ac.getArtifactInfo().setFileExtension( "pom" );
306 ac.getArtifactInfo().setPackaging( "pom" );
307 ac.getArtifactInfo().setClassifier( "pom" );
309 indexer.addArtifactToIndex( ac, context );
310 context.updateTimestamp( true );
315 DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, new ArtifactScanListener());
316 ScanningRequest req = new ScanningRequest(context, listener );
322 // context.getIndexWriter().commit();
323 context.setSearchable( true );
327 static class ArtifactScanListener
328 implements ArtifactScanningListener
330 protected Logger log = LoggerFactory.getLogger( getClass() );
333 public void scanningStarted( IndexingContext ctx )
339 public void scanningFinished( IndexingContext ctx, ScanningResult result )
345 public void artifactError( ArtifactContext ac, Exception e )
347 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
351 public void artifactDiscovered( ArtifactContext ac )
353 log.debug( "artifactDiscovered {}:{}", //
354 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
355 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
359 public String niceDisplay( SearchResults searchResults )
362 StringBuilder sb = new StringBuilder();
363 for ( SearchResultHit hit : searchResults.getHits() )
365 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
367 return sb.toString();