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.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.archiva.common.utils.FileUtil;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.index.ArtifactContext;
31 import org.apache.maven.index.ArtifactContextProducer;
32 import org.apache.maven.index.NexusIndexer;
33 import org.apache.maven.index.context.IndexingContext;
34 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
35 import org.easymock.MockControl;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.runner.RunWith;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.List;
49 * @author Olivier Lamy
51 @RunWith( SpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
53 public abstract class AbstractNexusRepositorySearch
57 public static String TEST_REPO_1 = "nexus-search-test-repo";
59 public static String TEST_REPO_2 = "nexus-search-test-repo-2";
61 NexusRepositorySearch search;
63 ArchivaConfiguration archivaConfig;
65 ArtifactContextProducer artifactContextProducer;
67 MockControl archivaConfigControl;
72 PlexusSisuBridge plexusSisuBridge;
75 MavenIndexerUtils mavenIndexerUtils;
77 NexusIndexer nexusIndexer;
85 FileUtils.deleteDirectory(
86 new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) );
87 assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() );
89 FileUtils.deleteDirectory(
90 new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
91 assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
93 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
95 archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
97 search = new NexusRepositorySearch( plexusSisuBridge, archivaConfig, mavenIndexerUtils );
99 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
101 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
103 config = new Configuration();
104 config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
105 config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
109 public void tearDown()
113 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
115 nexusIndexer.removeIndexingContext( indexingContext, true );
118 FileUtils.deleteDirectory(
119 new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) );
120 assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() );
122 FileUtils.deleteDirectory(
123 new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
124 assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
129 protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
131 ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
132 repositoryConfig.setId( repository );
133 repositoryConfig.setLocation( FileUtil.getBasedir() + "/target/test-classes/" + repository );
134 repositoryConfig.setLayout( "default" );
135 repositoryConfig.setName( repository );
136 repositoryConfig.setScanned( true );
137 repositoryConfig.setSnapshots( false );
138 repositoryConfig.setReleases( true );
140 return repositoryConfig;
143 protected void createIndex( String repository, List<File> filesToBeIndexed, boolean scan )
144 throws IOException, UnsupportedExistingLuceneIndexException
147 File indexerDirectory = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" );
149 if ( indexerDirectory.exists() )
151 FileUtils.deleteDirectory( indexerDirectory );
154 assertFalse( indexerDirectory.exists() );
157 new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer/write.lock" );
158 if ( lockFile.exists() )
163 assertFalse( lockFile.exists() );
165 File repo = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository );
166 File indexDirectory = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" );
168 IndexingContext context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
169 repo.toURI().toURL().toExternalForm(),
170 indexDirectory.toURI().toURL().toString(),
171 search.getAllIndexCreators() );
173 List<ArtifactContext> artifactContexts = new ArrayList<ArtifactContext>( filesToBeIndexed.size() );
174 for ( File artifactFile : filesToBeIndexed )
176 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
177 artifactContexts.add( ac );
180 context.setSearchable( true );
182 if ( filesToBeIndexed != null && !filesToBeIndexed.isEmpty() )
184 nexusIndexer.addArtifactsToIndex( artifactContexts, context );
188 nexusIndexer.scan( context, false );
190 assertTrue( new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() );