1 package org.apache.maven.archiva.indexer.lucene;
4 * Copyright 2005-2006 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.codehaus.plexus.util.FileUtils;
39 import java.util.HashMap;
40 import java.util.List;
44 * Test the Lucene implementation of the artifact index search.
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
49 public class LuceneMinimalArtifactIndexSearchTest
50 extends PlexusTestCase
52 private RepositoryArtifactIndex index;
54 private ArtifactRepository repository;
56 private ArtifactFactory artifactFactory;
58 private File indexLocation;
60 private RepositoryIndexRecordFactory recordFactory;
62 private Map records = new HashMap();
64 protected void setUp()
69 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
71 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
73 ArtifactRepositoryFactory repositoryFactory =
74 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
76 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
78 File file = getTestFile( "src/test/managed-repository" );
80 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
82 RepositoryArtifactIndexFactory factory =
83 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
85 indexLocation = getTestFile( "target/test-index" );
87 FileUtils.deleteDirectory( indexLocation );
89 index = factory.createMinimalIndex( indexLocation );
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" ) ) );
106 index.indexRecords( records.values() );
109 public void testExactMatchMd5()
110 throws RepositoryIndexSearchException
112 Query query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" );
113 List results = index.search( new LuceneQuery( query ) );
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() );
122 // test non-match fails
123 query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "foo" );
124 results = index.search( new LuceneQuery( query ) );
126 assertTrue( "Check results size", results.isEmpty() );
129 public void testMatchFilename()
130 throws RepositoryIndexSearchException, ParseException
132 Query query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "maven" );
133 List results = index.search( new LuceneQuery( query ) );
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() );
140 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "plugin" );
141 results = index.search( new LuceneQuery( query ) );
143 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
144 assertEquals( "Check results size", 1, results.size() );
146 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "test" );
147 results = index.search( new LuceneQuery( query ) );
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() );
154 // test non-match fails
155 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "foo" );
156 results = index.search( new LuceneQuery( query ) );
158 assertTrue( "Check results size", results.isEmpty() );
161 public void testMatchClass()
162 throws RepositoryIndexSearchException, ParseException
164 Query query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "b.c.C" );
165 List results = index.search( new LuceneQuery( query ) );
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() );
174 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "C" );
175 results = index.search( new LuceneQuery( query ) );
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() );
184 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "MyMojo" );
185 results = index.search( new LuceneQuery( query ) );
187 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
188 assertEquals( "Check results size", 1, results.size() );
190 // test non-match fails
191 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "foo" );
192 results = index.search( new LuceneQuery( query ) );
194 assertTrue( "Check results size", results.isEmpty() );
197 private static Query createExactMatchQuery( String field, String value )
199 return new TermQuery( new Term( field, value ) );
202 private static Query createMatchQuery( String field, String value )
203 throws ParseException
205 return new QueryParser( field, LuceneRepositoryArtifactIndex.getAnalyzer() ).parse( value );
208 private Artifact createArtifact( String artifactId )
210 return createArtifact( artifactId, "1.0", "jar", null );
213 private Artifact createArtifact( String artifactId, String version, String type )
215 return createArtifact( artifactId, version, type, null );
218 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
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 );