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.RepositoryMetadata;
\r
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
\r
28 import org.apache.maven.artifact.repository.metadata.Versioning;
\r
29 import org.apache.maven.artifact.repository.metadata.Metadata;
\r
30 import org.apache.maven.artifact.repository.metadata.Plugin;
\r
31 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
\r
32 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
\r
33 import org.apache.maven.repository.indexing.query.Query;
\r
34 import org.apache.maven.repository.indexing.query.RangeQuery;
\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.List;
\r
43 import java.util.Iterator;
\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 repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
141 // search last update
\r
142 org.apache.maven.repository.indexing.query.Query qry =
\r
143 new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
144 List metadataList = repoSearcher.search( qry );
\r
145 assertEquals( 1, metadataList.size() );
\r
146 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
148 RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
\r
150 Metadata metadata = repoMetadata.getMetadata();
\r
151 Versioning versioning = metadata.getVersioning();
\r
152 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
155 // search plugin prefix
\r
156 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
\r
157 metadataList = repoSearcher.search( qry );
\r
158 assertEquals( 1, metadataList.size() );
\r
159 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
161 RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
\r
162 Metadata metadata = repoMetadata.getMetadata();
\r
163 List plugins = metadata.getPlugins();
\r
164 for ( Iterator it = plugins.iterator(); it.hasNext(); )
\r
166 Plugin plugin = (Plugin) it.next();
\r
167 assertEquals( "org.apache.maven", plugin.getPrefix() );
\r
171 // search last update using INCLUSIVE Range Query
\r
172 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
173 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
\r
174 RangeQuery rQry = new RangeQuery( true );
\r
175 rQry.addQuery( qry1 );
\r
176 rQry.addQuery( qry2 );
\r
178 metadataList = repoSearcher.search( rQry );
\r
179 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
181 RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
\r
182 Metadata metadata = repoMetadata.getMetadata();
\r
183 Versioning versioning = metadata.getVersioning();
\r
184 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
187 // search last update using EXCLUSIVE Range Query
\r
188 qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
189 qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
190 rQry = new RangeQuery( false );
\r
191 rQry.addQuery( qry1 );
\r
192 rQry.addQuery( qry2 );
\r
194 metadataList = repoSearcher.search( rQry );
\r
195 assertEquals( metadataList.size(), 0 );
\r
201 * Test the exceptions thrown by MetadataRepositoryIndex.
\r
203 * @throws Exception
\r
205 public void testExceptions()
\r
208 //test when the object passed in the index(..) method is not a RepositoryMetadata instance
\r
209 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
210 indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
213 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
214 indexer.index( artifact );
\r
215 fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
\r
216 indexer.optimize();
\r
219 catch ( RepositoryIndexException e )
\r
221 assertTrue( true );
\r
226 indexer.isIndexed( new Object() );
\r
227 fail( "Must throw exception when the passed object is not of type metadata." );
\r
229 catch ( RepositoryIndexException e )
\r
231 assertTrue( true );
\r
236 * Test delete of document from metadata index.
\r
238 * @throws Exception
\r
240 public void testDeleteMetadataDocument()
\r
245 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
246 indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
248 RepositoryMetadata repoMetadata =
\r
249 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
250 indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
252 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
253 org.apache.maven.repository.indexing.query.Query qry =
\r
254 new SinglePhraseQuery( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
255 List metadataList = repoSearcher.search( qry );
\r
256 assertEquals( metadataList.size(), 0 );
\r
260 * Create RepositoryMetadata object.
\r
262 * @param groupId the groupId to be set
\r
263 * @param artifactId the artifactId to be set
\r
264 * @param version the version to be set
\r
265 * @param filename the name of the metadata file
\r
266 * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
\r
267 * @return RepositoryMetadata
\r
268 * @throws Exception
\r
270 private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
\r
271 String metadataType )
\r
274 RepositoryMetadata repoMetadata = null;
\r
276 InputStream is = null;
\r
277 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
\r
280 if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
\r
282 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
\r
283 is = url.openStream();
\r
284 repoMetadata = new GroupRepositoryMetadata( groupId );
\r
285 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
287 //artifact metadata
\r
288 else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
\r
291 repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();
\r
292 is = url.openStream();
\r
293 repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
294 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
296 //snapshot/version metadata
\r
297 else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
\r
299 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +
\r
300 "/" + filename ).toURL();
\r
301 is = url.openStream();
\r
302 repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
303 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
306 return repoMetadata;
\r
311 * Create artifact object.
\r
313 * @param groupId the groupId of the artifact
\r
314 * @param artifactId the artifactId of the artifact
\r
315 * @param version the version of the artifact
\r
317 * @throws Exception
\r
319 private Artifact getArtifact( String groupId, String artifactId, String version )
\r
322 if ( artifactFactory == null )
\r
324 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
\r
326 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
\r