1 package org.apache.maven.archiva.indexer.lucene;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
42 import java.util.HashMap;
43 import java.util.List;
47 * Test the Lucene implementation of the artifact index search.
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
52 public class LuceneMinimalArtifactIndexSearchTest
53 extends PlexusTestCase
55 private RepositoryArtifactIndex index;
57 private ArtifactRepository repository;
59 private ArtifactFactory artifactFactory;
61 private File indexLocation;
63 private RepositoryIndexRecordFactory recordFactory;
65 private Map records = new HashMap();
67 protected void setUp()
72 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
74 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
76 ArtifactRepositoryFactory repositoryFactory =
77 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
79 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
81 File file = getTestFile( "src/test/managed-repository" );
83 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
85 RepositoryArtifactIndexFactory factory =
86 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
88 indexLocation = getTestFile( "target/test-index" );
90 FileUtils.deleteDirectory( indexLocation );
92 index = factory.createMinimalIndex( indexLocation );
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" ) ) );
109 index.indexRecords( records.values() );
112 public void testExactMatchMd5()
113 throws RepositoryIndexSearchException
115 Query query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" );
116 List results = index.search( new LuceneQuery( query ) );
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() );
125 // test non-match fails
126 query = createExactMatchQuery( MinimalIndexRecordFields.MD5, "foo" );
127 results = index.search( new LuceneQuery( query ) );
129 assertTrue( "Check results size", results.isEmpty() );
132 public void testMatchFilename()
133 throws RepositoryIndexSearchException, ParseException
135 Query query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "maven" );
136 List results = index.search( new LuceneQuery( query ) );
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() );
143 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "plugin" );
144 results = index.search( new LuceneQuery( query ) );
146 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
147 assertEquals( "Check results size", 1, results.size() );
149 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "test" );
150 results = index.search( new LuceneQuery( query ) );
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() );
157 // test non-match fails
158 query = createMatchQuery( MinimalIndexRecordFields.FILENAME, "foo" );
159 results = index.search( new LuceneQuery( query ) );
161 assertTrue( "Check results size", results.isEmpty() );
164 public void testMatchClass()
165 throws RepositoryIndexSearchException, ParseException
167 Query query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "b.c.C" );
168 List results = index.search( new LuceneQuery( query ) );
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() );
177 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "C" );
178 results = index.search( new LuceneQuery( query ) );
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() );
187 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "MyMojo" );
188 results = index.search( new LuceneQuery( query ) );
190 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
191 assertEquals( "Check results size", 1, results.size() );
193 // test non-match fails
194 query = createMatchQuery( MinimalIndexRecordFields.CLASSES, "foo" );
195 results = index.search( new LuceneQuery( query ) );
197 assertTrue( "Check results size", results.isEmpty() );
200 private static Query createExactMatchQuery( String field, String value )
202 return new TermQuery( new Term( field, value ) );
205 private static Query createMatchQuery( String field, String value )
206 throws ParseException
208 return new QueryParser( field, LuceneRepositoryArtifactIndex.getAnalyzer() ).parse( value );
211 private Artifact createArtifact( String artifactId )
213 return createArtifact( artifactId, "1.0", "jar", null );
216 private Artifact createArtifact( String artifactId, String version, String type )
218 return createArtifact( artifactId, version, type, null );
221 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
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 );