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.commons.io.FileUtils;
32 import org.apache.commons.lang.SystemUtils;
33 import org.apache.maven.index.ArtifactContext;
34 import org.apache.maven.index.ArtifactContextProducer;
35 import org.apache.maven.index.ArtifactScanningListener;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.ScanningResult;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.easymock.MockControl;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.runner.RunWith;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.test.context.ContextConfiguration;
47 import javax.inject.Inject;
49 import java.util.List;
50 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
53 * @author Olivier Lamy
55 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
57 public abstract class AbstractMavenRepositorySearch
61 protected Logger log = LoggerFactory.getLogger( getClass() );
63 public static String TEST_REPO_1 = "maven-search-test-repo";
65 public static String TEST_REPO_2 = "maven-search-test-repo-2";
68 public static String REPO_RELEASE = "repo-release";
70 MavenRepositorySearch search;
72 ArchivaConfiguration archivaConfig;
74 ArtifactContextProducer artifactContextProducer;
76 MockControl archivaConfigControl;
81 PlexusSisuBridge plexusSisuBridge;
84 MavenIndexerUtils mavenIndexerUtils;
86 NexusIndexer nexusIndexer;
94 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
95 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
97 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
98 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
100 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
102 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
104 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
105 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
107 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
108 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
110 search = new MavenRepositorySearch( plexusSisuBridge, defaultManagedRepositoryAdmin, mavenIndexerUtils,
111 defaultProxyConnectorAdmin );
113 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
115 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
117 defaultManagedRepositoryAdmin.setMavenIndexerUtils( mavenIndexerUtils );
118 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
119 defaultManagedRepositoryAdmin.setIndexCreators( mavenIndexerUtils.getAllIndexCreators() );
121 config = new Configuration();
122 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
123 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
124 config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
128 public void tearDown()
131 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
133 nexusIndexer.removeIndexingContext( indexingContext, true );
136 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
137 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
139 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
140 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
145 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
147 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
148 repositoryConfig.setId( repository );
149 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
150 File f = new File( repositoryConfig.getLocation() );
155 repositoryConfig.setLayout( "default" );
156 repositoryConfig.setName( repository );
157 repositoryConfig.setScanned( true );
158 repositoryConfig.setSnapshots( false );
159 repositoryConfig.setReleases( true );
161 return repositoryConfig;
164 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
168 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
170 if ( context != null )
172 nexusIndexer.removeIndexingContext( context, true );
175 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
177 if ( indexerDirectory.exists() )
179 FileUtils.deleteDirectory( indexerDirectory );
182 assertFalse( indexerDirectory.exists() );
184 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
185 if ( lockFile.exists() )
190 assertFalse( lockFile.exists() );
192 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
193 assertTrue( repo.exists() );
194 File indexDirectory =
195 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
196 indexDirectory.deleteOnExit();
197 FileUtils.deleteDirectory( indexDirectory );
199 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
200 repo.toURI().toURL().toExternalForm(),
201 indexDirectory.toURI().toURL().toString(),
202 search.getAllIndexCreators() );
204 // minimize datas in memory
205 context.getIndexWriter().setMaxBufferedDocs( -1 );
206 context.getIndexWriter().setRAMBufferSizeMB( 1 );
207 for ( File artifactFile : filesToBeIndexed )
209 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
210 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
212 if ( artifactFile.getPath().endsWith( ".pom" ) )
214 ac.getArtifactInfo().fextension = "pom";
215 ac.getArtifactInfo().packaging = "pom";
216 ac.getArtifactInfo().classifier = "pom";
218 nexusIndexer.addArtifactToIndex( ac, context );
219 context.updateTimestamp( true );
224 nexusIndexer.scan( context, new ArtifactScanListener(), false );
227 context.getIndexWriter().commit();
228 context.setSearchable( true );
232 static class ArtifactScanListener
233 implements ArtifactScanningListener
235 protected Logger log = LoggerFactory.getLogger( getClass() );
237 public void scanningStarted( IndexingContext ctx )
242 public void scanningFinished( IndexingContext ctx, ScanningResult result )
247 public void artifactError( ArtifactContext ac, Exception e )
249 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
252 public void artifactDiscovered( ArtifactContext ac )
254 log.debug( "artifactDiscovered {}:{}", ac.getArtifact().getPath(), ac.getArtifactInfo() );
258 public String niceDisplay( SearchResults searchResults )
261 StringBuilder sb = new StringBuilder();
262 for ( SearchResultHit hit : searchResults.getHits() )
264 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
266 return sb.toString();