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;
53 * @author Olivier Lamy
55 @RunWith( SpringJUnit4ClassRunner.class )
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
57 public abstract class AbstractNexusRepositorySearch
61 protected Logger log = LoggerFactory.getLogger( getClass() );
63 public static String TEST_REPO_1 = "nexus-search-test-repo";
65 public static String TEST_REPO_2 = "nexus-search-test-repo-2";
67 NexusRepositorySearch search;
69 ArchivaConfiguration archivaConfig;
71 ArtifactContextProducer artifactContextProducer;
73 MockControl archivaConfigControl;
78 PlexusSisuBridge plexusSisuBridge;
81 MavenIndexerUtils mavenIndexerUtils;
83 NexusIndexer nexusIndexer;
91 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
92 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ).exists() );
94 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
95 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ).exists() );
97 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
99 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
101 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin = new DefaultManagedRepositoryAdmin();
102 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfig );
104 DefaultProxyConnectorAdmin defaultProxyConnectorAdmin = new DefaultProxyConnectorAdmin();
105 defaultProxyConnectorAdmin.setArchivaConfiguration( archivaConfig );
107 search = new NexusRepositorySearch( plexusSisuBridge, defaultManagedRepositoryAdmin, mavenIndexerUtils,
108 defaultProxyConnectorAdmin );
110 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
112 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
114 config = new Configuration();
115 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
116 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
120 public void tearDown()
124 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
126 nexusIndexer.removeIndexingContext( indexingContext, true );
129 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
130 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
132 FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
133 assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
138 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
140 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
141 repositoryConfig.setId( repository );
142 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/repos/" + repository );
143 File f = new File( repositoryConfig.getLocation() );
148 repositoryConfig.setLayout( "default" );
149 repositoryConfig.setName( repository );
150 repositoryConfig.setScanned( true );
151 repositoryConfig.setSnapshots( false );
152 repositoryConfig.setReleases( true );
154 return repositoryConfig;
157 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
161 IndexingContext context = nexusIndexer.getIndexingContexts().get( repository );
163 if ( context != null )
165 nexusIndexer.removeIndexingContext( context, true );
168 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
170 if ( indexerDirectory.exists() )
172 FileUtils.deleteDirectory( indexerDirectory );
175 assertFalse( indexerDirectory.exists() );
177 File lockFile = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer/write.lock" );
178 if ( lockFile.exists() )
183 assertFalse( lockFile.exists() );
185 File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
186 assertTrue( repo.exists() );
187 File indexDirectory =
188 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
189 indexDirectory.deleteOnExit();
190 FileUtils.deleteDirectory( indexDirectory );
192 context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
193 repo.toURI().toURL().toExternalForm(),
194 indexDirectory.toURI().toURL().toString(),
195 search.getAllIndexCreators() );
197 // minimize datas in memory
198 context.getIndexWriter().setMaxBufferedDocs( -1 );
199 context.getIndexWriter().setRAMBufferSizeMB( 1 );
200 for ( File artifactFile : filesToBeIndexed )
202 assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
203 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
204 nexusIndexer.addArtifactToIndex( ac, context );
205 context.updateTimestamp( true );
210 nexusIndexer.scan( context, new ArtifactScanListener(), false );
213 context.getIndexWriter().commit();
214 context.getIndexWriter().close( true );
215 // wait for io flush ....
216 //Thread.sleep( 2000 );
217 context.setSearchable( true );
221 static class ArtifactScanListener
222 implements ArtifactScanningListener
224 protected Logger log = LoggerFactory.getLogger( getClass() );
226 public void scanningStarted( IndexingContext ctx )
231 public void scanningFinished( IndexingContext ctx, ScanningResult result )
236 public void artifactError( ArtifactContext ac, Exception e )
238 log.debug( "artifactError " + ac.getArtifact().getPath(), e );
241 public void artifactDiscovered( ArtifactContext ac )
243 log.debug( "artifactDiscovered {}:", ac.getArtifact().getPath(), ac.getArtifactInfo() );
247 public String niceDisplay( SearchResults searchResults )
250 StringBuilder sb = new StringBuilder();
251 for ( SearchResultHit hit : searchResults.getHits() )
253 sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
255 return sb.toString();