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