]> source.dussan.org Git - archiva.git/blob
6361ab276d3bd55b36ff81931298c2ba0204de8d
[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.CompoundQuery;
27 import org.apache.maven.repository.indexing.query.Query;
28 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.util.FileUtils;
31
32 import java.io.File;
33 import java.util.Iterator;
34 import java.util.List;
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         RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
166
167         // search version
168         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
169         List artifacts = repoSearchLayer.searchAdvanced( qry );
170         assertEquals( 1, artifacts.size() );
171         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
172         {
173             SearchResult result = (SearchResult) iter.next();
174             Artifact artifact = result.getArtifact();
175             assertEquals( "1.0", artifact.getVersion() );
176         }
177
178         // search classes
179         qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" );
180         artifacts = repoSearchLayer.searchAdvanced( qry );
181         assertEquals( 1, artifacts.size() );
182         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
183         {
184             SearchResult result = (SearchResult) iter.next();
185             Artifact artifact = result.getArtifact();
186             assertEquals( "test-artifactId", artifact.getArtifactId() );
187         }
188
189         // search packages
190         qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" );
191         artifacts = repoSearchLayer.searchAdvanced( qry );
192         assertEquals( 1, artifacts.size() );
193         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
194         {
195             SearchResult result = (SearchResult) iter.next();
196             Artifact artifact = result.getArtifact();
197             assertEquals( "test-artifactId", artifact.getArtifactId() );
198         }
199
200         // search files
201         qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" );
202         artifacts = repoSearchLayer.searchAdvanced( qry );
203         assertEquals( 3, artifacts.size() );
204         Iterator iter = artifacts.iterator();
205         if ( iter.hasNext() )
206         {
207             SearchResult result = (SearchResult) iter.next();
208             Artifact artifact = result.getArtifact();
209             assertEquals( "test-artifactId", artifact.getArtifactId() );
210         }
211
212         // search group id
213         qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
214         artifacts = repoSearchLayer.searchAdvanced( qry );
215         assertEquals( 2, artifacts.size() );
216         iter = artifacts.iterator();
217         if ( iter.hasNext() )
218         {
219             SearchResult result = (SearchResult) iter.next();
220             Artifact artifact = result.getArtifact();
221             assertEquals( "org.apache.maven", artifact.getGroupId() );
222         }
223
224         // search artifact id
225         qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
226         artifacts = repoSearchLayer.searchAdvanced( qry );
227         assertEquals( 1, artifacts.size() );
228         for ( iter = artifacts.iterator(); iter.hasNext(); )
229         {
230             SearchResult result = (SearchResult) iter.next();
231             Artifact artifact = result.getArtifact();
232             assertEquals( "maven-artifact", artifact.getArtifactId() );
233         }
234
235         // search version
236         qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
237         artifacts = repoSearchLayer.searchAdvanced( qry );
238         assertEquals( 2, artifacts.size() );
239         for ( iter = artifacts.iterator(); iter.hasNext(); )
240         {
241             SearchResult result = (SearchResult) iter.next();
242             Artifact artifact = result.getArtifact();
243             assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
244         }
245
246         // search sha1 checksum
247         Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
248         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
249
250         String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
251
252         qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
253         artifacts = repoSearchLayer.searchAdvanced( qry );
254         assertEquals( 1, artifacts.size() );
255         for ( iter = artifacts.iterator(); iter.hasNext(); )
256         {
257             SearchResult result = (SearchResult) iter.next();
258             Artifact artifact2 = result.getArtifact();
259             String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 );
260             assertEquals( sha1, sha1Tmp );
261         }
262
263         // search md5 checksum
264         String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
265         qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
266         artifacts = repoSearchLayer.searchAdvanced( qry );
267         assertEquals( 1, artifacts.size() );
268         for ( iter = artifacts.iterator(); iter.hasNext(); )
269         {
270             SearchResult result = (SearchResult) iter.next();
271             Artifact artifact2 = result.getArtifact();
272             String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 );
273             assertEquals( md5, md5Tmp );
274         }
275
276         indexer.close();
277     }
278
279     /**
280      * Test the ArtifactRepositoryIndex using compound search (AND, OR).
281      *
282      * @throws Exception
283      */
284     public void testSearchCompound()
285         throws Exception
286     {
287         createTestIndex();
288
289         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
290         ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
291         //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
292         RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
293
294         // Criteria 1: required query
295         // ex. artifactId=maven-artifact AND groupId=org.apache.maven
296         Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
297         Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
298         CompoundQuery rQry = new CompoundQuery();
299         rQry.and( qry1 );
300         rQry.and( qry2 );
301
302         List artifacts = repoSearchLayer.searchAdvanced( rQry );
303         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
304         {
305             SearchResult result = (SearchResult) iter.next();
306             Artifact artifact = result.getArtifact();
307             assertEquals( "maven-artifact", artifact.getArtifactId() );
308             assertEquals( "org.apache.maven", artifact.getGroupId() );
309         }
310
311         // Criteria 2: nested required query
312         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
313         // version=2.0.3
314         Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
315         CompoundQuery oQry = new CompoundQuery();
316         oQry.or( rQry );
317         oQry.or( qry3 );
318
319         artifacts = repoSearchLayer.searchAdvanced( oQry );
320         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
321         {
322             SearchResult result = (SearchResult) iter.next();
323             Artifact artifact = result.getArtifact();
324             assertEquals( "maven-artifact", artifact.getArtifactId() );
325             assertEquals( "org.apache.maven", artifact.getGroupId() );
326         }
327
328         // Criteria 3: nested required query
329         // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
330         // (version=2.0.3 OR version=2.0.1)
331         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
332         Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
333         oQry = new CompoundQuery();
334         oQry.or( qry3 );
335         oQry.or( qry4 );
336
337         CompoundQuery oQry5 = new CompoundQuery();
338         Query qry9 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
339         Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact" );
340         oQry5.or( qry9 );
341         oQry5.or( qry10 );
342
343         CompoundQuery rQry2 = new CompoundQuery();
344         rQry2.or( oQry );
345         rQry2.and( rQry );
346         rQry2.or( oQry5 );
347
348         artifacts = repoSearchLayer.searchAdvanced( rQry2 );
349         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
350         {
351             SearchResult result = (SearchResult) iter.next();
352             Artifact artifact = result.getArtifact();
353             assertEquals( "maven-artifact", artifact.getArtifactId() );
354             assertEquals( "org.apache.maven", artifact.getGroupId() );
355             assertEquals( "2.0.1", artifact.getVersion() );
356         }
357
358         // Criteria 4: nested required query
359         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
360         // (version=2.0.3 OR version=2.0.1)
361         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
362         // OR [(artifactId=sample AND groupId=test)]
363         CompoundQuery rQry3 = new CompoundQuery();
364         Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
365         Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
366         rQry3.and( qry5 );
367         rQry3.and( qry6 );
368         CompoundQuery oQry2 = new CompoundQuery();
369         oQry2.and( rQry2 );
370         oQry2.and( rQry3 );
371
372         artifacts = repoSearchLayer.searchAdvanced( oQry2 );
373         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
374         {
375             SearchResult result = (SearchResult) iter.next();
376             Artifact artifact = result.getArtifact();
377             assertEquals( "maven-artifact", artifact.getArtifactId() );
378             assertEquals( "org.apache.maven", artifact.getGroupId() );
379             assertEquals( "2.0.1", artifact.getVersion() );
380         }
381
382         // Criteria 4: nested required query
383         // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
384         // (version=2.0.3 OR version=2.0.1)
385         // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
386         // [(artifactId=sample AND groupId=test)] OR
387         // [(artifactId=sample2 AND groupId=test)]
388         CompoundQuery rQry4 = new CompoundQuery();
389         Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
390         Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
391         rQry4.and( qry7 );
392         rQry4.and( qry8 );
393         oQry2.and( rQry4 );
394
395         artifacts = repoSearchLayer.searchAdvanced( oQry2 );
396         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
397         {
398             SearchResult result = (SearchResult) iter.next();
399             Artifact artifact = result.getArtifact();
400             assertEquals( "maven-artifact", artifact.getArtifactId() );
401             assertEquals( "org.apache.maven", artifact.getGroupId() );
402         }
403
404         indexer.close();
405     }
406
407     /**
408      * Test the exceptions thrown by DefaultRepositoryIndexSearcher
409      *
410      * @throws Exception
411      */
412     public void testSearchExceptions()
413         throws Exception
414     {
415         createTestIndex();
416
417         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
418         ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
419         // RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
420         RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
421
422         try
423         {
424             Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" );
425             List artifacts = repoSearchLayer.searchAdvanced( qry );
426             fail( "Must throw an exception on unparseable query." );
427         }
428         catch ( RepositoryIndexSearchException re )
429         {
430             assertTrue( true );
431         }
432
433         indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository );
434         repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
435
436         try
437         {
438             Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
439             List artifacts = repoSearchLayer.searchAdvanced( qry );
440             fail( "Must throw an exception on invalid index location." );
441         }
442         catch ( RepositoryIndexSearchException re )
443         {
444             assertTrue( true );
445         }
446
447     }
448
449     /**
450      * Test delete of document from the artifact index.
451      *
452      * @throws Exception
453      */
454     public void testDeleteArtifactDocument()
455         throws Exception
456     {
457         createTestIndex();
458
459         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
460         ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
461
462         Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
463         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
464         indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
465
466         RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
467         Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
468         List artifacts = repoSearcher.search( qry );
469         assertEquals( artifacts.size(), 0 );
470     }
471
472     /**
473      * Method for creating artifact object
474      *
475      * @param groupId    the groupId of the artifact to be created
476      * @param artifactId the artifactId of the artifact to be created
477      * @param version    the version of the artifact to be created
478      * @return Artifact object
479      * @throws Exception
480      */
481     private Artifact getArtifact( String groupId, String artifactId, String version )
482         throws Exception
483     {
484         if ( artifactFactory == null )
485         {
486             artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
487         }
488
489         return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
490     }
491
492     protected void tearDown()
493         throws Exception
494     {
495         repository = null;
496
497         super.tearDown();
498     }
499 }