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
38 import java.io.File;
\r
39 import java.io.InputStream;
\r
40 import java.io.InputStreamReader;
\r
41 import java.net.URL;
\r
42 import java.util.Iterator;
\r
43 import java.util.List;
\r
46 * This class tests the MetadataRepositoryIndex.
\r
48 public class MetadataRepositoryIndexingTest
\r
49 extends PlexusTestCase
\r
51 private ArtifactRepository repository;
\r
53 private String indexPath;
\r
55 private MetadataRepositoryIndex indexer;
\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 = "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 indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
103 RepositoryMetadata repoMetadata =
\r
104 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
105 indexer.index( repoMetadata );
\r
106 indexer.optimize();
\r
109 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
110 MetadataRepositoryIndex.ARTIFACT_METADATA );
\r
111 indexer.index( repoMetadata );
\r
112 indexer.optimize();
\r
115 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
116 MetadataRepositoryIndex.SNAPSHOT_METADATA );
\r
117 indexer.index( repoMetadata );
\r
118 indexer.optimize();
\r
121 repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
122 indexer.index( repoMetadata );
\r
123 indexer.optimize();
\r
128 * Test the ArtifactRepositoryIndex using a single-phrase search.
\r
130 * @throws Exception
\r
132 public void testSearch()
\r
137 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
138 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
139 //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
140 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
\r
142 // search last update
\r
143 org.apache.maven.repository.indexing.query.Query qry =
\r
144 new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
145 List metadataList = repoSearchLayer.searchAdvanced( qry );
\r
146 //assertEquals( 1, metadataList.size() );
\r
147 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
149 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
150 if ( hit.isMetadata() )
\r
152 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
153 Metadata metadata = repoMetadata.getMetadata();
\r
154 Versioning versioning = metadata.getVersioning();
\r
155 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
159 // search plugin prefix
\r
160 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
\r
161 metadataList = repoSearchLayer.searchAdvanced( qry );
\r
162 //assertEquals( 1, metadataList.size() );
\r
163 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
165 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
166 if ( hit.isMetadata() )
\r
168 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
169 Metadata metadata = repoMetadata.getMetadata();
\r
170 List plugins = metadata.getPlugins();
\r
171 for ( Iterator it = plugins.iterator(); it.hasNext(); )
\r
173 Plugin plugin = (Plugin) it.next();
\r
174 assertEquals( "org.apache.maven", plugin.getPrefix() );
\r
179 // search last update using INCLUSIVE Range Query
\r
180 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
181 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
\r
182 RangeQuery rQry = new RangeQuery( true );
\r
183 rQry.addQuery( qry1 );
\r
184 rQry.addQuery( qry2 );
\r
186 metadataList = repoSearchLayer.searchAdvanced( rQry );
\r
187 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
189 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
190 if ( hit.isMetadata() )
\r
192 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
193 Metadata metadata = repoMetadata.getMetadata();
\r
194 Versioning versioning = metadata.getVersioning();
\r
195 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
199 // search last update using EXCLUSIVE Range Query
\r
200 qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
201 qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
202 rQry = new RangeQuery( false );
\r
203 rQry.addQuery( qry1 );
\r
204 rQry.addQuery( qry2 );
\r
206 metadataList = repoSearchLayer.searchAdvanced( rQry );
\r
207 assertEquals( metadataList.size(), 0 );
\r
213 * Test the exceptions thrown by MetadataRepositoryIndex.
\r
215 * @throws Exception
\r
217 public void testExceptions()
\r
220 //test when the object passed in the index(..) method is not a RepositoryMetadata instance
\r
221 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
222 indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
225 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
226 indexer.index( artifact );
\r
227 fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
\r
228 indexer.optimize();
\r
231 catch ( RepositoryIndexException e )
\r
233 assertTrue( true );
\r
238 indexer.isIndexed( new Object() );
\r
239 fail( "Must throw exception when the passed object is not of type metadata." );
\r
241 catch ( RepositoryIndexException e )
\r
243 assertTrue( true );
\r
248 * Test delete of document from metadata index.
\r
250 * @throws Exception
\r
252 public void testDeleteMetadataDocument()
\r
257 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
258 indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
260 RepositoryMetadata repoMetadata =
\r
261 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
262 indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
264 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
265 org.apache.maven.repository.indexing.query.Query qry =
\r
266 new SinglePhraseQuery( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
267 List metadataList = repoSearcher.search( qry );
\r
268 assertEquals( metadataList.size(), 0 );
\r
272 * Create RepositoryMetadata object.
\r
274 * @param groupId the groupId to be set
\r
275 * @param artifactId the artifactId to be set
\r
276 * @param version the version to be set
\r
277 * @param filename the name of the metadata file
\r
278 * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
\r
279 * @return RepositoryMetadata
\r
280 * @throws Exception
\r
282 private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
\r
283 String metadataType )
\r
286 RepositoryMetadata repoMetadata = null;
\r
288 InputStream is = null;
\r
289 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
\r
292 if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
\r
294 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
\r
295 is = url.openStream();
\r
296 repoMetadata = new GroupRepositoryMetadata( groupId );
\r
297 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
299 //artifact metadata
\r
300 else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
\r
303 repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();
\r
304 is = url.openStream();
\r
305 repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
306 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
308 //snapshot/version metadata
\r
309 else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
\r
311 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +
\r
312 "/" + filename ).toURL();
\r
313 is = url.openStream();
\r
314 repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
315 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
318 return repoMetadata;
\r
323 * Create artifact object.
\r
325 * @param groupId the groupId of the artifact
\r
326 * @param artifactId the artifactId of the artifact
\r
327 * @param version the version of the artifact
\r
329 * @throws Exception
\r
331 private Artifact getArtifact( String groupId, String artifactId, String version )
\r
334 if ( artifactFactory == null )
\r
336 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
\r
338 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
\r