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;
95 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
96 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
98 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
99 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
101 archivaConfigControl = EasyMock.createControl( );
103 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
105 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
106 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
108 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
109 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
111 search = new MavenRepositorySearch( plexusSisuBridge, defaultManagedRepositoryAdmin, mavenIndexerUtils,
112 defaultProxyConnectorAdmin );
114 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
116 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
118 defaultManagedRepositoryAdmin.setMavenIndexerUtils( mavenIndexerUtils );
119 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
120 defaultManagedRepositoryAdmin.setIndexCreators( mavenIndexerUtils.getAllIndexCreators() );
122 config = new Configuration();
123 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
124 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
125 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
129 public void tearDown()
132 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
134 nexusIndexer.removeIndexingContext( indexingContext, true );
137 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
138 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
140 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
141 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
146 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
148 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
149 repositoryConfig.setId( repository );
150 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
151 File f = new File( repositoryConfig.getLocation() );
156 repositoryConfig.setLayout( "default" );
157 repositoryConfig.setName( repository );
158 repositoryConfig.setScanned( true );
159 repositoryConfig.setSnapshots( false );
160 repositoryConfig.setReleases( true );
162 return repositoryConfig;
165 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
169 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
171 if ( context != null )
173 nexusIndexer.removeIndexingContext( context, true );
176 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
178 if ( indexerDirectory.exists() )
180 FileUtils.deleteDirectory( indexerDirectory );
183 assertFalse( indexerDirectory.exists() );
185 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
186 if ( lockFile.exists() )
191 assertFalse( lockFile.exists() );
193 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
194 assertTrue( repo.exists() );
195 File indexDirectory =
196 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
197 indexDirectory.deleteOnExit();
198 FileUtils.deleteDirectory( indexDirectory );
200 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
201 repo.toURI().toURL().toExternalForm(),
202 indexDirectory.toURI().toURL().toString(),
203 search.getAllIndexCreators() );
205 // minimize datas in memory
206 context.getIndexWriter().setMaxBufferedDocs( -1 );
207 context.getIndexWriter().setRAMBufferSizeMB( 1 );
208 for ( File artifactFile : filesToBeIndexed )
210 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
211 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
213 if ( artifactFile.getPath().endsWith( ".pom" ) )
215 ac.getArtifactInfo().fextension = "pom";
216 ac.getArtifactInfo().packaging = "pom";
217 ac.getArtifactInfo().classifier = "pom";
219 nexusIndexer.addArtifactToIndex( ac, context );
220 context.updateTimestamp( true );
225 nexusIndexer.scan( context, new ArtifactScanListener(), false );
228 context.getIndexWriter().commit();
229 context.setSearchable( true );
233 static class ArtifactScanListener
234 implements ArtifactScanningListener
236 protected Logger log = LoggerFactory.getLogger( getClass() );
238 public void scanningStarted( IndexingContext ctx )
243 public void scanningFinished( IndexingContext ctx, ScanningResult result )
248 public void artifactError( ArtifactContext ac, Exception e )
250 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
253 public void artifactDiscovered( ArtifactContext ac )
255 log.debug( "artifactDiscovered {}:{}", ac.getArtifact().getPath(), ac.getArtifactInfo() );
259 public String niceDisplay( SearchResults searchResults )
262 StringBuilder sb = new StringBuilder();
263 for ( SearchResultHit hit : searchResults.getHits() )
265 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
267 return sb.toString();