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