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