]> source.dussan.org Git - archiva.git/blob
e2e6091434509cdb12a89201bf61bb004eb43fe6
[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.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.RepositoryType;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.repository.maven2.MavenManagedRepository;
30 import org.apache.archiva.repository.maven2.MavenRemoteRepository;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.maven.index.MAVEN;
33 import org.apache.maven.index.QueryCreator;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.expr.UserInputSearchExpression;
36 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
37 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
38 import org.apache.maven.index_shaded.lucene.search.Query;
39 import org.junit.After;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.springframework.test.context.ContextConfiguration;
43
44 import javax.inject.Inject;
45 import java.io.IOException;
46 import java.net.URI;
47 import java.net.URISyntaxException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.stream.Collectors;
54
55 import static org.junit.Assert.*;
56
57 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
58 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
59 public class MavenIndexManagerTest {
60
61     @Inject
62     ArchivaRepositoryRegistry repositoryRegistry;
63
64
65     private Path indexPath;
66     private MavenManagedRepository repository;
67     private ArchivaIndexingContext ctx;
68     private MavenRemoteRepository repositoryRemote;
69
70     @Inject
71     MavenIndexManager mavenIndexManager;
72
73     @Inject
74     QueryCreator queryCreator;
75
76
77     @After
78     public void tearDown() {
79         repositoryRegistry.destroy();
80         if (ctx!=null) {
81             try {
82                 ctx.close(true);
83             } catch (IOException e) {
84                 //
85             }
86         }
87         if (indexPath!=null && Files.exists(indexPath)) {
88             FileUtils.deleteQuietly(indexPath);
89         }
90
91     }
92
93     @Test
94     public void pack() throws Exception {
95         createTestContext();
96         Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
97         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
98         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
99         mavenIndexManager.scan(ctx);
100         mavenIndexManager.pack(ctx);
101         assertTrue(Files.list(indexPath).filter(path -> {
102             try {
103                 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
104             } catch (IOException e) {
105                 return false;
106             }
107         }).findAny().isPresent());
108     }
109
110     @Test
111     public void scan() throws Exception {
112         createTestContext();
113         Path destDir = repository.getAsset("").getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
114         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
115         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
116         mavenIndexManager.scan(ctx);
117
118         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
119         String term = "org.apache.archiva";
120         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
121                 BooleanClause.Occur.SHOULD ).build();
122         assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
123     }
124
125     /*
126      * Does only a index update via file uri, no HTTP uri
127      */
128     @Test
129     public void update() throws Exception {
130         createTestContext();
131         mavenIndexManager.pack(ctx);
132         ctx.close(false);
133         createTestContextForRemote();
134         mavenIndexManager.update(ctx, true);
135     }
136
137     @Test
138     public void addArtifactsToIndex() throws Exception {
139
140         ArchivaIndexingContext ctx = createTestContext();
141         Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
142         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
143         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
144         List<URI> uriList = new ArrayList<>();
145         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
146         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
147         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
148
149         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
150         String term = "org.apache.archiva";
151         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
152                 BooleanClause.Occur.SHOULD ).build();
153         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
154     }
155
156     @Test
157     public void removeArtifactsFromIndex() throws Exception {
158         ArchivaIndexingContext ctx = createTestContext();
159         Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
160         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
161         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
162         List<URI> uriList = new ArrayList<>();
163         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
164         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
165         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
166
167         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
168         String term = "org.apache.archiva";
169         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
170                 BooleanClause.Occur.SHOULD ).build();
171         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
172         uriList.remove(0);
173         mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
174         assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
175     }
176
177     @Test
178     public void supportsRepository() throws Exception {
179         assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
180         assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
181     }
182
183     private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
184         indexPath = Paths.get("target/repositories/test-repo/.index-test");
185         FileUtils.deleteDirectory(indexPath);
186         repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
187         // repository.setLocation(new URI("test-repo"));
188         IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
189         icf.setIndexPath(new URI(".index-test"));
190         ctx = mavenIndexManager.createContext(repository);
191         return ctx;
192     }
193
194     private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
195         indexPath = Paths.get("target/repositories/test-repo/.index-test");
196         Path repoPath = Paths.get("target/repositories").toAbsolutePath();
197         repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
198         repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
199         RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
200         icf.setIndexUri(new URI(".index-test"));
201         ctx = mavenIndexManager.createContext(repositoryRemote);
202         return ctx;
203     }
204
205     @Test
206     public void createContext() throws Exception {
207         ArchivaIndexingContext ctx = createTestContext();
208         assertNotNull(ctx);
209         assertEquals(repository, ctx.getRepository());
210         assertEquals("test-repo", ctx.getId());
211         assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
212         assertTrue(Files.exists(indexPath));
213         List<Path> li = Files.list(indexPath).collect(Collectors.toList());
214         assertTrue(li.size()>0);
215
216     }
217
218 }