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;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 import javax.inject.Inject;
50 import java.util.List;
51 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
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 AbstractNexusRepositorySearch
62 protected Logger log = LoggerFactory.getLogger( getClass() );
64 public static String TEST_REPO_1 = "nexus-search-test-repo";
66 public static String TEST_REPO_2 = "nexus-search-test-repo-2";
68 NexusRepositorySearch search;
70 ArchivaConfiguration archivaConfig;
72 ArtifactContextProducer artifactContextProducer;
74 MockControl archivaConfigControl;
79 PlexusSisuBridge plexusSisuBridge;
82 MavenIndexerUtils mavenIndexerUtils;
84 NexusIndexer nexusIndexer;
92 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
93 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
95 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
96 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
98 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
100 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
102 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
103 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
105 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
106 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
108 search = new NexusRepositorySearch( plexusSisuBridge, defaultManagedRepositoryAdmin, mavenIndexerUtils,
109 defaultProxyConnectorAdmin );
111 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
113 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
115 config = new Configuration();
116 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
117 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
121 public void tearDown()
125 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
127 nexusIndexer.removeIndexingContext( indexingContext, true );
130 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
131 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
133 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
134 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
139 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
141 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
142 repositoryConfig.setId( repository );
143 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
144 File f = new File( repositoryConfig.getLocation() );
149 repositoryConfig.setLayout( "default" );
150 repositoryConfig.setName( repository );
151 repositoryConfig.setScanned( true );
152 repositoryConfig.setSnapshots( false );
153 repositoryConfig.setReleases( true );
155 return repositoryConfig;
158 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
162 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
164 if ( context != null )
166 nexusIndexer.removeIndexingContext( context, true );
169 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
171 if ( indexerDirectory.exists() )
173 FileUtils.deleteDirectory( indexerDirectory );
176 assertFalse( indexerDirectory.exists() );
178 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
179 if ( lockFile.exists() )
184 assertFalse( lockFile.exists() );
186 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
187 assertTrue( repo.exists() );
188 File indexDirectory =
189 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
190 indexDirectory.deleteOnExit();
191 FileUtils.deleteDirectory( indexDirectory );
193 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
194 repo.toURI().toURL().toExternalForm(),
195 indexDirectory.toURI().toURL().toString(),
196 search.getAllIndexCreators() );
198 // minimize datas in memory
199 context.getIndexWriter().setMaxBufferedDocs( -1 );
200 context.getIndexWriter().setRAMBufferSizeMB( 1 );
201 for ( File artifactFile : filesToBeIndexed )
203 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
204 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
205 nexusIndexer.addArtifactToIndex( ac, context );
206 context.updateTimestamp( true );
211 nexusIndexer.scan( context, new ArtifactScanListener(), false );
214 context.getIndexWriter().commit();
215 context.getIndexWriter().close( true );
216 // wait for io flush ....
217 //Thread.sleep( 2000 );
218 context.setSearchable( true );
222 static class ArtifactScanListener
223 implements ArtifactScanningListener
225 protected Logger log = LoggerFactory.getLogger( getClass() );
227 public void scanningStarted( IndexingContext ctx )
232 public void scanningFinished( IndexingContext ctx, ScanningResult result )
237 public void artifactError( ArtifactContext ac, Exception e )
239 log.debug( "artifactError " + ac.getArtifact().getPath(), e );
242 public void artifactDiscovered( ArtifactContext ac )
244 log.debug( "artifactDiscovered {}:", ac.getArtifact().getPath(), ac.getArtifactInfo() );
248 public String niceDisplay( SearchResults searchResults )
251 StringBuilder sb = new StringBuilder();
252 for ( SearchResultHit hit : searchResults.getHits() )
254 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
256 return sb.toString();