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.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.io.FileUtils;
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.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;
76 ArtifactContextProducer artifactContextProducer;
78 IMocksControl archivaConfigControl;
83 PlexusSisuBridge plexusSisuBridge;
86 List<IndexCreator> indexCreators;
89 NexusIndexer nexusIndexer;
92 QueryCreator queryCreator;
101 FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
102 assertFalse( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
104 FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
105 assertFalse( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
107 archivaConfigControl = EasyMock.createControl();
109 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
111 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
112 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
114 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
115 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
117 search = new MavenRepositorySearch( nexusIndexer, defaultManagedRepositoryAdmin, defaultProxyConnectorAdmin,
120 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
121 defaultManagedRepositoryAdmin.setIndexCreators( indexCreators );
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( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
140 assertFalse( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
142 FileUtils.deleteDirectory( new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
143 assertFalse( new File( org.apache.archiva.common.utils.FileUtils.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( org.apache.archiva.common.utils.FileUtils.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( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer" );
180 if ( indexerDirectory.exists() )
182 FileUtils.deleteDirectory( indexerDirectory );
185 assertFalse( indexerDirectory.exists() );
187 File lockFile = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
188 if ( lockFile.exists() )
193 assertFalse( lockFile.exists() );
195 File repo = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository );
196 assertTrue( repo.exists() );
197 File indexDirectory =
198 new File( org.apache.archiva.common.utils.FileUtils.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(), indexCreators );
206 // minimize datas in memory
207 // context.getIndexWriter().setMaxBufferedDocs( -1 );
208 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
209 for ( File artifactFile : filesToBeIndexed )
211 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
212 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
214 if ( artifactFile.getPath().endsWith( ".pom" ) )
216 ac.getArtifactInfo().setFileExtension( "pom" );
217 ac.getArtifactInfo().setPackaging( "pom" );
218 ac.getArtifactInfo().setClassifier( "pom" );
220 nexusIndexer.addArtifactToIndex( ac, context );
221 context.updateTimestamp( true );
226 nexusIndexer.scan( context, new ArtifactScanListener(), false );
229 context.getIndexWriter().commit();
230 context.setSearchable( true );
234 static class ArtifactScanListener
235 implements ArtifactScanningListener
237 protected Logger log = LoggerFactory.getLogger( getClass() );
240 public void scanningStarted( IndexingContext ctx )
246 public void scanningFinished( IndexingContext ctx, ScanningResult result )
252 public void artifactError( ArtifactContext ac, Exception e )
254 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
258 public void artifactDiscovered( ArtifactContext ac )
260 log.debug( "artifactDiscovered {}:{}", //
261 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
262 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
266 public String niceDisplay( SearchResults searchResults )
269 StringBuilder sb = new StringBuilder();
270 for ( SearchResultHit hit : searchResults.getHits() )
272 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
274 return sb.toString();