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 ArtifactFactory artifactFactory;
\r
66 File repositoryDirectory = getTestFile( "src/test/repository" );
\r
67 String repoDir = repositoryDirectory.toURL().toString();
\r
68 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
\r
69 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
\r
70 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
\r
72 indexPath = "target/index";
\r
73 FileUtils.deleteDirectory( indexPath );
\r
81 public void tearDown()
\r
89 * Create the test index.
\r
90 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
\r
91 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
\r
95 private void createTestIndex()
\r
98 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
99 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
101 RepositoryMetadata repoMetadata =
\r
102 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
103 indexer.index( repoMetadata );
\r
104 indexer.optimize();
\r
107 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
108 MetadataRepositoryIndex.ARTIFACT_METADATA );
\r
109 indexer.index( repoMetadata );
\r
110 indexer.optimize();
\r
113 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
114 MetadataRepositoryIndex.SNAPSHOT_METADATA );
\r
115 indexer.index( repoMetadata );
\r
116 indexer.optimize();
\r
119 repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
120 indexer.index( repoMetadata );
\r
121 indexer.optimize();
\r
126 * Test the ArtifactRepositoryIndex using a single-phrase search.
\r
128 * @throws Exception
\r
130 public void testSearch()
\r
135 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
136 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
137 //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
138 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
\r
140 // search last update
\r
141 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
142 List metadataList = repoSearchLayer.searchAdvanced( qry );
\r
143 //assertEquals( 1, metadataList.size() );
\r
144 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
146 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
147 if ( hit.isMetadata() )
\r
149 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
150 Metadata metadata = repoMetadata.getMetadata();
\r
151 Versioning versioning = metadata.getVersioning();
\r
152 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
156 // search plugin prefix
\r
157 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );
\r
158 metadataList = repoSearchLayer.searchAdvanced( qry );
\r
159 //assertEquals( 1, metadataList.size() );
\r
160 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
162 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
163 if ( hit.isMetadata() )
\r
165 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
166 Metadata metadata = repoMetadata.getMetadata();
\r
167 List plugins = metadata.getPlugins();
\r
168 for ( Iterator it = plugins.iterator(); it.hasNext(); )
\r
170 Plugin plugin = (Plugin) it.next();
\r
171 assertEquals( "org.apache.maven", plugin.getPrefix() );
\r
176 // search last update using INCLUSIVE Range Query
\r
177 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
178 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212235959" );
\r
179 RangeQuery rQry = new RangeQuery( true );
\r
180 rQry.addQuery( qry1 );
\r
181 rQry.addQuery( qry2 );
\r
183 metadataList = repoSearchLayer.searchAdvanced( rQry );
\r
184 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
\r
186 RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();
\r
187 if ( hit.isMetadata() )
\r
189 RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();
\r
190 Metadata metadata = repoMetadata.getMetadata();
\r
191 Versioning versioning = metadata.getVersioning();
\r
192 assertEquals( "20051212044643", versioning.getLastUpdated() );
\r
196 // search last update using EXCLUSIVE Range Query
\r
197 qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212000000" );
\r
198 qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );
\r
199 rQry = new RangeQuery( false );
\r
200 rQry.addQuery( qry1 );
\r
201 rQry.addQuery( qry2 );
\r
203 metadataList = repoSearchLayer.searchAdvanced( rQry );
\r
204 assertEquals( 0, metadataList.size() );
\r
210 * Test the exceptions thrown by MetadataRepositoryIndex.
\r
212 * @throws Exception
\r
214 public void testExceptions()
\r
217 //test when the object passed in the index(..) method is not a RepositoryMetadata instance
\r
218 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
219 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
222 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
223 indexer.index( artifact );
\r
224 fail( "Must throw exception when the passed object is not a RepositoryMetadata object." );
\r
225 indexer.optimize();
\r
228 catch ( RepositoryIndexException e )
\r
230 assertTrue( true );
\r
235 indexer.deleteIfIndexed( new Object() );
\r
236 fail( "Must throw exception when the passed object is not of type metadata." );
\r
238 catch ( RepositoryIndexException e )
\r
240 assertTrue( true );
\r
245 * Test delete of document from metadata index.
\r
247 * @throws Exception
\r
249 public void testDeleteMetadataDocument()
\r
254 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
255 MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
257 RepositoryMetadata repoMetadata =
\r
258 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA );
\r
259 indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
261 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
\r
262 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
\r
263 List metadataList = repoSearcher.search( qry );
\r
264 assertEquals( 0, metadataList.size() );
\r
268 * Create RepositoryMetadata object.
\r
270 * @param groupId the groupId to be set
\r
271 * @param artifactId the artifactId to be set
\r
272 * @param version the version to be set
\r
273 * @param filename the name of the metadata file
\r
274 * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
\r
275 * @return RepositoryMetadata
\r
276 * @throws Exception
\r
278 private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
\r
279 String metadataType )
\r
282 RepositoryMetadata repoMetadata = null;
\r
285 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
\r
288 if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
\r
290 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
\r
291 is = url.openStream();
\r
292 repoMetadata = new GroupRepositoryMetadata( groupId );
\r
293 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
295 //artifact metadata
\r
296 else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
\r
299 repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();
\r
300 is = url.openStream();
\r
301 repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
302 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
304 //snapshot/version metadata
\r
305 else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
\r
307 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +
\r
308 "/" + filename ).toURL();
\r
309 is = url.openStream();
\r
310 repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
311 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
314 return repoMetadata;
\r
319 * Create artifact object.
\r
321 * @param groupId the groupId of the artifact
\r
322 * @param artifactId the artifactId of the artifact
\r
323 * @param version the version of the artifact
\r
325 * @throws Exception
\r
327 private Artifact getArtifact( String groupId, String artifactId, String version )
\r
330 if ( artifactFactory == null )
\r
332 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
\r
334 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
\r