fix unit tests spring configuration in archiva-indexer

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1128212 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-05-27 09:34:43 +00:00
parent 63af39fba0
commit 7eebed1423
5 changed files with 387 additions and 260 deletions

View File

@ -66,6 +66,18 @@
<groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-indexer</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-plexus</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-plexus-bridge</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>

View File

@ -19,15 +19,11 @@ package org.apache.archiva.indexer.search;
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
@ -40,22 +36,33 @@ import org.sonatype.nexus.index.FlatSearchResponse;
import org.sonatype.nexus.index.NexusIndexer;
import org.sonatype.nexus.index.context.IndexingContext;
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* RepositorySearch implementation which uses the Nexus Indexer for searching.
*/
@Service( "nexusSearch" )
public class NexusRepositorySearch
implements RepositorySearch
{
private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
private Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
private NexusIndexer indexer;
private ArchivaConfiguration archivaConfig;
public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig )
@Inject
public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ArchivaConfiguration archivaConfig )
throws PlexusSisuBridgeException
{
this.indexer = indexer;
this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
this.archivaConfig = archivaConfig;
}
@ -215,9 +222,10 @@ public class NexusRepositorySearch
indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
}
IndexingContext context =
indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
indexDirectory, null, null, ArchivaNexusIndexerUtil.FULL_INDEX );
IndexingContext context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
new File( repoConfig.getLocation() ),
indexDirectory, null, null,
ArchivaNexusIndexerUtil.FULL_INDEX );
context.setSearchable( repoConfig.isScanned() );
}
else

View File

@ -1,13 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<context:annotation-config/>
<context:component-scan base-package="org.apache.archiva.indexer.search"/>
<!--
<bean id="nexusSearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch">
<constructor-arg ref="nexusIndexer"/>
<constructor-arg ref="archivaConfiguration"/>
<constructor-arg ref="archivaConfiguration#default"/>
</bean>
-->
<bean id="logger" class="org.apache.maven.archiva.common.utils.Slf4JPlexusLogger">
<constructor-arg type="java.lang.Class"><value>org.sonatype.nexus.index.DefaultNexusIndexer</value></constructor-arg>
</bean>

View File

@ -19,29 +19,40 @@ package org.apache.archiva.indexer.search;
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonatype.nexus.artifact.IllegalArtifactCoordinateException;
import org.sonatype.nexus.index.ArtifactContext;
import org.sonatype.nexus.index.ArtifactContextProducer;
import org.sonatype.nexus.index.IndexerEngine;
import org.sonatype.nexus.index.NexusIndexer;
import org.sonatype.nexus.index.context.DefaultIndexingContext;
import org.sonatype.nexus.index.context.IndexingContext;
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
import org.sonatype.nexus.index.IndexerEngine;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
public class NexusRepositorySearchTest
extends PlexusInSpringTestCase
extends TestCase
{
private RepositorySearch search;
@ -63,39 +74,53 @@ public class NexusRepositorySearchTest
private final static String TEST_REPO_2 = "nexus-search-test-repo-2";
@Override
protected void setUp()
@Inject
PlexusSisuBridge plexusSisuBridge;
@Before
public void setUp()
throws Exception
{
super.setUp();
indexer = (NexusIndexer) lookup( NexusIndexer.class );
indexer = plexusSisuBridge.lookup( NexusIndexer.class );
archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
search = new NexusRepositorySearch( indexer, archivaConfig );
search = new NexusRepositorySearch( plexusSisuBridge, archivaConfig );
indexerEngine = (IndexerEngine) lookup( IndexerEngine.class );
indexerEngine = plexusSisuBridge.lookup( IndexerEngine.class );
artifactContextProducer = (ArtifactContextProducer) lookup( ArtifactContextProducer.class );
artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
config = new Configuration();
config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
}
public static String getBasedir()
{
String basedir = System.getProperty( "basedir" );
if ( basedir == null )
{
basedir = new File( "" ).getAbsolutePath();
}
return basedir;
}
private void createSimpleIndex()
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
createIndex( TEST_REPO_1, files );
}
@ -104,20 +129,20 @@ public class NexusRepositorySearchTest
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/com/classname-search/1.0/classname-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/com/classname-search/1.0/classname-search-1.0.jar" ) );
createIndex( TEST_REPO_1, files );
}
@ -136,8 +161,8 @@ public class NexusRepositorySearchTest
return repositoryConfig;
}
@Override
protected void tearDown()
@After
public void tearDown()
throws Exception
{
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) );
@ -152,9 +177,11 @@ public class NexusRepositorySearchTest
private void createIndex( String repository, List<File> filesToBeIndexed )
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{
context = new DefaultIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" +
repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null,
ArchivaNexusIndexerUtil.FULL_INDEX, false );
context = new DefaultIndexingContext( repository, repository,
new File( getBasedir(), "/target/test-classes/" + repository ),
new File( getBasedir(),
"/target/test-classes/" + repository + "/.indexer" ), null,
null, ArchivaNexusIndexerUtil.FULL_INDEX, false );
//indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" +
// repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null,
// NexusIndexer.FULL_INDEX );
@ -175,6 +202,7 @@ public class NexusRepositorySearchTest
assertTrue( new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() );
}
@Test
public void testQuickSearch()
throws Exception
{
@ -218,6 +246,7 @@ public class NexusRepositorySearchTest
}
// search for existing artifact using multiple keywords
@Test
public void testQuickSearchWithMultipleKeywords()
throws Exception
{
@ -237,6 +266,7 @@ public class NexusRepositorySearchTest
assertEquals( 0, results.getTotalHits() );
}
@Test
public void testQuickSearchWithPagination()
throws Exception
{
@ -282,16 +312,17 @@ public class NexusRepositorySearchTest
assertEquals( limits, results.getLimits() );
}
@Test
public void testArtifactFoundInMultipleRepositories()
throws Exception
{
createSimpleIndex();
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
+ "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
+ "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>();
@ -323,6 +354,7 @@ public class NexusRepositorySearchTest
// TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
}
@Test
public void testNoMatchFound()
throws Exception
{
@ -343,6 +375,7 @@ public class NexusRepositorySearchTest
assertEquals( 0, results.getTotalHits() );
}
@Test
public void testNoIndexFound()
throws Exception
{
@ -360,6 +393,7 @@ public class NexusRepositorySearchTest
archivaConfigControl.verify();
}
@Test
public void testRepositoryNotFound()
throws Exception
{
@ -377,6 +411,7 @@ public class NexusRepositorySearchTest
archivaConfigControl.verify();
}
@Test
public void testSearchWithinSearchResults()
throws Exception
{
@ -407,15 +442,15 @@ public class NexusRepositorySearchTest
}
// tests for advanced search
@Test
public void testAdvancedSearch()
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
+ "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
+ "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>();
@ -443,6 +478,7 @@ public class NexusRepositorySearchTest
assertEquals( "1.0", hit.getVersions().get( 0 ) );
}
@Test
public void testAdvancedSearchWithPagination()
throws Exception
{
@ -492,14 +528,15 @@ public class NexusRepositorySearchTest
}
// MRM-981 - artifactIds with numeric characters aren't found in advanced search
@Test
public void testAdvancedSearchArtifactIdHasNumericChar()
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 +
"/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
+ "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
createIndex( TEST_REPO_1, files );
List<String> selectedRepos = new ArrayList<String>();
@ -521,6 +558,7 @@ public class NexusRepositorySearchTest
assertEquals( 2, results.getTotalHits() );
}
@Test
public void testAdvancedSearchNoRepositoriesConfigured()
throws Exception
{
@ -539,6 +577,7 @@ public class NexusRepositorySearchTest
}
}
@Test
public void testAdvancedSearchSearchFieldsAreNull()
throws Exception
{
@ -566,6 +605,7 @@ public class NexusRepositorySearchTest
}
}
@Test
public void testAdvancedSearchSearchFieldsAreBlank()
throws Exception
{
@ -599,6 +639,7 @@ public class NexusRepositorySearchTest
}
}
@Test
public void testAdvancedSearchAllSearchCriteriaSpecified()
throws Exception
{
@ -633,6 +674,7 @@ public class NexusRepositorySearchTest
assertEquals( "2.0", hit.getVersions().get( 0 ) );
}
@Test
public void testAdvancedSearchJarArtifacts()
throws Exception
{
@ -657,6 +699,7 @@ public class NexusRepositorySearchTest
assertEquals( 5, results.getTotalHits() );
}
@Test
public void testAdvancedSearchWithIncorrectPackaging()
throws Exception
{
@ -683,6 +726,7 @@ public class NexusRepositorySearchTest
assertEquals( 0, results.getTotalHits() );
}
@Test
public void testAdvancedSearchClassname()
throws Exception
{
@ -712,6 +756,7 @@ public class NexusRepositorySearchTest
assertEquals( "1.0", hit.getVersions().get( 0 ) );
}
@Test
public void testAdvancedSearchNoIndexFound()
throws Exception
{
@ -730,6 +775,7 @@ public class NexusRepositorySearchTest
archivaConfigControl.verify();
assertNotNull( results );assertEquals( 0, results.getTotalHits() );
assertNotNull( results );
assertEquals( 0, results.getTotalHits() );
}
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
</beans>