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