]> source.dussan.org Git - archiva.git/blob
71f32c037197e0aa3b31828f6f4905fbc36018cc
[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 String 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 = "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             String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
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             String notIndexDir = new File( "" ).getAbsolutePath();
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.isIndexed( 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         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
126         //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
127         RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
128
129         // search version
130         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
131         List artifactList = repoSearchLayer.searchAdvanced( qry );
132         assertEquals( 1, artifactList.size() );
133         for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
134         {
135             SearchResult result = (SearchResult) iter.next();
136             Artifact artifact = result.getArtifact();
137             assertEquals( "1.0", artifact.getVersion() );
138         }
139
140         // search group id
141         qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
142         artifactList = repoSearchLayer.searchAdvanced( qry );
143         assertEquals( 2, artifactList.size() );
144         Iterator artifacts = artifactList.iterator();
145         if ( artifacts.hasNext() )
146         {
147             SearchResult result = (SearchResult) artifacts.next();
148             Artifact artifact = result.getArtifact();
149             assertEquals( "org.apache.maven", artifact.getGroupId() );
150         }
151
152         // search artifact id
153         qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
154         artifactList = repoSearchLayer.searchAdvanced( qry );
155         assertEquals( 1, artifactList.size() );
156         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
157         {
158             SearchResult result = (SearchResult) artifacts.next();
159             Artifact artifact = result.getArtifact();
160             assertEquals( "maven-artifact", artifact.getArtifactId() );
161         }
162
163         // search version
164         qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
165         artifactList = repoSearchLayer.searchAdvanced( qry );
166         assertEquals( 2, artifactList.size() );
167         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
168         {
169             SearchResult result = (SearchResult) artifacts.next();
170             Artifact artifact = result.getArtifact();
171             assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
172         }
173
174         // search packaging
175         qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" );
176         artifactList = repoSearchLayer.searchAdvanced( qry );
177         assertEquals( 3, artifactList.size() );
178         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
179         {
180             SearchResult result = (SearchResult) artifacts.next();
181             Map map = result.getFieldMatches();
182             Set mapEntry = map.entrySet();
183             for ( Iterator it = mapEntry.iterator(); it.hasNext(); )
184             {
185                 Map.Entry entry = (Map.Entry) it.next();
186             }
187             assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) );
188         }
189
190         //search license url
191         qry =
192             new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" );
193         artifactList = repoSearchLayer.searchAdvanced( qry );
194         assertEquals( 2, artifactList.size() );
195         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
196         {
197             SearchResult result = (SearchResult) artifacts.next();
198             Map map = result.getFieldMatches();
199             List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS );
200             for ( Iterator it = matches.iterator(); it.hasNext(); )
201             {
202                 assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() );
203             }
204         }
205
206         //search dependencies
207         qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
208         artifactList = repoSearchLayer.searchAdvanced( qry );
209         assertEquals( 2, artifactList.size() );
210         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
211         {
212             SearchResult result = (SearchResult) artifacts.next();
213             Map map = result.getFieldMatches();
214             boolean depFound = false;
215             Iterator dependencies = ( (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ) ).iterator();
216             while ( dependencies.hasNext() )
217             {
218                 String dep = (String) dependencies.next();
219                 if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) )
220                 {
221                     depFound = true;
222                     break;
223                 }
224             }
225             assertTrue( "Searched dependency not found.", depFound );
226         }
227
228         //search build plugin
229         qry =
230             new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" );
231         artifactList = repoSearchLayer.searchAdvanced( qry );
232         assertEquals( 1, artifactList.size() );
233         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
234         {
235             SearchResult result = (SearchResult) artifacts.next();
236             Map map = result.getFieldMatches();
237             Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ) ).iterator();
238             boolean found = false;
239             while ( plugins.hasNext() )
240             {
241                 String plugin = (String) plugins.next();
242                 if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) )
243                 {
244                     found = true;
245                     break;
246                 }
247             }
248             assertTrue( "Searched plugin not found.", found );
249         }
250
251         //search reporting plugin
252         qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT,
253                                      "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
254         artifactList = repoSearchLayer.searchAdvanced( qry );
255         assertEquals( 1, artifactList.size() );
256         for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
257         {
258             SearchResult result = (SearchResult) artifacts.next();
259             Map map = result.getFieldMatches();
260             Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ) ).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 );
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 );
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         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
318         //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
319         RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
320
321         // Criteria 1: required query
322         // ex. artifactId=maven-artifact AND groupId=org.apache.maven
323         Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
324         Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
325         CompoundQuery rQry = new CompoundQuery();
326         rQry.and( qry1 );
327         rQry.and( qry2 );
328
329         List artifacts = repoSearchLayer.searchAdvanced( rQry );
330         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
331         {
332             SearchResult result = (SearchResult) iter.next();
333             Artifact artifact = result.getArtifact();
334             assertEquals( "maven-artifact", artifact.getArtifactId() );
335             assertEquals( "org.apache.maven", artifact.getGroupId() );
336         }
337
338         // Criteria 2: nested required query
339         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
340         // version=2.0.3
341         Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
342         CompoundQuery oQry = new CompoundQuery();
343         oQry.and( rQry );
344         oQry.or( qry3 );
345
346         artifacts = repoSearchLayer.searchAdvanced( oQry );
347         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
348         {
349             SearchResult result = (SearchResult) iter.next();
350             Artifact artifact = result.getArtifact();
351             assertEquals( "maven-artifact", artifact.getArtifactId() );
352             assertEquals( "org.apache.maven", artifact.getGroupId() );
353         }
354
355         // Criteria 3: nested required query
356         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
357         // (version=2.0.3 OR version=2.0.1)
358         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
359         Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
360         oQry = new CompoundQuery();
361         oQry.or( qry3 );
362         oQry.or( qry4 );
363
364         CompoundQuery oQry5 = new CompoundQuery();
365         Query qry9 =
366             new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
367         Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES,
368                                              "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
369         oQry5.or( qry9 );
370         oQry5.or( qry10 );
371
372         CompoundQuery rQry2 = new CompoundQuery();
373         rQry2.or( oQry );
374         rQry2.and( rQry );
375         rQry2.and( oQry5 );
376
377         artifacts = repoSearchLayer.searchAdvanced( rQry2 );
378         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
379         {
380             SearchResult result = (SearchResult) iter.next();
381             Artifact artifact = result.getArtifact();
382             assertEquals( "maven-artifact", artifact.getArtifactId() );
383             assertEquals( "org.apache.maven", artifact.getGroupId() );
384             assertEquals( "2.0.1", artifact.getVersion() );
385         }
386
387         // Criteria 4: nested required query
388         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
389         // (version=2.0.3 OR version=2.0.1)
390         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
391         // OR [(artifactId=sample AND groupId=test)]
392         CompoundQuery rQry3 = new CompoundQuery();
393         Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
394         Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
395         rQry3.and( qry5 );
396         rQry3.and( qry6 );
397         CompoundQuery oQry2 = new CompoundQuery();
398         oQry2.and( rQry2 );
399         oQry2.and( rQry3 );
400
401         artifacts = repoSearchLayer.searchAdvanced( oQry2 );
402         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
403         {
404             SearchResult result = (SearchResult) iter.next();
405             Artifact artifact = result.getArtifact();
406             assertEquals( "maven-artifact", artifact.getArtifactId() );
407             assertEquals( "org.apache.maven", artifact.getGroupId() );
408             assertEquals( "2.0.1", artifact.getVersion() );
409         }
410
411         // Criteria 4: nested required query
412         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
413         // (version=2.0.3 OR version=2.0.1)
414         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
415         // [(artifactId=sample AND groupId=test)] OR
416         // [(artifactId=sample2 AND groupId=test)]
417         CompoundQuery rQry4 = new CompoundQuery();
418         Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
419         Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
420         rQry4.and( qry7 );
421         rQry4.and( qry8 );
422         oQry2.and( rQry4 );
423
424         artifacts = repoSearchLayer.searchAdvanced( oQry2 );
425         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
426         {
427             SearchResult result = (SearchResult) iter.next();
428             Artifact artifact = result.getArtifact();
429             assertEquals( "maven-artifact", artifact.getArtifactId() );
430             assertEquals( "org.apache.maven", artifact.getGroupId() );
431         }
432
433         indexer.close();
434     }
435
436     /**
437      * Create an index that will be used for testing.
438      * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
439      * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
440      *
441      * @throws Exception
442      */
443     private void createTestIndex()
444         throws Exception
445     {
446         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
447         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
448
449         Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
450         indexer.indexPom( pom );
451         indexer.optimize();
452         indexer.close();
453
454         pom = getPom( "org.apache.maven", "maven-model", "2.0" );
455         indexer.indexPom( pom );
456         indexer.optimize();
457         indexer.close();
458
459         pom = getPom( "test", "test-artifactId", "1.0" );
460         indexer.indexPom( pom );
461         indexer.optimize();
462         indexer.close();
463
464         pom = getPom( "test", "test-artifactId", "1.0" );
465         indexer.indexPom( pom );
466         indexer.optimize();
467         indexer.close();
468     }
469
470     /**
471      * Test delete of pom document from index.
472      *
473      * @throws Exception
474      */
475     public void testDeletePomDocument()
476         throws Exception
477     {
478         createTestIndex();
479
480         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
481         PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
482         Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
483         indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
484
485         RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
486         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
487         List artifactList = repoSearcher.search( qry );
488         assertEquals( artifactList.size(), 0 );
489     }
490
491     private Model getPom( String groupId, String artifactId, String version )
492         throws Exception
493     {
494         Artifact artifact = getArtifact( groupId, artifactId, version );
495
496         return getPom( artifact );
497     }
498
499     private Model getPom( Artifact artifact )
500         throws Exception
501     {
502         File pomFile = getPomFile( artifact );
503
504         MavenXpp3Reader pomReader = new MavenXpp3Reader();
505         return pomReader.read( new FileReader( pomFile ) );
506     }
507
508     private File getPomFile( Artifact artifact )
509     {
510         String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
511         return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
512     }
513
514     private Artifact getArtifact( String groupId, String artifactId, String version )
515         throws Exception
516     {
517         if ( artifactFactory == null )
518         {
519             artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
520         }
521
522         return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
523     }
524
525     protected void tearDown()
526         throws Exception
527     {
528         repository = null;
529
530         super.tearDown();
531     }
532 }