]> source.dussan.org Git - archiva.git/blob
caac994b74b0a4f34d6a756c99bfecd247e793b6
[archiva.git] /
1 package org.apache.maven.archiva.indexing.lucene;
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.lucene.index.Term;
20 import org.apache.lucene.search.Query;
21 import org.apache.lucene.search.TermQuery;
22 import org.apache.maven.archiva.indexing.RepositoryArtifactIndex;
23 import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory;
24 import org.apache.maven.archiva.indexing.RepositoryIndexSearchException;
25 import org.apache.maven.archiva.indexing.record.MinimalIndexRecordFields;
26 import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
31 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32 import org.apache.maven.artifact.versioning.VersionRange;
33 import org.codehaus.plexus.PlexusTestCase;
34 import org.codehaus.plexus.util.FileUtils;
35
36 import java.io.File;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 /**
42  * Test the Lucene implementation of the artifact index search.
43  *
44  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45  * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough
46  */
47 public class LuceneMinimalArtifactIndexSearchTest
48     extends PlexusTestCase
49 {
50     private RepositoryArtifactIndex index;
51
52     private ArtifactRepository repository;
53
54     private ArtifactFactory artifactFactory;
55
56     private File indexLocation;
57
58     private RepositoryIndexRecordFactory recordFactory;
59
60     private Map records = new HashMap();
61
62     protected void setUp()
63         throws Exception
64     {
65         super.setUp();
66
67         recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
68
69         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
70
71         ArtifactRepositoryFactory repositoryFactory =
72             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
73
74         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
75
76         File file = getTestFile( "src/test/managed-repository" );
77         repository =
78             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
79
80         RepositoryArtifactIndexFactory factory =
81             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
82
83         indexLocation = getTestFile( "target/test-index" );
84
85         FileUtils.deleteDirectory( indexLocation );
86
87         index = factory.createMinimalIndex( indexLocation );
88
89         records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) );
90         records.put( "test-jar-jdk14",
91                      recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) );
92         records.put( "test-jar-and-pom",
93                      recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) );
94         records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord(
95             createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) );
96         records.put( "test-child-pom",
97                      recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) );
98         records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) );
99         records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) );
100         records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) );
101         records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) );
102         records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) );
103
104         index.indexRecords( records.values() );
105     }
106
107     public void testExactMatchMd5()
108         throws RepositoryIndexSearchException
109     {
110         Query query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) );
111         List results = index.search( new LuceneQuery( query ) );
112
113         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
114         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
115         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
116         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
117         assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
118         assertEquals( "Check results size", 5, results.size() );
119
120         // test non-match fails
121         query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "foo" ) );
122         results = index.search( new LuceneQuery( query ) );
123
124         assertTrue( "Check results size", results.isEmpty() );
125     }
126
127     public void testMatchFilename()
128         throws RepositoryIndexSearchException
129     {
130         Query query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "maven" ) );
131         List results = index.search( new LuceneQuery( query ) );
132
133         assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
134         assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
135         assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
136         assertEquals( "Check results size", 7, results.size() );
137
138 /* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path
139         query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "plugin" ) );
140         results = index.search( new LuceneQuery( query ) );
141
142         assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
143         assertEquals( "Check results size", 1, results.size() );
144 */
145         query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "test" ) );
146         results = index.search( new LuceneQuery( query ) );
147
148         assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
149         assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
150         assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
151         assertEquals( "Check results size", 7, results.size() );
152
153         // test non-match fails
154         query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "foo" ) );
155         results = index.search( new LuceneQuery( query ) );
156
157         assertTrue( "Check results size", results.isEmpty() );
158     }
159
160     public void testMatchClass()
161         throws RepositoryIndexSearchException
162     {
163         // TODO: should be preserving case!
164         Query query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "b.c.c" ) );
165         List results = index.search( new LuceneQuery( query ) );
166
167         assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
168         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
169         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
170         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
171         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
172         assertEquals( "Check results size", 5, results.size() );
173
174 /* TODO!: need to change the analyzer if we want partial classes (split on '.')
175         query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "C" ) );
176         results = index.search( new LuceneQuery( query ) );
177
178         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
179         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
180         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
181         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
182         assertEquals( "Check results size", 4, results.size() );
183
184         query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "MyMojo" ) );
185         results = index.search( new LuceneQuery( query ) );
186
187         assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
188         assertEquals( "Check results size", 1, results.size() );
189 */
190
191         // test non-match fails
192         query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "foo" ) );
193         results = index.search( new LuceneQuery( query ) );
194
195         assertTrue( "Check results size", results.isEmpty() );
196     }
197
198     private Artifact createArtifact( String artifactId )
199     {
200         return createArtifact( artifactId, "1.0", "jar", null );
201     }
202
203     private Artifact createArtifact( String artifactId, String version, String type )
204     {
205         return createArtifact( artifactId, version, type, null );
206     }
207
208     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
209     {
210         Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId,
211                                                                       VersionRange.createFromVersion( version ), type,
212                                                                       classifier, Artifact.SCOPE_RUNTIME );
213         artifact.isSnapshot();
214         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
215         artifact.setRepository( repository );
216         return artifact;
217     }
218 }