1 package org.apache.maven.repository.indexing;
\r
4 * Copyright 2005-2006 The Apache Software Foundation.
\r
6 * Licensed under the Apache License, Version 2.0 (the "License");
\r
7 * you may not use this file except in compliance with the License.
\r
8 * You may obtain a copy of the License at
\r
10 * http://www.apache.org/licenses/LICENSE-2.0
\r
12 * Unless required by applicable law or agreed to in writing, software
\r
13 * distributed under the License is distributed on an "AS IS" BASIS,
\r
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 * See the License for the specific language governing permissions and
\r
16 * limitations under the License.
\r
19 import org.apache.maven.artifact.Artifact;
\r
20 import org.apache.maven.artifact.factory.ArtifactFactory;
\r
21 import org.apache.maven.artifact.repository.ArtifactRepository;
\r
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
\r
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
\r
24 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
\r
25 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
\r
26 import org.apache.maven.artifact.repository.metadata.Metadata;
\r
27 import org.apache.maven.artifact.repository.metadata.Plugin;
\r
28 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
\r
29 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
\r
30 import org.apache.maven.artifact.repository.metadata.Versioning;
\r
31 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
\r
32 import org.apache.maven.repository.indexing.query.Query;
\r
33 import org.apache.maven.repository.indexing.query.RangeQuery;
\r
34 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
\r
35 import org.codehaus.plexus.PlexusTestCase;
\r
36 import org.codehaus.plexus.util.FileUtils;
\r
37 import org.codehaus.plexus.util.IOUtil;
\r
38 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
\r
40 import java.io.File;
\r
41 import java.io.FileNotFoundException;
\r
42 import java.io.FileReader;
\r
43 import java.io.IOException;
\r
44 import java.util.Iterator;
\r
45 import java.util.List;
\r
48 * This class tests the MetadataRepositoryIndex.
\r
50 public class MetadataRepositoryIndexingTest
\r
51 extends PlexusTestCase
\r
53 private ArtifactRepository repository;
\r
55 private File indexPath;
\r
57 private ArtifactFactory artifactFactory;
\r
68 File repositoryDirectory = getTestFile( "src/test/repository" );
\r
69 String repoDir = repositoryDirectory.toURL().toString();
\r
70 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
\r
71 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
\r
72 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
\r
74 indexPath = getTestFile( "target/index" );
\r
75 FileUtils.deleteDirectory( indexPath );
\r
83 public void tearDown()
\r
91 * Create the test index.
\r
92 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
\r
93 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
\r
97 private void createTestIndex()
\r
100 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
101 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
103 RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven" );
\r
104 repoMetadata.setMetadata( readMetadata( repoMetadata ) );
\r
105 indexer.index( repoMetadata );
\r
106 indexer.optimize();
\r
109 repoMetadata = new ArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) );
\r
110 repoMetadata.setMetadata( readMetadata( repoMetadata ) );
\r
111 indexer.index( repoMetadata );
\r
112 indexer.optimize();
\r
116 new SnapshotArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ) );
\r
117 repoMetadata.setMetadata( readMetadata( repoMetadata ) );
\r
118 indexer.index( repoMetadata );
\r
119 indexer.optimize();
\r
122 repoMetadata = new GroupRepositoryMetadata( "test" );
\r
123 repoMetadata.setMetadata( readMetadata( repoMetadata ) );
\r
124 indexer.index( repoMetadata );
\r
125 indexer.optimize();
\r
130 * Test the ArtifactRepositoryIndex using a single-phrase search.
\r
132 * @throws Exception
\r
134 public void testSearch()
\r
139 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
140 RepositoryIndexSearchLayer repoSearchLayer =
\r
141 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
\r
143 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
145 // search last update
\r
146 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
147 List metadataList = repoSearchLayer.searchAdvanced( qry, indexer );
\r
148 //assertEquals( 1, metadataList.size() );
\r
149 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
151 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
152 if ( hit.isMetadata() )
\r
154 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
155 Metadata metadata = repoMetadata.getMetadata();
\r
156 Versioning versioning = metadata.getVersioning();
\r
157 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
161 // search plugin prefix
\r
162 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
\r
163 metadataList = repoSearchLayer.searchAdvanced( qry, indexer );
\r
164 //assertEquals( 1, metadataList.size() );
\r
165 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
167 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
168 if ( hit.isMetadata() )
\r
170 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
171 Metadata metadata = repoMetadata.getMetadata();
\r
172 List plugins = metadata.getPlugins();
\r
173 for ( Iterator it = plugins.iterator(); it.hasNext(); )
\r
175 Plugin plugin = (Plugin) it.next();
\r
176 assertEquals( "org.apache.maven", plugin.getPrefix() );
\r
181 // search last update using INCLUSIVE Range Query
\r
182 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
183 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
\r
184 RangeQuery rQry = new RangeQuery( true );
\r
185 rQry.addQuery( qry1 );
\r
186 rQry.addQuery( qry2 );
\r
188 metadataList = repoSearchLayer.searchAdvanced( rQry, indexer );
\r
189 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
191 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
192 if ( hit.isMetadata() )
\r
194 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
195 Metadata metadata = repoMetadata.getMetadata();
\r
196 Versioning versioning = metadata.getVersioning();
\r
197 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
201 // search last update using EXCLUSIVE Range Query
\r
202 qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
203 qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
204 rQry = new RangeQuery( false );
\r
205 rQry.addQuery( qry1 );
\r
206 rQry.addQuery( qry2 );
\r
208 metadataList = repoSearchLayer.searchAdvanced( rQry, indexer );
\r
209 assertEquals( 0, metadataList.size() );
\r
215 * Test the exceptions thrown by MetadataRepositoryIndex.
\r
217 * @throws Exception
\r
219 public void testExceptions()
\r
222 //test when the object passed in the index(..) method is not a RepositoryMetadata instance
\r
223 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
224 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
227 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
228 indexer.index( artifact );
\r
229 fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
\r
230 indexer.optimize();
\r
233 catch ( RepositoryIndexException e )
\r
235 assertTrue( true );
\r
240 indexer.deleteIfIndexed( new Object() );
\r
241 fail( "Must throw exception when the passed object is not of type metadata." );
\r
243 catch ( RepositoryIndexException e )
\r
245 assertTrue( true );
\r
250 * Test delete of document from metadata index.
\r
252 * @throws Exception
\r
254 public void testDeleteMetadataDocument()
\r
259 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
260 RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE );
\r
262 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
264 RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven" );
\r
265 repoMetadata.setMetadata( readMetadata( repoMetadata ) );
\r
266 indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
268 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
269 List metadataList = repoSearcher.search( qry, indexer );
\r
270 assertEquals( 0, metadataList.size() );
\r
275 * Create artifact object.
\r
277 * @param groupId the groupId of the artifact
\r
278 * @param artifactId the artifactId of the artifact
\r
279 * @param version the version of the artifact
\r
281 * @throws Exception
\r
283 private Artifact getArtifact( String groupId, String artifactId, String version )
\r
286 if ( artifactFactory == null )
\r
288 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
\r
290 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
\r
294 * Create RepositoryMetadata object.
\r
296 * @return RepositoryMetadata
\r
298 private Metadata readMetadata( RepositoryMetadata repoMetadata )
\r
299 throws RepositoryIndexSearchException
\r
301 File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repoMetadata ) );
\r
303 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
\r
305 FileReader reader = null;
\r
308 reader = new FileReader( file );
\r
309 return metadataReader.read( reader );
\r
311 catch ( FileNotFoundException e )
\r
313 throw new RepositoryIndexSearchException( "Unable to find metadata file: " + e.getMessage(), e );
\r
315 catch ( IOException e )
\r
317 throw new RepositoryIndexSearchException( "Unable to read metadata file: " + e.getMessage(), e );
\r
319 catch ( XmlPullParserException xe )
\r
321 throw new RepositoryIndexSearchException( "Unable to parse metadata file: " + xe.getMessage(), xe );
\r
325 IOUtil.close( reader );
\r