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;
50 import java.io.IOException;
51 import java.nio.file.Files;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.util.List;
57 * @author Olivier Lamy
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
61 public abstract class AbstractMavenRepositorySearch
65 protected Logger log = LoggerFactory.getLogger( getClass() );
67 public static String TEST_REPO_1 = "maven-search-test-repo";
69 public static String TEST_REPO_2 = "maven-search-test-repo-2";
72 public static String REPO_RELEASE = "repo-release";
74 MavenRepositorySearch search;
76 ArchivaConfiguration archivaConfig;
79 ArtifactContextProducer artifactContextProducer;
81 IMocksControl archivaConfigControl;
86 PlexusSisuBridge plexusSisuBridge;
89 List<IndexCreator> indexCreators;
92 NexusIndexer nexusIndexer;
95 QueryCreator queryCreator;
104 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
105 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
107 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
108 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
110 archivaConfigControl = EasyMock.createControl();
112 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
114 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
115 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
117 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
118 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
120 search = new MavenRepositorySearch( nexusIndexer, defaultManagedRepositoryAdmin, defaultProxyConnectorAdmin,
123 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
124 defaultManagedRepositoryAdmin.setIndexCreators( indexCreators );
126 config = new Configuration();
127 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
128 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
129 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
134 public void tearDown()
137 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
139 nexusIndexer.removeIndexingContext( indexingContext, true );
142 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
143 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
145 FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
146 assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
151 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
153 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
154 repositoryConfig.setId( repository );
155 repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
156 Path f = Paths.get( repositoryConfig.getLocation() );
157 if ( !Files.exists(f) )
161 Files.createDirectories( f );
163 catch ( IOException e )
165 log.error("Could not create directories for {}", f);
168 repositoryConfig.setLayout( "default" );
169 repositoryConfig.setName( repository );
170 repositoryConfig.setScanned( true );
171 repositoryConfig.setSnapshots( false );
172 repositoryConfig.setReleases( true );
174 return repositoryConfig;
177 protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan )
181 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
183 if ( context != null )
185 nexusIndexer.removeIndexingContext( context, true );
188 Path indexerDirectory = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer" );
190 if ( Files.exists(indexerDirectory) )
192 FileUtils.deleteDirectory( indexerDirectory );
195 assertFalse( Files.exists(indexerDirectory) );
197 Path lockFile = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
198 if ( Files.exists(lockFile) )
200 Files.delete(lockFile);
203 assertFalse( Files.exists(lockFile) );
205 Path repo = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository );
206 assertTrue( Files.exists(repo) );
207 Path indexDirectory =
208 Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
209 indexDirectory.toFile().deleteOnExit();
210 FileUtils.deleteDirectory( indexDirectory );
212 context = nexusIndexer.addIndexingContext( repository, repository, repo.toFile(), indexDirectory.toFile(),
213 repo.toUri().toURL().toExternalForm(),
214 indexDirectory.toUri().toURL().toString(), indexCreators );
216 // minimize datas in memory
217 // context.getIndexWriter().setMaxBufferedDocs( -1 );
218 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
219 for ( Path artifactFile : filesToBeIndexed )
221 assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
222 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
224 if ( artifactFile.toString().endsWith( ".pom" ) )
226 ac.getArtifactInfo().setFileExtension( "pom" );
227 ac.getArtifactInfo().setPackaging( "pom" );
228 ac.getArtifactInfo().setClassifier( "pom" );
230 nexusIndexer.addArtifactToIndex( ac, context );
231 context.updateTimestamp( true );
236 nexusIndexer.scan( context, new ArtifactScanListener(), false );
239 context.getIndexWriter().commit();
240 context.setSearchable( true );
244 static class ArtifactScanListener
245 implements ArtifactScanningListener
247 protected Logger log = LoggerFactory.getLogger( getClass() );
250 public void scanningStarted( IndexingContext ctx )
256 public void scanningFinished( IndexingContext ctx, ScanningResult result )
262 public void artifactError( ArtifactContext ac, Exception e )
264 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
268 public void artifactDiscovered( ArtifactContext ac )
270 log.debug( "artifactDiscovered {}:{}", //
271 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
272 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
276 public String niceDisplay( SearchResults searchResults )
279 StringBuilder sb = new StringBuilder();
280 for ( SearchResultHit hit : searchResults.getHits() )
282 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
284 return sb.toString();