]> source.dussan.org Git - archiva.git/commitdiff
-fix unit : lucene sometimes has pain to flush...
authorOlivier Lamy <olamy@apache.org>
Wed, 14 Sep 2011 22:52:52 +0000 (22:52 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 14 Sep 2011 22:52:52 +0000 (22:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1170884 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-indexer/pom.xml
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchResultHit.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/SearchResults.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/util/SearchUtil.java
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/AbstractNexusRepositorySearch.java
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java
archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/ArchivaIndexingTaskExecutor.java

index 2c61d74f6a963294d4564a7448785964f6ec5ddc..944d89e83255495d1bfb0e351050cf793b5ab92b 100644 (file)
     </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>
           <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>
index b374b9a9ef3c2bc673e9f9ab2759b3687842f71a..86c9230d86b5cbe03171883f8716d530b2a9dfdb 100644 (file)
@@ -189,8 +189,9 @@ public class NexusRepositorySearch
 
         if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
         {
-            q.add( indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
-                   Occur.MUST );
+            q.add(
+                indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
+                Occur.MUST );
         }
 
         if ( q.getClauses() == null || q.getClauses().length <= 0 )
@@ -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
index 30119b3c1ca8608e39c83fa1600fe53e10c5f4ae..0cfbec314f66bd95e1919447a9334de2a4def73d 100644 (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;
+    }
 }
index 7ac0872396d873e0d78f15f67facb17e0d957ddf..bd8d65a811578495130d9e1e0b206fd7f502b174 100644 (file)
@@ -26,7 +26,7 @@ import java.util.Map;
 
 
 /**
- * SearchResults 
+ * SearchResults
  *
  * @version $Id: SearchResults.java 742859 2009-02-10 05:35:05Z jdumay $
  */
@@ -47,20 +47,31 @@ public class SearchResults
 
     // for new RepositorySearch
     public void addHit( String id, SearchResultHit hit )
-    {   
+    {
         hits.put( id, hit );
     }
 
     /**
      * Get the list of {@link SearchResultHit} objects.
-     * 
+     *
      * @return the list of {@link SearchResultHit} objects.
      */
     public List<SearchResultHit> getHits()
     {
         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 )
     {
index 2048e600258fe0b65a129b0e9db637bc96c7d8cf..bdb4de4aefcee07d921af5fb2aa2b93849c61b66 100644 (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 ) );
     }
 }
index a10cc5f40f7de35974e6f359a0475d1605dd2db0..701d397868d8e4e6d40fda536bb56eb9f8c36960 100644 (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
index 90e2a5ec6966fbfd3a056bcdf34b48b85bf3debc..fb6f4ae0d6c779bfd3f1e644e5ae845014cbc07a 100644 (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" );
index 2477c7898915b44b6fc4a4035f24fe3a9fac9b2c..e3654714210f473e5afd946f92057bd97b238ac7 100644 (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