]> source.dussan.org Git - archiva.git/blob
fe0aa1d41e2cb91b193548c86060671a35930575
[archiva.git] /
1 package org.apache.maven.repository.indexing;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.apache.maven.model.Model;
25 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
26 import org.apache.maven.repository.digest.DefaultDigester;
27 import org.apache.maven.repository.digest.Digester;
28 import org.apache.maven.repository.indexing.query.CompoundQuery;
29 import org.apache.maven.repository.indexing.query.Query;
30 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
31 import org.codehaus.plexus.PlexusTestCase;
32 import org.codehaus.plexus.util.FileUtils;
33
34 import java.io.File;
35 import java.io.FileReader;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40
41 /**
42  * @author Edwin Punzalan
43  */
44 public class PomRepositoryIndexingTest
45     extends PlexusTestCase
46 {
47     private ArtifactRepository repository;
48
49     private ArtifactFactory artifactFactory;
50
51     private File indexPath;
52
53     private Digester digester;
54
55     protected void setUp()
56         throws Exception
57     {
58         super.setUp();
59
60         File repositoryDirectory = getTestFile( "src/test/repository" );
61         String repoDir = repositoryDirectory.toURL().toString();
62         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
63         ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
64         repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
65         digester = new DefaultDigester();
66
67         indexPath = getTestFile( "target/index" );
68         FileUtils.deleteDirectory( indexPath );
69     }
70
71
72     public void testIndexerExceptions()
73         throws Exception
74     {
75         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
76         Model pom = getPom( "test", "test-artifactId", "1.0" );
77
78         try
79         {
80             File notIndexDir = new File( "pom.xml" );
81             PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
82             indexer.indexPom( pom );
83             fail( "Must throw exception on non-directory index directory" );
84         }
85         catch ( RepositoryIndexException e )
86         {
87             assertTrue( true );
88         }
89
90         try
91         {
92             File notIndexDir = new File( "" );
93             PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
94             indexer.indexPom( pom );
95             fail( "Must throw an exception on a non-index directory" );
96         }
97         catch ( RepositoryIndexException e )
98         {
99             assertTrue( true );
100         }
101
102         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
103         try
104         {
105             indexer.deleteIfIndexed( new Object() );
106             fail( "Must throw exception when the passed object is not of type model." );
107         }
108         catch ( RepositoryIndexException e )
109         {
110             assertTrue( true );
111         }
112     }
113
114     /**
115      * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using a single-phrase search.
116      *
117      * @throws Exception
118      */
119     public void testSearchSingle()
120         throws Exception
121     {
122         createTestIndex();
123
124         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
125         RepositoryIndexSearchLayer repoSearchLayer =
126             (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
127
128         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
129
130         // search version
131         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
132         List artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
133         assertEquals( 1, artifactList.size() );
134         for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
135         {
136             SearchResult result = (SearchResult) iter.next();
137             Artifact artifact = result.getArtifact();
138             assertEquals( "1.0", artifact.getVersion() );
139         }
140
141         // search group id
142         qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
143         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
144         assertEquals( 2, artifactList.size() );
145         Iterator artifacts = artifactList.iterator();
146         if ( artifacts.hasNext() )
147         {
148             SearchResult result = (SearchResult) artifacts.next();
149             Artifact artifact = result.getArtifact();
150             assertEquals( "org.apache.maven", artifact.getGroupId() );
151         }
152
153         // search artifact id
154         qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
155         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
156         assertEquals( 1, artifactList.size() );
157         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
158         {
159             SearchResult result = (SearchResult) artifacts.next();
160             Artifact artifact = result.getArtifact();
161             assertEquals( "maven-artifact", artifact.getArtifactId() );
162         }
163
164         // search version
165         qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
166         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
167         assertEquals( 2, artifactList.size() );
168         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
169         {
170             SearchResult result = (SearchResult) artifacts.next();
171             Artifact artifact = result.getArtifact();
172             assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
173         }
174
175         // search packaging
176         qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" );
177         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
178         assertEquals( 3, artifactList.size() );
179         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
180         {
181             SearchResult result = (SearchResult) artifacts.next();
182             Map map = result.getFieldMatches();
183             Set mapEntry = map.entrySet();
184             assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) );
185         }
186
187         //search license url
188         qry =
189             new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" );
190         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
191         assertEquals( 2, artifactList.size() );
192         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
193         {
194             SearchResult result = (SearchResult) artifacts.next();
195             Map map = result.getFieldMatches();
196             List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS );
197             for ( Iterator it = matches.iterator(); it.hasNext(); )
198             {
199                 assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() );
200             }
201         }
202
203         //search dependencies
204         qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
205         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
206         assertEquals( 2, artifactList.size() );
207         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
208         {
209             SearchResult result = (SearchResult) artifacts.next();
210             Map map = result.getFieldMatches();
211             boolean depFound = false;
212             List list = (List) map.get( RepositoryIndex.FLD_DEPENDENCIES );
213             Iterator dependencies = list.iterator();
214             while ( dependencies.hasNext() )
215             {
216                 String dep = (String) dependencies.next();
217                 if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) )
218                 {
219                     depFound = true;
220                     break;
221                 }
222             }
223             assertTrue( "Searched dependency not found.", depFound );
224         }
225
226         //search build plugin
227         qry =
228             new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" );
229         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
230         assertEquals( 1, artifactList.size() );
231         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
232         {
233             SearchResult result = (SearchResult) artifacts.next();
234             Map map = result.getFieldMatches();
235             List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD );
236             Iterator plugins = list.iterator();
237             boolean found = false;
238             while ( plugins.hasNext() )
239             {
240                 String plugin = (String) plugins.next();
241                 if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) )
242                 {
243                     found = true;
244                     break;
245                 }
246             }
247             assertTrue( "Searched plugin not found.", found );
248         }
249
250         //search reporting plugin
251         qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT,
252                                      "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
253         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
254         assertEquals( 1, artifactList.size() );
255         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
256         {
257             SearchResult result = (SearchResult) artifacts.next();
258             Map map = result.getFieldMatches();
259             List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT );
260             Iterator plugins = list.iterator();
261             boolean found = false;
262             while ( plugins.hasNext() )
263             {
264                 String plugin = (String) plugins.next();
265                 if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) )
266                 {
267                     found = true;
268                     break;
269                 }
270             }
271             assertTrue( "Searched report plugin not found.", found );
272         }
273
274         // search sha1 checksum
275         Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
276         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
277         String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
278
279         qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
280         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
281         assertEquals( 1, artifactList.size() );
282         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
283         {
284             SearchResult result = (SearchResult) artifacts.next();
285             Artifact artifact2 = result.getArtifact();
286             String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
287             assertEquals( sha1, sha1Tmp );
288         }
289
290         // search md5 checksum
291         String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 );
292         qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
293         artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
294         assertEquals( 1, artifactList.size() );
295         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
296         {
297             SearchResult result = (SearchResult) artifacts.next();
298             Artifact artifact2 = result.getArtifact();
299             String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 );
300             assertEquals( md5, md5Tmp );
301         }
302
303         indexer.close();
304     }
305
306     /**
307      * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using compound search (AND, OR).
308      *
309      * @throws Exception
310      */
311     public void testSearchCompound()
312         throws Exception
313     {
314         createTestIndex();
315
316         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
317         RepositoryIndexSearchLayer repoSearchLayer =
318             (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
319
320         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
321
322         // Criteria 1: required query
323         // ex. artifactId=maven-artifact AND groupId=org.apache.maven
324         Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
325         Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
326         CompoundQuery rQry = new CompoundQuery();
327         rQry.and( qry1 );
328         rQry.and( qry2 );
329
330         List artifacts = repoSearchLayer.searchAdvanced( rQry, indexer );
331         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
332         {
333             SearchResult result = (SearchResult) iter.next();
334             Artifact artifact = result.getArtifact();
335             assertEquals( "maven-artifact", artifact.getArtifactId() );
336             assertEquals( "org.apache.maven", artifact.getGroupId() );
337         }
338
339         // Criteria 2: nested required query
340         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
341         // version=2.0.3
342         Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
343         CompoundQuery oQry = new CompoundQuery();
344         oQry.and( rQry );
345         oQry.or( qry3 );
346
347         artifacts = repoSearchLayer.searchAdvanced( oQry, indexer );
348         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
349         {
350             SearchResult result = (SearchResult) iter.next();
351             Artifact artifact = result.getArtifact();
352             assertEquals( "maven-artifact", artifact.getArtifactId() );
353             assertEquals( "org.apache.maven", artifact.getGroupId() );
354         }
355
356         // Criteria 3: nested required query
357         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
358         // (version=2.0.3 OR version=2.0.1)
359         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
360         Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
361         oQry = new CompoundQuery();
362         oQry.or( qry3 );
363         oQry.or( qry4 );
364
365         CompoundQuery oQry5 = new CompoundQuery();
366         Query qry9 =
367             new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
368         Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES,
369                                              "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
370         oQry5.or( qry9 );
371         oQry5.or( qry10 );
372
373         CompoundQuery rQry2 = new CompoundQuery();
374         rQry2.or( oQry );
375         rQry2.and( rQry );
376         rQry2.and( oQry5 );
377
378         artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer );
379         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
380         {
381             SearchResult result = (SearchResult) iter.next();
382             Artifact artifact = result.getArtifact();
383             assertEquals( "maven-artifact", artifact.getArtifactId() );
384             assertEquals( "org.apache.maven", artifact.getGroupId() );
385             assertEquals( "2.0.1", artifact.getVersion() );
386         }
387
388         // Criteria 4: nested required query
389         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
390         // (version=2.0.3 OR version=2.0.1)
391         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
392         // OR [(artifactId=sample AND groupId=test)]
393         CompoundQuery rQry3 = new CompoundQuery();
394         Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
395         Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
396         rQry3.and( qry5 );
397         rQry3.and( qry6 );
398         CompoundQuery oQry2 = new CompoundQuery();
399         oQry2.and( rQry2 );
400         oQry2.and( rQry3 );
401
402         artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
403         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
404         {
405             SearchResult result = (SearchResult) iter.next();
406             Artifact artifact = result.getArtifact();
407             assertEquals( "maven-artifact", artifact.getArtifactId() );
408             assertEquals( "org.apache.maven", artifact.getGroupId() );
409             assertEquals( "2.0.1", artifact.getVersion() );
410         }
411
412         // Criteria 4: nested required query
413         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
414         // (version=2.0.3 OR version=2.0.1)
415         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
416         // [(artifactId=sample AND groupId=test)] OR
417         // [(artifactId=sample2 AND groupId=test)]
418         CompoundQuery rQry4 = new CompoundQuery();
419         Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
420         Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
421         rQry4.and( qry7 );
422         rQry4.and( qry8 );
423         oQry2.and( rQry4 );
424
425         artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
426         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
427         {
428             SearchResult result = (SearchResult) iter.next();
429             Artifact artifact = result.getArtifact();
430             assertEquals( "maven-artifact", artifact.getArtifactId() );
431             assertEquals( "org.apache.maven", artifact.getGroupId() );
432         }
433
434         indexer.close();
435     }
436
437     /**
438      * Create an index that will be used for testing.
439      * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
440      * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
441      *
442      * @throws Exception
443      */
444     private void createTestIndex()
445         throws Exception
446     {
447         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
448         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
449
450         Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
451         indexer.indexPom( pom );
452         indexer.optimize();
453         indexer.close();
454
455         pom = getPom( "org.apache.maven", "maven-model", "2.0" );
456         indexer.indexPom( pom );
457         indexer.optimize();
458         indexer.close();
459
460         pom = getPom( "test", "test-artifactId", "1.0" );
461         indexer.indexPom( pom );
462         indexer.optimize();
463         indexer.close();
464
465         pom = getPom( "test", "test-artifactId", "1.0" );
466         indexer.indexPom( pom );
467         indexer.optimize();
468         indexer.close();
469     }
470
471     /**
472      * Test delete of pom document from index.
473      *
474      * @throws Exception
475      */
476     public void testDeletePomDocument()
477         throws Exception
478     {
479         createTestIndex();
480
481         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
482         RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE );
483
484         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
485
486         Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
487         indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
488
489         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
490         List artifactList = repoSearcher.search( qry, indexer );
491         assertEquals( 0, artifactList.size() );
492     }
493
494     private Model getPom( String groupId, String artifactId, String version )
495         throws Exception
496     {
497         Artifact artifact = getArtifact( groupId, artifactId, version );
498
499         return getPom( artifact );
500     }
501
502     private Model getPom( Artifact artifact )
503         throws Exception
504     {
505         File pomFile = getPomFile( artifact );
506
507         MavenXpp3Reader pomReader = new MavenXpp3Reader();
508         return pomReader.read( new FileReader( pomFile ) );
509     }
510
511     private File getPomFile( Artifact artifact )
512     {
513         String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
514         return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
515     }
516
517     private Artifact getArtifact( String groupId, String artifactId, String version )
518         throws Exception
519     {
520         if ( artifactFactory == null )
521         {
522             artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
523         }
524
525         return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
526     }
527
528     protected void tearDown()
529         throws Exception
530     {
531         repository = null;
532
533         super.tearDown();
534     }
535 }