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