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.features.IndexCreationFeature;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.commons.lang3.SystemUtils;
37 import org.apache.maven.index.ArtifactContext;
38 import org.apache.maven.index.ArtifactContextProducer;
39 import org.apache.maven.index.ArtifactScanningListener;
40 import org.apache.maven.index.DefaultScannerListener;
41 import org.apache.maven.index.Indexer;
42 import org.apache.maven.index.IndexerEngine;
43 import org.apache.maven.index.QueryCreator;
44 import org.apache.maven.index.Scanner;
45 import org.apache.maven.index.ScanningRequest;
46 import org.apache.maven.index.ScanningResult;
47 import org.apache.maven.index.context.IndexingContext;
48 import org.easymock.EasyMock;
49 import org.easymock.IMocksControl;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.runner.RunWith;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.test.context.ContextConfiguration;
57 import javax.inject.Inject;
58 import java.io.IOException;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
62 import java.util.List;
63 import java.util.Locale;
65 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
68 * @author Olivier Lamy
70 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
71 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
72 public abstract class AbstractMavenRepositorySearch
76 protected Logger log = LoggerFactory.getLogger( getClass() );
78 public static String TEST_REPO_1 = "maven-search-test-repo";
80 public static String TEST_REPO_2 = "maven-search-test-repo-2";
83 public static String REPO_RELEASE = "repo-release";
85 MavenRepositorySearch search;
87 ArchivaConfiguration archivaConfig;
90 ArtifactContextProducer artifactContextProducer;
93 ArchivaRepositoryRegistry repositoryRegistry;
96 ProxyRegistry proxyRegistry;
99 private IndexerEngine indexerEngine;
101 IMocksControl archivaConfigControl;
103 Configuration config;
112 QueryCreator queryCreator;
121 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
122 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
124 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
125 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
127 archivaConfigControl = EasyMock.createControl();
129 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
131 repositoryRegistry.setArchivaConfiguration( archivaConfig );
133 search = new MavenRepositorySearch( indexer, repositoryRegistry, proxyRegistry,
136 assertNotNull( repositoryRegistry );
138 config = new Configuration();
139 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
140 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
141 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
143 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
144 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
145 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
146 archivaConfig.save(EasyMock.anyObject(Configuration.class));
147 EasyMock.expectLastCall().anyTimes();
148 archivaConfigControl.replay();
149 repositoryRegistry.reload();
155 public void tearDown()
158 archivaConfigControl.reset();
159 EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
160 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
161 archivaConfig.save(EasyMock.anyObject(Configuration.class));
162 EasyMock.expectLastCall().anyTimes();
163 archivaConfigControl.replay();
164 repositoryRegistry.removeRepository(TEST_REPO_1);
165 repositoryRegistry.removeRepository(TEST_REPO_2);
166 repositoryRegistry.removeRepository(REPO_RELEASE);
167 repositoryRegistry.destroy();
168 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
169 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
171 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
172 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
177 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
179 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
180 repositoryConfig.setId( repository );
181 repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
182 Path f = Paths.get( repositoryConfig.getLocation() );
183 if ( !Files.exists(f) )
187 Files.createDirectories( f );
189 catch ( IOException e )
191 log.error("Could not create directories for {}", f);
194 repositoryConfig.setLayout( "default" );
195 repositoryConfig.setName( repository );
196 repositoryConfig.setScanned( true );
197 repositoryConfig.setSnapshots( false );
198 repositoryConfig.setReleases( true );
199 repositoryConfig.setIndexDir(DEFAULT_INDEX_PATH);
201 return repositoryConfig;
204 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan) throws Exception {
205 createIndex(repository, filesToBeIndexed, scan, null, true);
208 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles)
211 Repository rRepo = repositoryRegistry.getRepository(repository);
212 IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
215 ArchivaIndexingContext archivaCtx = rRepo.getIndexingContext();
216 IndexingContext context = archivaCtx.getBaseContext(IndexingContext.class);
218 if ( archivaCtx != null )
220 archivaCtx.close(true);
223 Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
225 Path indexerDirectory = repoDir.resolve(".indexer" );
227 if ( Files.exists(indexerDirectory) )
229 FileUtils.deleteDirectory( indexerDirectory );
232 assertFalse( Files.exists(indexerDirectory) );
234 Path lockFile = repoDir.resolve(".indexer/write.lock" );
235 if ( Files.exists(lockFile) )
237 Files.delete(lockFile);
239 assertFalse( Files.exists(lockFile) );
240 if (indexDir==null) {
241 Path indexDirectory =
242 Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
243 indexDirectory.toFile().deleteOnExit();
244 FileUtils.deleteDirectory(indexDirectory);
245 icf.setIndexPath(indexDirectory.toUri());
248 icf.setIndexPath(indexDir.toUri());
251 Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
252 assertTrue(Files.exists(repo));
253 org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
259 archivaConfigControl.reset();
260 archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
261 EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
262 archivaConfig.save(EasyMock.anyObject(Configuration.class));
263 EasyMock.expectLastCall().anyTimes();
264 archivaConfigControl.replay();
265 repositoryRegistry.reload();
266 archivaConfigControl.reset();
268 rRepo = repositoryRegistry.getRepository(repository);
269 icf = rRepo.getFeature(IndexCreationFeature.class).get();
272 archivaCtx = rRepo.getIndexingContext();
273 context = archivaCtx.getBaseContext(IndexingContext.class);
276 // minimize datas in memory
277 // context.getIndexWriter().setMaxBufferedDocs( -1 );
278 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
279 for ( Path artifactFile : filesToBeIndexed )
281 assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
282 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
284 if ( artifactFile.toString().endsWith( ".pom" ) )
286 ac.getArtifactInfo().setFileExtension( "pom" );
287 ac.getArtifactInfo().setPackaging( "pom" );
288 ac.getArtifactInfo().setClassifier( "pom" );
290 indexer.addArtifactToIndex( ac, context );
291 context.updateTimestamp( true );
296 DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, new ArtifactScanListener());
297 ScanningRequest req = new ScanningRequest(context, listener );
303 // context.getIndexWriter().commit();
304 context.setSearchable( true );
308 static class ArtifactScanListener
309 implements ArtifactScanningListener
311 protected Logger log = LoggerFactory.getLogger( getClass() );
314 public void scanningStarted( IndexingContext ctx )
320 public void scanningFinished( IndexingContext ctx, ScanningResult result )
326 public void artifactError( ArtifactContext ac, Exception e )
328 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
332 public void artifactDiscovered( ArtifactContext ac )
334 log.debug( "artifactDiscovered {}:{}", //
335 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
336 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
340 public String niceDisplay( SearchResults searchResults )
343 StringBuilder sb = new StringBuilder();
344 for ( SearchResultHit hit : searchResults.getHits() )
346 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
348 return sb.toString();