]> source.dussan.org Git - archiva.git/blob
48c959f936eff99e48a5b9bec384a7fde0d1ab2e
[archiva.git] /
1 package org.apache.maven.archiva.indexer.lucene;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.queryParser.ParseException;
25 import org.apache.lucene.queryParser.QueryParser;
26 import org.apache.lucene.search.Query;
27 import org.apache.lucene.search.TermQuery;
28 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
29 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
30 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
31 import org.apache.maven.archiva.indexer.record.MinimalIndexRecordFields;
32 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.artifact.factory.ArtifactFactory;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
37 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
38 import org.apache.maven.artifact.versioning.VersionRange;
39 import org.codehaus.plexus.PlexusTestCase;
40
41 import java.io.File;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 /**
47  * Test the Lucene implementation of the artifact index search.
48  *
49  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50  * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough
51  */
52 public class LuceneMinimalArtifactIndexSearchTest
53     extends PlexusTestCase
54 {
55     private RepositoryArtifactIndex index;
56
57     private ArtifactRepository repository;
58
59     private ArtifactFactory artifactFactory;
60
61     private File indexLocation;
62
63     private RepositoryIndexRecordFactory recordFactory;
64
65     private Map records = new HashMap();
66
67     protected void setUp()
68         throws Exception
69     {
70         super.setUp();
71
72         recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
73
74         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
75
76         ArtifactRepositoryFactory repositoryFactory =
77             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
78
79         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
80
81         File file = getTestFile( "src/test/managed-repository" );
82         repository =
83             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
84
85         RepositoryArtifactIndexFactory factory =
86             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
87
88         indexLocation = getTestFile( "target/test-index" );
89
90         FileUtils.deleteDirectory( indexLocation );
91
92         index = factory.createMinimalIndex( indexLocation );
93
94         records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) );
95         records.put( "test-jar-jdk14",
96                      recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) );
97         records.put( "test-jar-and-pom",
98                      recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) );
99         records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord(
100             createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) );
101         records.put( "test-child-pom",
102                      recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) );
103         records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) );
104         records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) );
105         records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) );
106         records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) );
107         records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) );
108
109         index.indexRecords( records.values() );
110     }
111
112     public void testExactMatchMd5()
113         throws RepositoryIndexSearchException
114     {
115         Query query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" );
116         List results = index.search( new LuceneQuery( query ) );
117
118         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
119         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
120         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
121         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
122         assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
123         assertEquals( "Check results size", 5, results.size() );
124
125         // test non-match fails
126         query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "foo" );
127         results = index.search( new LuceneQuery( query ) );
128
129         assertTrue( "Check results size", results.isEmpty() );
130     }
131
132     public void testMatchFilename()
133         throws RepositoryIndexSearchException, ParseException
134     {
135         Query query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "maven" );
136         List results = index.search( new LuceneQuery( query ) );
137
138         assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
139         assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
140         assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
141         assertEquals( "Check results size", 7, results.size() );
142
143         query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "plugin" );
144         results = index.search( new LuceneQuery( query ) );
145
146         assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
147         assertEquals( "Check results size", 1, results.size() );
148
149         query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "test" );
150         results = index.search( new LuceneQuery( query ) );
151
152         assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) );
153         assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) );
154         assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) );
155         assertEquals( "Check results size", 7, results.size() );
156
157         // test non-match fails
158         query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "foo" );
159         results = index.search( new LuceneQuery( query ) );
160
161         assertTrue( "Check results size", results.isEmpty() );
162     }
163
164     public void testMatchClass()
165         throws RepositoryIndexSearchException, ParseException
166     {
167         Query query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "b.c.C" );
168         List results = index.search( new LuceneQuery( query ) );
169
170         assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
171         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
172         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
173         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
174         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
175         assertEquals( "Check results size", 5, results.size() );
176
177         query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "C" );
178         results = index.search( new LuceneQuery( query ) );
179
180         assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) );
181         assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) );
182         assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) );
183         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) );
184         assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) );
185         assertEquals( "Check results size", 5, results.size() );
186
187         query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "MyMojo" );
188         results = index.search( new LuceneQuery( query ) );
189
190         assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
191         assertEquals( "Check results size", 1, results.size() );
192
193         // test non-match fails
194         query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "foo" );
195         results = index.search( new LuceneQuery( query ) );
196
197         assertTrue( "Check results size", results.isEmpty() );
198     }
199
200     private static Query createExactMatchQuery( String field, String value )
201     {
202         return new TermQuery( new Term( field, value ) );
203     }
204
205     private static Query createMatchQuery( String field, String value )
206         throws ParseException
207     {
208         return new QueryParser( field, LuceneRepositoryArtifactIndex.getAnalyzer() ).parse( value );
209     }
210
211     private Artifact createArtifact( String artifactId )
212     {
213         return createArtifact( artifactId, "1.0", "jar", null );
214     }
215
216     private Artifact createArtifact( String artifactId, String version, String type )
217     {
218         return createArtifact( artifactId, version, type, null );
219     }
220
221     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
222     {
223         Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId,
224                                                                       VersionRange.createFromVersion( version ), type,
225                                                                       classifier, Artifact.SCOPE_RUNTIME );
226         artifact.isSnapshot();
227         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
228         artifact.setRepository( repository );
229         return artifact;
230     }
231 }