1 package org.apache.archiva.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
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.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.utils.FileUtils;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.commons.lang.SystemUtils;
32 import org.apache.maven.index.ArtifactContext;
33 import org.apache.maven.index.ArtifactContextProducer;
34 import org.apache.maven.index.ArtifactScanningListener;
35 import org.apache.maven.index.NexusIndexer;
36 import org.apache.maven.index.QueryCreator;
37 import org.apache.maven.index.ScanningResult;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.easymock.EasyMock;
41 import org.easymock.IMocksControl;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.runner.RunWith;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.test.context.ContextConfiguration;
49 import javax.inject.Inject;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.List;
58 * @author Olivier Lamy
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
62 public abstract class AbstractMavenRepositorySearch
66 protected Logger log = LoggerFactory.getLogger( getClass() );
68 public static String TEST_REPO_1 = "maven-search-test-repo";
70 public static String TEST_REPO_2 = "maven-search-test-repo-2";
73 public static String REPO_RELEASE = "repo-release";
75 MavenRepositorySearch search;
77 ArchivaConfiguration archivaConfig;
80 ArtifactContextProducer artifactContextProducer;
82 IMocksControl archivaConfigControl;
87 PlexusSisuBridge plexusSisuBridge;
90 List<IndexCreator> indexCreators;
93 NexusIndexer nexusIndexer;
96 QueryCreator queryCreator;
105 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
106 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
108 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
109 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
111 archivaConfigControl = EasyMock.createControl();
113 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
115 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
116 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
118 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
119 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
121 search = new MavenRepositorySearch( nexusIndexer, defaultManagedRepositoryAdmin, defaultProxyConnectorAdmin,
124 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
125 defaultManagedRepositoryAdmin.setIndexCreators( indexCreators );
127 config = new Configuration();
128 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
129 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
130 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
135 public void tearDown()
138 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
140 nexusIndexer.removeIndexingContext( indexingContext, true );
143 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
144 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
146 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
147 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
152 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
154 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
155 repositoryConfig.setId( repository );
156 repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
157 Path f = Paths.get( repositoryConfig.getLocation() );
158 if ( !Files.exists(f) )
162 Files.createDirectories( f );
164 catch ( IOException e )
166 log.error("Could not create directories for {}", f);
169 repositoryConfig.setLayout( "default" );
170 repositoryConfig.setName( repository );
171 repositoryConfig.setScanned( true );
172 repositoryConfig.setSnapshots( false );
173 repositoryConfig.setReleases( true );
175 return repositoryConfig;
178 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan )
182 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
184 if ( context != null )
186 nexusIndexer.removeIndexingContext( context, true );
189 Path indexerDirectory = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer" );
191 if ( Files.exists(indexerDirectory) )
193 FileUtils.deleteDirectory( indexerDirectory );
196 assertFalse( Files.exists(indexerDirectory) );
198 Path lockFile = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
199 if ( Files.exists(lockFile) )
201 Files.delete(lockFile);
204 assertFalse( Files.exists(lockFile) );
206 Path repo = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository );
207 assertTrue( Files.exists(repo) );
208 Path indexDirectory =
209 Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
210 indexDirectory.toFile().deleteOnExit();
211 FileUtils.deleteDirectory( indexDirectory );
213 context = nexusIndexer.addIndexingContext( repository, repository, repo.toFile(), indexDirectory.toFile(),
214 repo.toUri().toURL().toExternalForm(),
215 indexDirectory.toUri().toURL().toString(), indexCreators );
217 // minimize datas in memory
218 // context.getIndexWriter().setMaxBufferedDocs( -1 );
219 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
220 for ( Path artifactFile : filesToBeIndexed )
222 assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
223 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
225 if ( artifactFile.toString().endsWith( ".pom" ) )
227 ac.getArtifactInfo().setFileExtension( "pom" );
228 ac.getArtifactInfo().setPackaging( "pom" );
229 ac.getArtifactInfo().setClassifier( "pom" );
231 nexusIndexer.addArtifactToIndex( ac, context );
232 context.updateTimestamp( true );
237 nexusIndexer.scan( context, new ArtifactScanListener(), false );
240 context.getIndexWriter().commit();
241 context.setSearchable( true );
245 static class ArtifactScanListener
246 implements ArtifactScanningListener
248 protected Logger log = LoggerFactory.getLogger( getClass() );
251 public void scanningStarted( IndexingContext ctx )
257 public void scanningFinished( IndexingContext ctx, ScanningResult result )
263 public void artifactError( ArtifactContext ac, Exception e )
265 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
269 public void artifactDiscovered( ArtifactContext ac )
271 log.debug( "artifactDiscovered {}:{}", //
272 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
273 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
277 public String niceDisplay( SearchResults searchResults )
280 StringBuilder sb = new StringBuilder();
281 for ( SearchResultHit hit : searchResults.getHits() )
283 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
285 return sb.toString();