-fix unit : lucene sometimes has pain to flush...

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1170884 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-09-14 22:52:52 +00:00
parent ae4ff95a87
commit 6e252d9a2d
8 changed files with 122 additions and 65 deletions

View File

@ -100,19 +100,6 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<check>
<!-- TODO: increase coverage -->
<totalLineRate>80</totalLineRate>
<totalBranchRate>80</totalBranchRate>
</check>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
@ -120,7 +107,7 @@
<artifactId>apache-rat-plugin</artifactId>
<configuration>
<excludes>
<exclude>src/test/resources/nexus-search-test-repo*/**</exclude>
<exclude>src/test/nexus-search-test-repo*/**</exclude>
</excludes>
</configuration>
</plugin>

View File

@ -189,7 +189,8 @@ public class NexusRepositorySearch
if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
{
q.add( indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
q.add(
indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
Occur.MUST );
}
@ -204,6 +205,7 @@ public class NexusRepositorySearch
private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds )
throws RepositorySearchException
{
try
{
FlatSearchRequest request = new FlatSearchRequest( q );
@ -343,9 +345,11 @@ public class NexusRepositorySearch
{
log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
continue;
} catch ( RepositoryAdminException e )
}
catch ( RepositoryAdminException e )
{
log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
+ e.getMessage() );
continue;
}
}
@ -366,7 +370,8 @@ public class NexusRepositorySearch
for ( ArtifactInfo artifactInfo : artifactInfos )
{
String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
artifactInfo.packaging );
Map<String, SearchResultHit> hitsMap = results.getHitsMap();
SearchResultHit hit = hitsMap.get( id );
@ -450,7 +455,8 @@ public class NexusRepositorySearch
SearchResultHit hit = results.getHits().get( ( offset + i ) );
if ( hit != null )
{
String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
hit.getPackaging() );
paginated.addHit( id, hit );
}
else

View File

@ -373,5 +373,47 @@ public class SearchResultHit
return sb.toString();
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
SearchResultHit that = (SearchResultHit) o;
if ( artifactId != null ? !artifactId.equals( that.artifactId ) : that.artifactId != null )
{
return false;
}
if ( classifier != null ? !classifier.equals( that.classifier ) : that.classifier != null )
{
return false;
}
if ( groupId != null ? !groupId.equals( that.groupId ) : that.groupId != null )
{
return false;
}
if ( packaging != null ? !packaging.equals( that.packaging ) : that.packaging != null )
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = groupId != null ? groupId.hashCode() : 0;
result = 31 * result + ( artifactId != null ? artifactId.hashCode() : 0 );
result = 31 * result + ( packaging != null ? packaging.hashCode() : 0 );
result = 31 * result + ( classifier != null ? classifier.hashCode() : 0 );
return result;
}
}

View File

@ -61,6 +61,17 @@ public class SearchResults
return new ArrayList<SearchResultHit>( hits.values() );
}
/**
* see SearchUtil on how to generate the key
*
* @param key
* @return
*/
public SearchResultHit getSearchResultHit( String key )
{
return hits.get( key );
}
public Map<String, SearchResultHit> getHitsMap()
{
return hits;
@ -92,8 +103,8 @@ public class SearchResults
}
/**
* @since 1.4
* @return
* @since 1.4
*/
public int getReturnedHitsCount()
{
@ -101,8 +112,8 @@ public class SearchResults
}
/**
* @since 1.4
* @param returnedHitsCount
* @since 1.4
*/
public void setReturnedHitsCount( int returnedHitsCount )
{

View File

@ -19,13 +19,18 @@ package org.apache.archiva.indexer.util;
* under the License.
*/
import org.apache.commons.lang.StringUtils;
/**
* SearchUtil - utility class for search.
*/
public class SearchUtil
{
public static String getHitId( String groupId, String artifactId )
public static String getHitId( String groupId, String artifactId, String classifier, String packaging )
{
return groupId + ":" + artifactId;
return ( StringUtils.isBlank( groupId ) ? "" : StringUtils.trim( groupId ) ) + ":"
+ ( StringUtils.isBlank( artifactId ) ? "" : StringUtils.trim( artifactId ) ) + ":"
+ ( StringUtils.isBlank( classifier ) ? "" : StringUtils.trim( classifier ) ) + ":" + ( StringUtils.isBlank(
packaging ) ? "" : StringUtils.trim( packaging ) );
}
}

View File

@ -46,7 +46,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
@ -122,6 +121,12 @@ public abstract class AbstractNexusRepositorySearch
nexusIndexer.removeIndexingContext( indexingContext, true );
}
FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_1 ).exists() );
FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
assertFalse( new File( FileUtil.getBasedir(), "/target/repos/" + TEST_REPO_2 ).exists() );
super.tearDown();
}
@ -172,38 +177,40 @@ public abstract class AbstractNexusRepositorySearch
assertFalse( lockFile.exists() );
File repo = new File( FileUtil.getBasedir(), "/target/repos/" + repository );
File indexDirectory = new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" );
File repo = new File( FileUtil.getBasedir(), "src/test/" + repository );
assertTrue( repo.exists() );
File indexDirectory =
new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
indexDirectory.deleteOnExit();
FileUtils.deleteDirectory( indexDirectory );
context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory,
repo.toURI().toURL().toExternalForm(),
indexDirectory.toURI().toURL().toString(),
search.getAllIndexCreators() );
List<ArtifactContext> artifactContexts = new ArrayList<ArtifactContext>( filesToBeIndexed.size() );
// minimize datas in memory
context.getIndexWriter().setMaxBufferedDocs( -1 );
context.getIndexWriter().setRAMBufferSizeMB( 1 );
for ( File artifactFile : filesToBeIndexed )
{
assertTrue( "file not exists " + artifactFile.getPath(), artifactFile.exists() );
ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
artifactContexts.add( ac );
nexusIndexer.addArtifactToIndex( ac, context );
context.updateTimestamp( true );
}
if ( filesToBeIndexed != null && !filesToBeIndexed.isEmpty() )
{
nexusIndexer.addArtifactsToIndex( artifactContexts, context );
while ( context.isReceivingUpdates() )
{
Thread.sleep( 10 );
}
}
if ( scan )
{
nexusIndexer.scan( context, new ArtifactScanListener(), false );
}
// force flushing
context.getIndexWriter().commit();
context.getIndexWriter().close( true );
// wait for io flush ....
//Thread.sleep( 2000 );
context.setSearchable( true );
assertTrue( new File( FileUtil.getBasedir(), "/target/repos/" + repository + "/.indexer" ).exists() );
}
static class ArtifactScanListener

View File

@ -19,16 +19,14 @@ package org.apache.archiva.indexer.search;
* under the License.
*/
import org.apache.archiva.indexer.util.SearchUtil;
import org.apache.maven.archiva.common.utils.FileUtil;
import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -45,11 +43,11 @@ public class NexusRepositorySearchTest
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( FileUtil.getBasedir(), "src/test" + TEST_REPO_1
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1
files.add( new File( FileUtil.getBasedir(), "/src/test/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
createIndex( TEST_REPO_1, files, scan );
@ -67,12 +65,12 @@ public class NexusRepositorySearchTest
+ "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(),
"src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/com/classname-search/1.0/classname-search-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(),
"src/test/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
createIndex( TEST_REPO_1, files, scan );
}
@ -108,9 +106,10 @@ public class NexusRepositorySearchTest
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 1, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 );
SearchResultHit hit =
results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
assertNotNull( "hit null in result " + results.getHits(), hit );
assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-search", hit.getArtifactId() );
assertEquals( "1.0", hit.getVersions().get( 0 ) );
@ -166,7 +165,7 @@ public class NexusRepositorySearchTest
public void testMultipleArtifactsSameVersionWithClassifier()
throws Exception
{
createIndexContainingMultipleArtifactsSameVersion( false );
createIndexContainingMultipleArtifactsSameVersion( true );
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
@ -291,17 +290,20 @@ public class NexusRepositorySearchTest
archivaConfigControl.replay();
// wait lucene flush.....
Thread.sleep( 2000 );
SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 4, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 );
SearchResultHit hit =
results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-search", hit.getArtifactId() );
assertEquals("not 2 version for hit " + hit, 2, hit.getVersions().size() );
assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
assertTrue( hit.getVersions().contains( "1.0" ) );
assertTrue( hit.getVersions().contains( "1.1" ) );
@ -489,8 +491,8 @@ public class NexusRepositorySearchTest
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(),
"src/test/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( FileUtil.getBasedir(), "src/test/" + TEST_REPO_1
+ "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
createIndex( TEST_REPO_1, files, true );
@ -688,8 +690,7 @@ public class NexusRepositorySearchTest
{
createIndexContainingMoreArtifacts( true );
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
SearchFields searchFields = new SearchFields();
searchFields.setClassName( "com.classname.search.App" );

View File

@ -197,8 +197,6 @@ public class ArchivaIndexingTaskExecutor
nexusIndexer.addArtifactToIndex( ac, context );
}
//nexusIndexer.scan( context, true );
context.updateTimestamp();
// close the context if not a repo scan request