Author: cstamas
Date: Fri Dec  3 19:17:22 2010
New Revision: 1041959

URL: http://svn.apache.org/viewvc?rev=1041959&view=rev
Log:
In rev1041505 I introduced a bug but was not aware since it had no test!

So, making it right:
* added test for REAL index downloads/updates
* same, but using DocumentFilters (this is where bug went in unnoticed).
* fixed the bug when Lucene index to be filtered was actually nuked

Added:
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
   (with props)
    maven/indexer/trunk/indexer-core/src/test/resources/repo-index/
    maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.gz
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
   (with props)
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.zip
    maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.gz
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
   (with props)
    
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.zip
Modified:
    
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
    
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/AbstractIndexUpdaterTest.java

Modified: 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java?rev=1041959&r1=1041958&r2=1041959&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/updater/DefaultIndexUpdater.java
 Fri Dec  3 19:17:22 2010
@@ -404,7 +404,7 @@ public class DefaultIndexUpdater
         try
         {
             // analyzer is unimportant, since we are not adding/searching 
to/on index, only reading/deleting
-            w = new NexusIndexWriter( directory, new NexusAnalyzer(), true );
+            w = new NexusIndexWriter( directory, new NexusAnalyzer(), false );
 
             w.optimize();
 

Modified: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/AbstractIndexUpdaterTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/AbstractIndexUpdaterTest.java?rev=1041959&r1=1041958&r2=1041959&view=diff
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/AbstractIndexUpdaterTest.java
 (original)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/AbstractIndexUpdaterTest.java
 Fri Dec  3 19:17:22 2010
@@ -20,11 +20,17 @@ package org.apache.maven.index.updater;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 
+import org.apache.lucene.search.Query;
 import org.apache.maven.index.AbstractIndexCreatorHelper;
 import org.apache.maven.index.ArtifactContext;
 import org.apache.maven.index.ArtifactInfo;
+import org.apache.maven.index.IteratorSearchRequest;
+import org.apache.maven.index.IteratorSearchResponse;
+import org.apache.maven.index.MAVEN;
 import org.apache.maven.index.NexusIndexer;
+import org.apache.maven.index.SearchType;
 import org.apache.maven.index.artifact.Gav;
 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
 import org.apache.maven.index.context.IndexingContext;
@@ -125,4 +131,32 @@ public abstract class AbstractIndexUpdat
         packer.packIndex( request );
     }
 
+    protected void searchFor( String groupId, int expected, IndexingContext 
context )
+        throws IOException, Exception
+    {
+        Query q = indexer.constructQuery( MAVEN.GROUP_ID, groupId, 
SearchType.EXACT );
+
+        IteratorSearchRequest req;
+
+        if ( context != null )
+        {
+            req = new IteratorSearchRequest( q, context );
+        }
+        else
+        {
+            req = new IteratorSearchRequest( q );
+        }
+
+        IteratorSearchResponse response = indexer.searchIterator( req );
+
+        ArrayList<ArtifactInfo> ais = new ArrayList<ArtifactInfo>( 
response.getTotalHits() );
+
+        for ( ArtifactInfo ai : response )
+        {
+            ais.add( ai );
+        }
+
+        assertEquals( ais.toString(), expected, ais.size() );
+    }
+
 }

Added: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java?rev=1041959&view=auto
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
 (added)
+++ 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
 Fri Dec  3 19:17:22 2010
@@ -0,0 +1,190 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version.
+ * Copyright (c) 2008 Sonatype, Inc. All rights reserved.
+ * Includes the third-party code listed at 
http://nexus.sonatype.org/dev/attributions.html
+ * This program is licensed to you under Version 3 only of the GNU General 
Public License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.
+ * See the GNU General Public License Version 3 for more details.
+ * You should have received a copy of the GNU General Public License Version 3 
along with this program.
+ * If not, see http://www.gnu.org/licenses/.
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc.
+ * "Sonatype" and "Sonatype Nexus" are trademarks of Sonatype, Inc.
+ */
+package org.apache.maven.index.updater;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Properties;
+import java.util.TimeZone;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.lucene.document.Document;
+import org.apache.maven.index.context.DocumentFilter;
+import org.apache.maven.index.context.IndexingContext;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Response;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.handler.DefaultHandler;
+import org.mortbay.jetty.handler.HandlerList;
+import org.mortbay.jetty.handler.ResourceHandler;
+
+public class DownloadRemoteIndexerManagerTest
+    extends AbstractIndexUpdaterTest
+{
+    private Server server;
+
+    private File fakeCentral;
+
+    private IndexingContext centralContext;
+
+    @Override
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        fakeCentral = new File( getBasedir(), "target/repos/fake-central" );
+        fakeCentral.mkdirs();
+
+        // create proxy server
+        ServerSocket s = new ServerSocket( 0 );
+        int port = s.getLocalPort();
+        s.close();
+
+        server = new Server( port );
+
+        ResourceHandler resource_handler = new ResourceHandler()
+        {
+            @Override
+            public void handle( String target, HttpServletRequest request, 
HttpServletResponse response, int dispatch )
+                throws IOException, ServletException
+            {
+                System.out.print( "JETTY: " + target );
+                super.handle( target, request, response, dispatch );
+                System.out.println( "  ::  " + ( (Response) response 
).getStatus() );
+            }
+        };
+        resource_handler.setResourceBase( fakeCentral.getAbsolutePath() );
+        HandlerList handlers = new HandlerList();
+        handlers.setHandlers( new Handler[] { resource_handler, new 
DefaultHandler() } );
+        server.setHandler( handlers );
+
+        System.out.print( "JETTY Started on port: " + port );
+        server.start();
+
+        // make context "fake central"
+        centralContext =
+            indexer.addIndexingContext( "central", "central", fakeCentral, 
getDirectory( "central" ),
+                "http://localhost:"; + port, null, MIN_CREATORS );
+    }
+
+    @Override
+    protected void tearDown()
+        throws Exception
+    {
+        server.stop();
+
+        FileUtils.forceDelete( fakeCentral );
+
+        super.tearDown();
+    }
+
+    public void testRepoReindex()
+        throws Exception
+    {
+        IndexUpdateRequest iur;
+
+        File index1 = new File( getBasedir(), 
"src/test/resources/repo-index/index" );
+        File index2 = new File( getBasedir(), 
"src/test/resources/repo-index/index2" );
+        File centralIndex = new File( fakeCentral, ".index" );
+
+        // copy index 02
+        overwriteIndex( index2, centralIndex );
+
+        iur =
+            new IndexUpdateRequest( centralContext, new WagonHelper( 
getContainer() ).getWagonResourceFetcher( null ) );
+        iur.setForceFullUpdate( true );
+
+        updater.fetchAndUpdateIndex( iur );
+
+        searchFor( "org.sonatype.nexus", 8, centralContext );
+
+        // copy index 01
+        overwriteIndex( index1, centralIndex );
+
+        iur =
+            new IndexUpdateRequest( centralContext, new WagonHelper( 
getContainer() ).getWagonResourceFetcher( null ) );
+        iur.setForceFullUpdate( true );
+        // just a dummy filter to invoke filtering! -- this is what I broke 
unnoticing it
+        iur.setDocumentFilter( new DocumentFilter()
+        {
+            public boolean accept( Document doc )
+            {
+                return true;
+            }
+        });
+
+        updater.fetchAndUpdateIndex( iur );
+
+        searchFor( "org.sonatype.nexus", 1, centralContext );
+
+        // copy index 02
+        overwriteIndex( index2, centralIndex );
+
+        iur =
+            new IndexUpdateRequest( centralContext, new WagonHelper( 
getContainer() ).getWagonResourceFetcher( null ) );
+        iur.setForceFullUpdate( true );
+
+        updater.fetchAndUpdateIndex( iur );
+
+        searchFor( "org.sonatype.nexus", 8, centralContext );
+    }
+
+    private void overwriteIndex( File source, File destination )
+        throws Exception
+    {
+        File indexFile = new File( destination, 
"nexus-maven-repository-index.gz" );
+        File indexProperties = new File( destination, 
"nexus-maven-repository-index.properties" );
+
+        long lastMod = -1;
+        if ( destination.exists() )
+        {
+            FileUtils.forceDelete( destination );
+            lastMod = indexFile.lastModified();
+        }
+        FileUtils.copyDirectory( source, destination, false );
+        long lastMod2 = indexFile.lastModified();
+        assertTrue( lastMod < lastMod2 );
+
+        Properties p = new Properties();
+        InputStream input = new FileInputStream( indexProperties );
+        p.load( input );
+        input.close();
+
+        p.setProperty( "nexus.index.time", format( new Date() ) );
+        p.setProperty( "nexus.index.timestamp", format( new Date() ) );
+
+        OutputStream output = new FileOutputStream( indexProperties );
+        p.store( output, null );
+        output.close();
+    }
+
+    private String format( Date d )
+    {
+        SimpleDateFormat df = new SimpleDateFormat( 
IndexingContext.INDEX_TIME_FORMAT );
+        df.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
+        return df.format( d );
+    }
+}

Propchange: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/updater/DownloadRemoteIndexerManagerTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.gz
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.gz?rev=1041959&view=auto
==============================================================================
Files 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.gz
 (added) and 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.gz
 Fri Dec  3 19:17:22 2010 differ

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties?rev=1041959&view=auto
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
 (added)
+++ 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
 Fri Dec  3 19:17:22 2010
@@ -0,0 +1,6 @@
+#Thu May 28 14:56:58 BRT 2009
+nexus.index.time=20090528175658.015 +0000
+nexus.index.chain-id=1243533418968
+nexus.index.id=apache-snapshots-local
+nexus.index.timestamp=20090528175658.015 +0000
+nexus.index.last-incremental=0

Propchange: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.zip
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.zip?rev=1041959&view=auto
==============================================================================
Files 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.zip
 (added) and 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index/nexus-maven-repository-index.zip
 Fri Dec  3 19:17:22 2010 differ

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.gz
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.gz?rev=1041959&view=auto
==============================================================================
Files 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.gz
 (added) and 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.gz
 Fri Dec  3 19:17:22 2010 differ

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties?rev=1041959&view=auto
==============================================================================
--- 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
 (added)
+++ 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
 Fri Dec  3 19:17:22 2010
@@ -0,0 +1,6 @@
+#Thu May 28 14:05:32 BRT 2009
+nexus.index.time=20090528170531.406 +0000
+nexus.index.chain-id=1243530332531
+nexus.index.id=snapshots-local
+nexus.index.timestamp=20090528170531.406 +0000
+nexus.index.last-incremental=0

Propchange: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.zip
URL: 
http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.zip?rev=1041959&view=auto
==============================================================================
Files 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.zip
 (added) and 
maven/indexer/trunk/indexer-core/src/test/resources/repo-index/index2/nexus-maven-repository-index.zip
 Fri Dec  3 19:17:22 2010 differ


Reply via email to