1 package org.apache.maven.archiva.indexing.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.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;
37 import java.util.HashMap;
38 import java.util.List;
42 * Test the Lucene implementation of the artifact index search.
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
47 public class LuceneMinimalArtifactIndexSearchTest
48 extends PlexusTestCase
50 private RepositoryArtifactIndex index;
52 private ArtifactRepository repository;
54 private ArtifactFactory artifactFactory;
56 private File indexLocation;
58 private RepositoryIndexRecordFactory recordFactory;
60 private Map records = new HashMap();
62 protected void setUp()
67 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
69 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
71 ArtifactRepositoryFactory repositoryFactory =
72 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
74 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
76 File file = getTestFile( "src/test/managed-repository" );
78 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
80 RepositoryArtifactIndexFactory factory =
81 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
83 indexLocation = getTestFile( "target/test-index" );
85 FileUtils.deleteDirectory( indexLocation );
87 index = factory.createMinimalIndex( indexLocation );
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" ) ) );
104 index.indexRecords( records.values() );
107 public void testExactMatchMd5()
108 throws RepositoryIndexSearchException
110 Query query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) );
111 List results = index.search( new LuceneQuery( query ) );
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() );
120 // test non-match fails
121 query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "foo" ) );
122 results = index.search( new LuceneQuery( query ) );
124 assertTrue( "Check results size", results.isEmpty() );
127 public void testMatchFilename()
128 throws RepositoryIndexSearchException
130 Query query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "maven" ) );
131 List results = index.search( new LuceneQuery( query ) );
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() );
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 ) );
142 assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
143 assertEquals( "Check results size", 1, results.size() );
145 query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "test" ) );
146 results = index.search( new LuceneQuery( query ) );
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() );
153 // test non-match fails
154 query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "foo" ) );
155 results = index.search( new LuceneQuery( query ) );
157 assertTrue( "Check results size", results.isEmpty() );
160 public void testMatchClass()
161 throws RepositoryIndexSearchException
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 ) );
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 /* 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 ) );
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() );
184 query = new TermQuery( new Term( 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() );
191 // test non-match fails
192 query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "foo" ) );
193 results = index.search( new LuceneQuery( query ) );
195 assertTrue( "Check results size", results.isEmpty() );
198 private Artifact createArtifact( String artifactId )
200 return createArtifact( artifactId, "1.0", "jar", null );
203 private Artifact createArtifact( String artifactId, String version, String type )
205 return createArtifact( artifactId, version, type, null );
208 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
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 );