]> source.dussan.org Git - archiva.git/blob
96876ad951caecd9b7cd42c32fc4b67cd2dcfcba
[archiva.git] /
1 package org.apache.archiva.indexer.maven;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements.  See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership.  The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License.  You may obtain a copy of the License at
11 *
12 *  http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied.  See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.IndexCreationFailedException;
25 import org.apache.archiva.repository.RepositoryType;
26 import org.apache.archiva.repository.features.IndexCreationFeature;
27 import org.apache.archiva.repository.maven2.MavenManagedRepository;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.QueryCreator;
31 import org.apache.maven.index.context.IndexingContext;
32 import org.apache.maven.index.expr.UserInputSearchExpression;
33 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
34 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
35 import org.apache.maven.index_shaded.lucene.search.Query;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.springframework.test.context.ContextConfiguration;
40
41 import javax.inject.Inject;
42
43 import java.io.IOException;
44 import java.net.URI;
45 import java.net.URISyntaxException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.stream.Collectors;
52
53 import static org.junit.Assert.*;
54
55 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
57 public class MavenIndexManagerTest {
58
59     private Path indexPath;
60     private MavenManagedRepository repository;
61     private ArchivaIndexingContext ctx;
62
63     @Inject
64     MavenIndexManager mavenIndexManager;
65
66     @Inject
67     QueryCreator queryCreator;
68
69
70     @After
71     public void tearDown() {
72         if (ctx!=null) {
73             try {
74                 ctx.close(true);
75             } catch (IOException e) {
76                 //
77             }
78         }
79         if (indexPath!=null && Files.exists(indexPath)) {
80             FileUtils.deleteQuietly(indexPath);
81         }
82
83     }
84
85     @Test
86     public void pack() throws Exception {
87     }
88
89     @Test
90     public void scan() throws Exception {
91         createTestContext();
92         Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-webapp/1.0");
93         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
94         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
95         mavenIndexManager.scan(ctx);
96
97         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
98         String term = "org.apache.archiva";
99         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
100                 BooleanClause.Occur.SHOULD ).build();
101         assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
102     }
103
104     @Test
105     public void update() throws Exception {
106     }
107
108     @Test
109     public void addArtifactsToIndex() throws Exception {
110
111         ArchivaIndexingContext ctx = createTestContext();
112         Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
113         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
114         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
115         List<URI> uriList = new ArrayList<>();
116         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
117         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
118         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
119
120         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
121         String term = "org.apache.archiva";
122         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
123                 BooleanClause.Occur.SHOULD ).build();
124         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
125     }
126
127     @Test
128     public void removeArtifactsFromIndex() throws Exception {
129         ArchivaIndexingContext ctx = createTestContext();
130         Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
131         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
132         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
133         List<URI> uriList = new ArrayList<>();
134         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
135         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
136         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
137
138         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
139         String term = "org.apache.archiva";
140         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
141                 BooleanClause.Occur.SHOULD ).build();
142         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
143         uriList.remove(0);
144         mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
145         assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
146     }
147
148     @Test
149     public void supportsRepository() throws Exception {
150         assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
151         assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
152     }
153
154     private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
155         indexPath = Paths.get("target/repositories/test-repo/.index-test");
156         FileUtils.deleteDirectory(indexPath);
157         repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
158         repository.setLocation(new URI("test-repo"));
159         IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
160         icf.setIndexPath(new URI(".index-test"));
161         ctx = mavenIndexManager.createContext(repository);
162         return ctx;
163     }
164
165     @Test
166     public void createContext() throws Exception {
167         ArchivaIndexingContext ctx = createTestContext();
168         assertNotNull(ctx);
169         assertEquals(repository, ctx.getRepository());
170         assertEquals("test-repo", ctx.getId());
171         assertEquals(indexPath.toAbsolutePath(), Paths.get(ctx.getPath()).toAbsolutePath());
172         assertTrue(Files.exists(indexPath));
173         List<Path> li = Files.list(indexPath).collect(Collectors.toList());
174         assertTrue(li.size()>0);
175
176     }
177
178 }