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.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.common.utils.FileUtil;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.Configuration;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang.SystemUtils;
34 import org.apache.maven.index.ArtifactContext;
35 import org.apache.maven.index.ArtifactContextProducer;
36 import org.apache.maven.index.ArtifactScanningListener;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.ScanningResult;
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.util.List;
54 * @author Olivier Lamy
56 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
57 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
58 public abstract class AbstractMavenRepositorySearch
62 protected Logger log = LoggerFactory.getLogger( getClass() );
64 public static String TEST_REPO_1 = "maven-search-test-repo";
66 public static String TEST_REPO_2 = "maven-search-test-repo-2";
69 public static String REPO_RELEASE = "repo-release";
71 MavenRepositorySearch search;
73 ArchivaConfiguration archivaConfig;
75 ArtifactContextProducer artifactContextProducer;
77 IMocksControl archivaConfigControl;
82 PlexusSisuBridge plexusSisuBridge;
85 MavenIndexerUtils mavenIndexerUtils;
87 NexusIndexer nexusIndexer;
96 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
97 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
99 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
100 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
102 archivaConfigControl = EasyMock.createControl( );
104 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
106 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
107 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
109 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
110 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
112 search = new MavenRepositorySearch( plexusSisuBridge, defaultManagedRepositoryAdmin, mavenIndexerUtils,
113 defaultProxyConnectorAdmin );
115 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
117 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
119 defaultManagedRepositoryAdmin.setMavenIndexerUtils( mavenIndexerUtils );
120 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
121 defaultManagedRepositoryAdmin.setIndexCreators( mavenIndexerUtils.getAllIndexCreators() );
123 config = new Configuration();
124 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
125 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
126 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
131 public void tearDown()
134 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
136 nexusIndexer.removeIndexingContext( indexingContext, true );
139 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
140 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
142 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
143 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
148 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
150 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
151 repositoryConfig.setId( repository );
152 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
153 File f = new File( repositoryConfig.getLocation() );
158 repositoryConfig.setLayout( "default" );
159 repositoryConfig.setName( repository );
160 repositoryConfig.setScanned( true );
161 repositoryConfig.setSnapshots( false );
162 repositoryConfig.setReleases( true );
164 return repositoryConfig;
167 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
171 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
173 if ( context != null )
175 nexusIndexer.removeIndexingContext( context, true );
178 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
180 if ( indexerDirectory.exists() )
182 FileUtils.deleteDirectory( indexerDirectory );
185 assertFalse( indexerDirectory.exists() );
187 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
188 if ( lockFile.exists() )
193 assertFalse( lockFile.exists() );
195 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
196 assertTrue( repo.exists() );
197 File indexDirectory =
198 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
199 indexDirectory.deleteOnExit();
200 FileUtils.deleteDirectory( indexDirectory );
202 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
203 repo.toURI().toURL().toExternalForm(),
204 indexDirectory.toURI().toURL().toString(),
205 search.getAllIndexCreators() );
207 // minimize datas in memory
208 context.getIndexWriter().setMaxBufferedDocs( -1 );
209 context.getIndexWriter().setRAMBufferSizeMB( 1 );
210 for ( File artifactFile : filesToBeIndexed )
212 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
213 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
215 if ( artifactFile.getPath().endsWith( ".pom" ) )
217 ac.getArtifactInfo().fextension = "pom";
218 ac.getArtifactInfo().packaging = "pom";
219 ac.getArtifactInfo().classifier = "pom";
221 nexusIndexer.addArtifactToIndex( ac, context );
222 context.updateTimestamp( true );
227 nexusIndexer.scan( context, new ArtifactScanListener(), false );
230 context.getIndexWriter().commit();
231 context.setSearchable( true );
235 static class ArtifactScanListener
236 implements ArtifactScanningListener
238 protected Logger log = LoggerFactory.getLogger( getClass() );
241 public void scanningStarted( IndexingContext ctx )
247 public void scanningFinished( IndexingContext ctx, ScanningResult result )
253 public void artifactError( ArtifactContext ac, Exception e )
255 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
259 public void artifactDiscovered( ArtifactContext ac )
261 log.debug( "artifactDiscovered {}:{}", ac.getArtifact().getPath(), ac.getArtifactInfo() );
265 public String niceDisplay( SearchResults searchResults )
268 StringBuilder sb = new StringBuilder();
269 for ( SearchResultHit hit : searchResults.getHits() )
271 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
273 return sb.toString();