]> source.dussan.org Git - archiva.git/blob
b94e6b8e5200f38e47dcb8ea397d802f28b3870d
[archiva.git] /
1 package org.apache.archiva.indexer.maven.search;
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 junit.framework.TestCase;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.ConfigurationListener;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.indexer.ArchivaIndexingContext;
29 import org.apache.archiva.indexer.search.SearchResultHit;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.proxy.ProxyRegistry;
32 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
33 import org.apache.archiva.repository.Repository;
34 import org.apache.archiva.repository.features.IndexCreationFeature;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.commons.lang3.SystemUtils;
37 import org.apache.maven.index.ArtifactContext;
38 import org.apache.maven.index.ArtifactContextProducer;
39 import org.apache.maven.index.ArtifactScanningListener;
40 import org.apache.maven.index.DefaultScannerListener;
41 import org.apache.maven.index.Indexer;
42 import org.apache.maven.index.IndexerEngine;
43 import org.apache.maven.index.QueryCreator;
44 import org.apache.maven.index.Scanner;
45 import org.apache.maven.index.ScanningRequest;
46 import org.apache.maven.index.ScanningResult;
47 import org.apache.maven.index.context.IndexingContext;
48 import org.easymock.EasyMock;
49 import org.easymock.IMocksControl;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.runner.RunWith;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.test.context.ContextConfiguration;
56
57 import javax.inject.Inject;
58 import java.io.IOException;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
62 import java.util.List;
63 import java.util.Locale;
64
65 import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
66
67 /**
68  * @author Olivier Lamy
69  */
70 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
71 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
72 public abstract class AbstractMavenRepositorySearch
73     extends TestCase
74 {
75
76     protected Logger log = LoggerFactory.getLogger( getClass() );
77
78     public static String TEST_REPO_1 = "maven-search-test-repo";
79
80     public static String TEST_REPO_2 = "maven-search-test-repo-2";
81
82
83     public static String REPO_RELEASE = "repo-release";
84
85     MavenRepositorySearch search;
86
87     ArchivaConfiguration archivaConfig;
88
89     @Inject
90     ArtifactContextProducer artifactContextProducer;
91
92     @Inject
93     ArchivaRepositoryRegistry repositoryRegistry;
94
95     @Inject
96     ProxyRegistry proxyRegistry;
97
98     @Inject
99     private IndexerEngine indexerEngine;
100
101     IMocksControl archivaConfigControl;
102
103     Configuration config;
104
105     @Inject
106     Indexer indexer;
107
108     @Inject
109     Scanner scanner;
110
111     @Inject
112     QueryCreator queryCreator;
113
114     @Before
115     @Override
116     public void setUp()
117         throws Exception
118     {
119         super.setUp();
120
121         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
122         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
123
124         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
125         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
126
127         archivaConfigControl = EasyMock.createControl();
128
129         archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
130
131         repositoryRegistry.setArchivaConfiguration( archivaConfig );
132
133         search = new MavenRepositorySearch( indexer, repositoryRegistry, proxyRegistry,
134                                             queryCreator );
135
136         assertNotNull( repositoryRegistry );
137
138         config = new Configuration();
139         config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
140         config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
141         config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
142
143         archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
144         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
145         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
146         archivaConfig.save(EasyMock.anyObject(Configuration.class));
147         EasyMock.expectLastCall().anyTimes();
148         archivaConfigControl.replay();
149         repositoryRegistry.reload();
150
151     }
152
153     @After
154     @Override
155     public void tearDown()
156         throws Exception
157     {
158         archivaConfigControl.reset();
159         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
160         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
161         archivaConfig.save(EasyMock.anyObject(Configuration.class));
162         EasyMock.expectLastCall().anyTimes();
163         archivaConfigControl.replay();
164         repositoryRegistry.removeRepository(TEST_REPO_1);
165         repositoryRegistry.removeRepository(TEST_REPO_2);
166         repositoryRegistry.removeRepository(REPO_RELEASE);
167         repositoryRegistry.destroy();
168         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
169         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
170
171         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
172         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
173
174         super.tearDown();
175     }
176
177     protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
178     {
179         ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
180         repositoryConfig.setId( repository );
181         repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
182         Path f = Paths.get( repositoryConfig.getLocation() );
183         if ( !Files.exists(f) )
184         {
185             try
186             {
187                 Files.createDirectories( f );
188             }
189             catch ( IOException e )
190             {
191                 log.error("Could not create directories for {}", f);
192             }
193         }
194         repositoryConfig.setLayout( "default" );
195         repositoryConfig.setName( repository );
196         repositoryConfig.setScanned( true );
197         repositoryConfig.setSnapshots( false );
198         repositoryConfig.setReleases( true );
199         repositoryConfig.setIndexDir(DEFAULT_INDEX_PATH);
200
201         return repositoryConfig;
202     }
203
204     protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan) throws Exception {
205         createIndex(repository, filesToBeIndexed, scan, null, true);
206     }
207
208     protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles)
209         throws Exception
210     {
211         Repository rRepo = repositoryRegistry.getRepository(repository);
212         IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
213
214
215         ArchivaIndexingContext archivaCtx = rRepo.getIndexingContext();
216         IndexingContext context = archivaCtx.getBaseContext(IndexingContext.class);
217
218         if ( archivaCtx != null )
219         {
220             archivaCtx.close(true);
221         }
222
223         Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
224
225         Path indexerDirectory = repoDir.resolve(".indexer" );
226
227         if ( Files.exists(indexerDirectory) )
228         {
229             FileUtils.deleteDirectory( indexerDirectory );
230         }
231
232         assertFalse( Files.exists(indexerDirectory) );
233
234         Path lockFile = repoDir.resolve(".indexer/write.lock" );
235         if ( Files.exists(lockFile) )
236         {
237             Files.delete(lockFile);
238         }
239         assertFalse( Files.exists(lockFile) );
240         if (indexDir==null) {
241             Path indexDirectory =
242                     Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
243             indexDirectory.toFile().deleteOnExit();
244             FileUtils.deleteDirectory(indexDirectory);
245             icf.setIndexPath(indexDirectory.toUri());
246         } else {
247
248             icf.setIndexPath(indexDir.toUri());
249         }
250         if (copyFiles) {
251             Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
252             assertTrue(Files.exists(repo));
253             org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
254         }
255
256
257
258
259         archivaConfigControl.reset();
260         archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
261         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
262         archivaConfig.save(EasyMock.anyObject(Configuration.class));
263         EasyMock.expectLastCall().anyTimes();
264         archivaConfigControl.replay();
265         repositoryRegistry.reload();
266         archivaConfigControl.reset();
267
268         rRepo = repositoryRegistry.getRepository(repository);
269         icf = rRepo.getFeature(IndexCreationFeature.class).get();
270
271
272         archivaCtx = rRepo.getIndexingContext();
273         context = archivaCtx.getBaseContext(IndexingContext.class);
274
275
276         // minimize datas in memory
277 //        context.getIndexWriter().setMaxBufferedDocs( -1 );
278 //        context.getIndexWriter().setRAMBufferSizeMB( 1 );
279         for ( Path artifactFile : filesToBeIndexed )
280         {
281             assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
282             ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
283
284             if ( artifactFile.toString().endsWith( ".pom" ) )
285             {
286                 ac.getArtifactInfo().setFileExtension( "pom" );
287                 ac.getArtifactInfo().setPackaging( "pom" );
288                 ac.getArtifactInfo().setClassifier( "pom" );
289             }
290             indexer.addArtifactToIndex( ac, context );
291             context.updateTimestamp( true );
292         }
293
294         if ( scan )
295         {
296             DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, new ArtifactScanListener());
297             ScanningRequest req = new ScanningRequest(context, listener );
298             scanner.scan( req );
299             context.commit();
300         }
301         // force flushing
302         context.commit();
303         //  context.getIndexWriter().commit();
304         context.setSearchable( true );
305
306     }
307
308     static class ArtifactScanListener
309         implements ArtifactScanningListener
310     {
311         protected Logger log = LoggerFactory.getLogger( getClass() );
312
313         @Override
314         public void scanningStarted( IndexingContext ctx )
315         {
316             //
317         }
318
319         @Override
320         public void scanningFinished( IndexingContext ctx, ScanningResult result )
321         {
322             // no op
323         }
324
325         @Override
326         public void artifactError( ArtifactContext ac, Exception e )
327         {
328             log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
329         }
330
331         @Override
332         public void artifactDiscovered( ArtifactContext ac )
333         {
334             log.debug( "artifactDiscovered {}:{}", //
335                        ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
336                        ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
337         }
338     }
339
340     public String niceDisplay( SearchResults searchResults )
341         throws Exception
342     {
343         StringBuilder sb = new StringBuilder();
344         for ( SearchResultHit hit : searchResults.getHits() )
345         {
346             sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
347         }
348         return sb.toString();
349     }
350 }