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.QueryCreator;
39 import org.apache.maven.index.ScanningResult;
40 import org.apache.maven.index.context.IndexingContext;
41 import org.apache.maven.index.shaded.lucene.index.IndexUpgrader;
42 import org.easymock.EasyMock;
43 import org.easymock.IMocksControl;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.runner.RunWith;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.test.context.ContextConfiguration;
51 import javax.inject.Inject;
53 import java.util.List;
56 * @author Olivier Lamy
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
60 public abstract class AbstractMavenRepositorySearch
64 protected Logger log = LoggerFactory.getLogger( getClass() );
66 public static String TEST_REPO_1 = "maven-search-test-repo";
68 public static String TEST_REPO_2 = "maven-search-test-repo-2";
71 public static String REPO_RELEASE = "repo-release";
73 MavenRepositorySearch search;
75 ArchivaConfiguration archivaConfig;
78 ArtifactContextProducer artifactContextProducer;
80 IMocksControl archivaConfigControl;
85 PlexusSisuBridge plexusSisuBridge;
88 MavenIndexerUtils mavenIndexerUtils;
91 NexusIndexer nexusIndexer;
94 QueryCreator queryCreator;
103 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
104 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
106 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
107 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
109 archivaConfigControl = EasyMock.createControl( );
111 archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
113 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
114 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
116 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
117 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
119 search = new MavenRepositorySearch( nexusIndexer, defaultManagedRepositoryAdmin, mavenIndexerUtils,
120 defaultProxyConnectorAdmin, queryCreator );
122 defaultManagedRepositoryAdmin.setMavenIndexerUtils( mavenIndexerUtils );
123 defaultManagedRepositoryAdmin.setIndexer( nexusIndexer );
124 defaultManagedRepositoryAdmin.setIndexCreators( mavenIndexerUtils.getAllIndexCreators() );
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( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
143 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
145 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
146 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
151 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
153 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
154 repositoryConfig.setId( repository );
155 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
156 File f = new File( repositoryConfig.getLocation() );
161 repositoryConfig.setLayout( "default" );
162 repositoryConfig.setName( repository );
163 repositoryConfig.setScanned( true );
164 repositoryConfig.setSnapshots( false );
165 repositoryConfig.setReleases( true );
167 return repositoryConfig;
170 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
174 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
176 if ( context != null )
178 nexusIndexer.removeIndexingContext( context, true );
181 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
183 if ( indexerDirectory.exists() )
185 FileUtils.deleteDirectory( indexerDirectory );
188 assertFalse( indexerDirectory.exists() );
190 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
191 if ( lockFile.exists() )
196 assertFalse( lockFile.exists() );
198 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
199 assertTrue( repo.exists() );
200 File indexDirectory =
201 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
202 indexDirectory.deleteOnExit();
203 FileUtils.deleteDirectory( indexDirectory );
205 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
206 repo.toURI().toURL().toExternalForm(),
207 indexDirectory.toURI().toURL().toString(),
208 search.getAllIndexCreators() );
210 // minimize datas in memory
211 // context.getIndexWriter().setMaxBufferedDocs( -1 );
212 // context.getIndexWriter().setRAMBufferSizeMB( 1 );
213 for ( File artifactFile : filesToBeIndexed )
215 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
216 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
218 if ( artifactFile.getPath().endsWith( ".pom" ) )
220 ac.getArtifactInfo().setFileExtension( "pom" );
221 ac.getArtifactInfo().setPackaging( "pom" );
222 ac.getArtifactInfo().setClassifier( "pom" );
224 nexusIndexer.addArtifactToIndex( ac, context );
225 context.updateTimestamp( true );
230 nexusIndexer.scan( context, new ArtifactScanListener(), false );
233 context.getIndexWriter().commit();
234 context.setSearchable( true );
238 static class ArtifactScanListener
239 implements ArtifactScanningListener
241 protected Logger log = LoggerFactory.getLogger( getClass() );
244 public void scanningStarted( IndexingContext ctx )
250 public void scanningFinished( IndexingContext ctx, ScanningResult result )
256 public void artifactError( ArtifactContext ac, Exception e )
258 log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
262 public void artifactDiscovered( ArtifactContext ac )
264 log.debug( "artifactDiscovered {}:{}", //
265 ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
266 ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
270 public String niceDisplay( SearchResults searchResults )
273 StringBuilder sb = new StringBuilder();
274 for ( SearchResultHit hit : searchResults.getHits() )
276 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
278 return sb.toString();