]> source.dussan.org Git - archiva.git/commitdiff
Switching path in indexer api to uri adding additional tests
authorMartin Stockhammer <martin.stockhammer@ars.de>
Mon, 20 Nov 2017 20:48:17 +0000 (21:48 +0100)
committerMartin Stockhammer <martin.stockhammer@ars.de>
Mon, 20 Nov 2017 20:48:17 +0000 (21:48 +0100)
archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java
archiva-modules/archiva-base/archiva-maven2-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java
archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java
archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java

index 60c2fdd3c7c673e9013b4a1c7a4eead6637bd807..d54f868a0ed14f005988f1b7d3597757498b56cb 100644 (file)
@@ -29,7 +29,6 @@ import org.apache.archiva.indexer.ArchivaIndexingContext;
 import org.apache.archiva.indexer.IndexCreationFailedException;
 import org.apache.archiva.indexer.IndexUpdateFailedException;
 import org.apache.archiva.indexer.UnsupportedBaseContextException;
-import org.apache.archiva.model.ArtifactReference;
 import org.apache.archiva.proxy.common.WagonFactory;
 import org.apache.archiva.proxy.common.WagonFactoryException;
 import org.apache.archiva.proxy.common.WagonFactoryRequest;
@@ -78,7 +77,6 @@ import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -134,7 +132,7 @@ public class MavenIndexManager implements ArchivaIndexManager
     private static final int MAX_WAIT = 10;
 
 
-    IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
+    public static IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
     {
         if ( !context.supports( IndexingContext.class ) )
         {
@@ -226,10 +224,10 @@ public class MavenIndexManager implements ArchivaIndexManager
     }
 
     @Override
-    public void scan( final ArchivaIndexingContext context, final boolean update ) throws IndexUpdateFailedException
+    public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
     {
         executeUpdateFunction( context, indexingContext -> {
-            DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, update, null );
+            DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
             ScanningRequest request = new ScanningRequest( indexingContext, listener );
             ScanningResult result = scanner.scan( request );
             if ( result.hasExceptions( ) )
@@ -383,10 +381,11 @@ public class MavenIndexManager implements ArchivaIndexManager
     }
 
     @Override
-    public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<Path> artifactReference ) throws IndexUpdateFailedException
+    public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
     {
+        final URI ctxUri = context.getPath();
         executeUpdateFunction(context, indexingContext -> {
-            Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+            Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
             try {
                 indexer.addArtifactsToIndex(artifacts, indexingContext);
             } catch (IOException e) {
@@ -398,10 +397,11 @@ public class MavenIndexManager implements ArchivaIndexManager
     }
 
     @Override
-    public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<Path> artifactReference ) throws IndexUpdateFailedException
+    public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
     {
+        final URI ctxUri = context.getPath();
         executeUpdateFunction(context, indexingContext -> {
-            Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+            Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
             try {
                 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
             } catch (IOException e) {
index a70c584d3af482e8b7c070b1fae14ec573ca061e..96876ad951caecd9b7cd42c32fc4b67cd2dcfcba 100644 (file)
@@ -19,21 +19,36 @@ package org.apache.archiva.indexer.maven;
 * under the License.
 */
 
+import org.apache.archiva.common.utils.FileUtils;
 import org.apache.archiva.indexer.ArchivaIndexingContext;
-import org.apache.archiva.repository.ManagedRepository;
+import org.apache.archiva.indexer.IndexCreationFailedException;
+import org.apache.archiva.repository.RepositoryType;
 import org.apache.archiva.repository.features.IndexCreationFeature;
 import org.apache.archiva.repository.maven2.MavenManagedRepository;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
+import org.apache.maven.index.MAVEN;
+import org.apache.maven.index.QueryCreator;
+import org.apache.maven.index.context.IndexingContext;
+import org.apache.maven.index.expr.UserInputSearchExpression;
+import org.apache.maven.index_shaded.lucene.search.BooleanClause;
+import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
+import org.apache.maven.index_shaded.lucene.search.Query;
+import org.junit.After;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 
 import javax.inject.Inject;
 
+import java.io.IOException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
 
 import static org.junit.Assert.*;
 
@@ -41,10 +56,31 @@ import static org.junit.Assert.*;
 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
 public class MavenIndexManagerTest {
 
+    private Path indexPath;
+    private MavenManagedRepository repository;
+    private ArchivaIndexingContext ctx;
+
     @Inject
     MavenIndexManager mavenIndexManager;
 
+    @Inject
+    QueryCreator queryCreator;
+
+
+    @After
+    public void tearDown() {
+        if (ctx!=null) {
+            try {
+                ctx.close(true);
+            } catch (IOException e) {
+                //
+            }
+        }
+        if (indexPath!=null && Files.exists(indexPath)) {
+            FileUtils.deleteQuietly(indexPath);
+        }
 
+    }
 
     @Test
     public void pack() throws Exception {
@@ -52,6 +88,17 @@ public class MavenIndexManagerTest {
 
     @Test
     public void scan() throws Exception {
+        createTestContext();
+        Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-webapp/1.0");
+        Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
+        org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
+        mavenIndexManager.scan(ctx);
+
+        IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+        String term = "org.apache.archiva";
+        Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+                BooleanClause.Occur.SHOULD ).build();
+        assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
     }
 
     @Test
@@ -60,29 +107,71 @@ public class MavenIndexManagerTest {
 
     @Test
     public void addArtifactsToIndex() throws Exception {
+
+        ArchivaIndexingContext ctx = createTestContext();
+        Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
+        Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
+        org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
+        List<URI> uriList = new ArrayList<>();
+        uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
+        uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
+        mavenIndexManager.addArtifactsToIndex(ctx, uriList);
+
+        IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+        String term = "org.apache.archiva";
+        Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+                BooleanClause.Occur.SHOULD ).build();
+        assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
     }
 
     @Test
     public void removeArtifactsFromIndex() throws Exception {
+        ArchivaIndexingContext ctx = createTestContext();
+        Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
+        Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
+        org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
+        List<URI> uriList = new ArrayList<>();
+        uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
+        uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
+        mavenIndexManager.addArtifactsToIndex(ctx, uriList);
+
+        IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+        String term = "org.apache.archiva";
+        Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+                BooleanClause.Occur.SHOULD ).build();
+        assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
+        uriList.remove(0);
+        mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
+        assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
     }
 
     @Test
     public void supportsRepository() throws Exception {
+        assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
+        assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
     }
 
-    @Test
-    public void createContext() throws Exception {
-        MavenManagedRepository repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
+    private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
+        indexPath = Paths.get("target/repositories/test-repo/.index-test");
+        FileUtils.deleteDirectory(indexPath);
+        repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
         repository.setLocation(new URI("test-repo"));
         IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
         icf.setIndexPath(new URI(".index-test"));
-        ArchivaIndexingContext ctx = mavenIndexManager.createContext(repository);
+        ctx = mavenIndexManager.createContext(repository);
+        return ctx;
+    }
+
+    @Test
+    public void createContext() throws Exception {
+        ArchivaIndexingContext ctx = createTestContext();
         assertNotNull(ctx);
         assertEquals(repository, ctx.getRepository());
         assertEquals("test-repo", ctx.getId());
-        Path indexPath = Paths.get("target/repositories/test-repo/.index-test");
         assertEquals(indexPath.toAbsolutePath(), Paths.get(ctx.getPath()).toAbsolutePath());
         assertTrue(Files.exists(indexPath));
+        List<Path> li = Files.list(indexPath).collect(Collectors.toList());
+        assertTrue(li.size()>0);
 
     }
 
index 96e5780422a9e0fe7851b8f1c00fce1820882989..5e870d04fa32436a6734ddcf79e7b828507b930d 100644 (file)
@@ -19,13 +19,10 @@ package org.apache.archiva.indexer;
  * under the License.
  */
 
-import org.apache.archiva.model.ArtifactReference;
 import org.apache.archiva.repository.Repository;
 import org.apache.archiva.repository.RepositoryType;
 
-import java.io.IOException;
 import java.net.URI;
-import java.nio.file.Path;
 import java.util.Collection;
 
 public interface ArchivaIndexManager {
@@ -39,9 +36,9 @@ public interface ArchivaIndexManager {
     /**
      * Rescans the whole repository, this index is associated to.
      * @param context
-     * @param update
+     *
      */
-    void scan(ArchivaIndexingContext context, boolean update) throws IndexUpdateFailedException;
+    void scan(ArchivaIndexingContext context) throws IndexUpdateFailedException;
 
     /**
      * Updates the index from the remote url.
@@ -56,14 +53,14 @@ public interface ArchivaIndexManager {
      * @param context
      * @param artifactReference
      */
-    void addArtifactsToIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) throws IndexUpdateFailedException;
+    void addArtifactsToIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) throws IndexUpdateFailedException;
 
     /**
      * Removes a list of artifacts from the index.
      * @param context
      * @param artifactReference
      */
-    void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) throws IndexUpdateFailedException;
+    void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) throws IndexUpdateFailedException;
 
 
     /**
index 931c60ac64cfca4b704db7174a1c3eaee3aefe22..02069773f2dd6da0c572628dea22f9b5eba21aec 100644 (file)
@@ -19,13 +19,11 @@ package org.apache.archiva.indexer;
  * under the License.
  */
 
-import org.apache.archiva.model.ArtifactReference;
 import org.apache.archiva.repository.Repository;
 import org.apache.archiva.repository.RepositoryType;
 import org.springframework.stereotype.Service;
 
 import java.net.URI;
-import java.nio.file.Path;
 import java.util.Collection;
 
 @Service("indexManager#none")
@@ -37,7 +35,7 @@ public class GenericIndexManager implements ArchivaIndexManager {
     }
 
     @Override
-    public void scan(ArchivaIndexingContext context, boolean update) {
+    public void scan(ArchivaIndexingContext context) {
 
     }
 
@@ -47,12 +45,12 @@ public class GenericIndexManager implements ArchivaIndexManager {
     }
 
     @Override
-    public void addArtifactsToIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) {
+    public void addArtifactsToIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) {
 
     }
 
     @Override
-    public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<Path> artifactReference) {
+    public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection<URI> artifactReference) {
 
     }