]> source.dussan.org Git - archiva.git/blob
81f518d3e46f89db3f600fec2347d8283c2d02a9
[archiva.git] /
1 package org.apache.archiva.maven.indexer.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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import junit.framework.TestCase;
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.ConfigurationListener;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.indexer.ArchivaIndexingContext;
28 import org.apache.archiva.indexer.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.maven.indexer.search.MavenRepositorySearch;
31 import org.apache.archiva.proxy.ProxyRegistry;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
34 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
35 import org.apache.archiva.repository.features.IndexCreationFeature;
36 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import org.apache.commons.lang3.SystemUtils;
38 import org.apache.maven.index.ArtifactContext;
39 import org.apache.maven.index.ArtifactContextProducer;
40 import org.apache.maven.index.ArtifactScanningListener;
41 import org.apache.maven.index.DefaultScannerListener;
42 import org.apache.maven.index.Indexer;
43 import org.apache.maven.index.IndexerEngine;
44 import org.apache.maven.index.QueryCreator;
45 import org.apache.maven.index.Scanner;
46 import org.apache.maven.index.ScanningRequest;
47 import org.apache.maven.index.ScanningResult;
48 import org.apache.maven.index.context.IndexingContext;
49 import org.apache.maven.index_shaded.lucene.index.IndexUpgrader;
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     @SuppressWarnings( "unused" )
98     @Inject
99     RepositoryHandlerDependencies repositoryHandlerDependencies;
100
101     @Inject
102     ProxyRegistry proxyRegistry;
103
104     @Inject
105     private IndexerEngine indexerEngine;
106
107     IMocksControl archivaConfigControl;
108
109     Configuration config;
110
111     @Inject
112     Indexer indexer;
113
114     @Inject
115     Scanner scanner;
116
117     @Inject
118     QueryCreator queryCreator;
119
120     @Before
121     @Override
122     public void setUp()
123         throws Exception
124     {
125         super.setUp();
126
127         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
128         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" )) );
129         Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 + "/.indexer" ) );
130
131         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
132         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" )) );
133         Files.createDirectories( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 + "/.indexer" ) );
134
135         archivaConfigControl = EasyMock.createControl();
136
137         archivaConfig = archivaConfigControl.createMock( ArchivaConfiguration.class );
138
139         repositoryRegistry.setArchivaConfiguration( archivaConfig );
140
141         search = new MavenRepositorySearch( indexer, repositoryRegistry, proxyRegistry,
142                                             queryCreator );
143
144         assertNotNull( repositoryRegistry );
145
146         config = new Configuration();
147         config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
148         config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
149         config.addManagedRepository( createRepositoryConfig( REPO_RELEASE ) );
150
151         archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
152         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
153         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
154         archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
155         EasyMock.expectLastCall().anyTimes();
156         archivaConfigControl.replay();
157         repositoryRegistry.reload();
158
159     }
160
161     @After
162     @Override
163     public void tearDown()
164         throws Exception
165     {
166         archivaConfigControl.reset();
167         EasyMock.expect( archivaConfig.getDefaultLocale() ).andReturn( Locale.getDefault( ) ).anyTimes();
168         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
169         archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
170         EasyMock.expectLastCall().anyTimes();
171         archivaConfigControl.replay();
172         repositoryRegistry.removeRepository(TEST_REPO_1);
173         repositoryRegistry.removeRepository(TEST_REPO_2);
174         repositoryRegistry.removeRepository(REPO_RELEASE);
175         repositoryRegistry.destroy();
176         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 ) );
177         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_1 )) );
178
179         FileUtils.deleteDirectory( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 ) );
180         assertFalse( Files.exists(Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "/target/repos/" + TEST_REPO_2 )) );
181
182         super.tearDown();
183     }
184
185     protected ManagedRepositoryConfiguration createRepositoryConfig( String repository )
186     {
187         ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
188         repositoryConfig.setId( repository );
189         repositoryConfig.setLocation( org.apache.archiva.common.utils.FileUtils.getBasedir() + "/target/repos/" + repository );
190         Path f = Paths.get( repositoryConfig.getLocation() );
191         if ( !Files.exists(f) )
192         {
193             try
194             {
195                 Files.createDirectories( f );
196             }
197             catch ( IOException e )
198             {
199                 log.error("Could not create directories for {}", f);
200             }
201         }
202         repositoryConfig.setLayout( "default" );
203         repositoryConfig.setName( repository );
204         repositoryConfig.setScanned( true );
205         repositoryConfig.setSnapshots( false );
206         repositoryConfig.setReleases( true );
207         repositoryConfig.setIndexDir(DEFAULT_INDEX_PATH);
208
209         return repositoryConfig;
210     }
211
212     protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan) throws Exception {
213         createIndex(repository, filesToBeIndexed, scan, null, true);
214     }
215
216     protected void createIndex( String repository, List<Path> filesToBeIndexed, boolean scan, Path indexDir, boolean copyFiles)
217         throws Exception
218     {
219         final Repository rRepo = repositoryRegistry.getRepository(repository);
220         IndexCreationFeature icf = rRepo.getFeature(IndexCreationFeature.class).get();
221
222
223         ArchivaIndexingContext archivaCtx = rRepo.getIndexingContext();
224         IndexingContext context = archivaCtx.getBaseContext(IndexingContext.class);
225
226         if ( archivaCtx != null )
227         {
228             archivaCtx.close(true);
229         }
230
231         Path repoDir = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir()).resolve("target").resolve("repos").resolve(repository);
232
233         Path indexerDirectory = repoDir.resolve(".indexer" );
234
235         if ( indexDir == null && Files.exists(indexerDirectory) )
236         {
237             FileUtils.deleteDirectory( indexerDirectory );
238             assertFalse( Files.exists(indexerDirectory) );
239         }
240
241
242         Path lockFile = repoDir.resolve(".indexer/write.lock" );
243         if ( Files.exists(lockFile) )
244         {
245             Files.delete(lockFile);
246         }
247         assertFalse( Files.exists(lockFile) );
248         if (indexDir==null) {
249             Path indexDirectory =
250                     Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/index/test-" + Long.toString(System.currentTimeMillis()));
251             indexDirectory.toFile().deleteOnExit();
252             FileUtils.deleteDirectory(indexDirectory);
253             icf.setIndexPath(indexDirectory.toUri());
254             config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r ->
255                 r.setIndexDir( indexDirectory.toAbsolutePath( ).toString( ) )
256             );
257             // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString()});
258         } else {
259
260             icf.setIndexPath(indexDir.toUri());
261             Files.createDirectories( indexDir );
262             config.getManagedRepositories( ).stream( ).filter( r -> r.getId( ).equals( rRepo.getId( ) ) ).findFirst( ).ifPresent( r ->
263                 r.setIndexDir( indexDir.toAbsolutePath( ).toString( ) )
264             );
265             IndexUpgrader.main(new String[]{indexDir.toAbsolutePath().toString()});
266
267         }
268
269         if (copyFiles) {
270             Path repo = Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/" + repository);
271             assertTrue(Files.exists(repo));
272             org.apache.commons.io.FileUtils.copyDirectory(repo.toFile(), repoDir.toFile());
273         }
274
275
276
277
278         archivaConfigControl.reset();
279         archivaConfig.addListener( EasyMock.anyObject( ConfigurationListener.class ) );
280         EasyMock.expect( archivaConfig.getConfiguration() ).andReturn(config).anyTimes();
281         archivaConfig.save(EasyMock.anyObject(Configuration.class), EasyMock.anyString());
282         EasyMock.expectLastCall().anyTimes();
283         archivaConfigControl.replay();
284         repositoryRegistry.reload();
285         archivaConfigControl.reset();
286
287         Repository rRepo2 = repositoryRegistry.getRepository( repository );
288         icf = rRepo2.getFeature(IndexCreationFeature.class).get();
289
290
291         archivaCtx = rRepo2.getIndexingContext();
292         context = archivaCtx.getBaseContext(IndexingContext.class);
293
294
295         // minimize datas in memory
296 //        context.getIndexWriter().setMaxBufferedDocs( -1 );
297 //        context.getIndexWriter().setRAMBufferSizeMB( 1 );
298         for ( Path artifactFile : filesToBeIndexed )
299         {
300             assertTrue( "file not exists " + artifactFile, Files.exists(artifactFile) );
301             ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile() );
302
303             if ( artifactFile.toString().endsWith( ".pom" ) )
304             {
305                 ac.getArtifactInfo().setFileExtension( "pom" );
306                 ac.getArtifactInfo().setPackaging( "pom" );
307                 ac.getArtifactInfo().setClassifier( "pom" );
308             }
309             indexer.addArtifactToIndex( ac, context );
310             context.updateTimestamp( true );
311         }
312
313         if ( scan )
314         {
315             DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, new ArtifactScanListener());
316             ScanningRequest req = new ScanningRequest(context, listener );
317             scanner.scan( req );
318             context.commit();
319         }
320         // force flushing
321         context.commit();
322         //  context.getIndexWriter().commit();
323         context.setSearchable( true );
324
325     }
326
327     static class ArtifactScanListener
328         implements ArtifactScanningListener
329     {
330         protected Logger log = LoggerFactory.getLogger( getClass() );
331
332         @Override
333         public void scanningStarted( IndexingContext ctx )
334         {
335             //
336         }
337
338         @Override
339         public void scanningFinished( IndexingContext ctx, ScanningResult result )
340         {
341             // no op
342         }
343
344         @Override
345         public void artifactError( ArtifactContext ac, Exception e )
346         {
347             log.debug( "artifactError {}", ac.getArtifact().getPath(), e );
348         }
349
350         @Override
351         public void artifactDiscovered( ArtifactContext ac )
352         {
353             log.debug( "artifactDiscovered {}:{}", //
354                        ac.getArtifact() == null ? "" : ac.getArtifact().getPath(), //
355                        ac.getArtifact() == null ? "" : ac.getArtifactInfo() );
356         }
357     }
358
359     public String niceDisplay( SearchResults searchResults )
360         throws Exception
361     {
362         StringBuilder sb = new StringBuilder();
363         for ( SearchResultHit hit : searchResults.getHits() )
364         {
365             sb.append( hit.toString() ).append( SystemUtils.LINE_SEPARATOR );
366         }
367         return sb.toString();
368     }
369 }