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.codehaus.plexus.PlexusTestCase;
\r
20 import org.codehaus.plexus.util.FileUtils;
\r
21 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
\r
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
\r
23 import org.apache.maven.artifact.repository.ArtifactRepository;
\r
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
\r
25 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
\r
26 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
\r
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
\r
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
\r
29 import org.apache.maven.artifact.Artifact;
\r
30 import org.apache.maven.artifact.factory.ArtifactFactory;
\r
31 import org.apache.maven.repository.digest.DefaultDigester;
\r
32 import org.apache.maven.repository.digest.Digester;
\r
33 import org.apache.maven.model.Model;
\r
34 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
\r
36 import java.io.File;
\r
37 import java.io.InputStream;
\r
38 import java.io.InputStreamReader;
\r
39 import java.io.FileReader;
\r
40 import java.util.List;
\r
41 import java.util.Iterator;
\r
42 import java.net.URL;
\r
45 * @author Maria Odea Ching
\r
47 * This class tests the GeneralRepositoryIndexSearcher.
\r
49 public class GeneralRepositoryIndexSearcherTest
\r
50 extends PlexusTestCase
\r
52 private ArtifactRepository repository;
\r
54 private ArtifactFactory artifactFactory;
\r
56 private Digester digester;
\r
58 private String indexPath;
\r
65 protected void setUp()
\r
69 File repositoryDirectory = getTestFile( "src/test/repository" );
\r
70 String repoDir = repositoryDirectory.toURL().toString();
\r
71 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
\r
72 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
\r
73 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
\r
74 digester = new DefaultDigester();
\r
76 indexPath = "target/index";
\r
77 FileUtils.deleteDirectory( indexPath );
\r
85 protected void tearDown()
\r
92 * Method for creating the index used for testing
\r
96 private void createTestIndex()
\r
99 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
100 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
\r
102 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
103 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
\r
104 indexer.indexArtifact( artifact );
\r
105 indexer.optimize();
\r
108 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
\r
109 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
\r
110 indexer.indexArtifact( artifact );
\r
111 indexer.optimize();
\r
114 artifact = getArtifact( "test", "test-artifactId", "1.0" );
\r
115 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
\r
116 indexer.indexArtifact( artifact );
\r
117 indexer.optimize();
\r
120 artifact = getArtifact( "test", "test-artifactId", "1.0" );
\r
121 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
\r
122 indexer.indexArtifact( artifact );
\r
123 indexer.optimize();
\r
126 MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository );
\r
127 RepositoryMetadata repoMetadata =
\r
128 getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );
\r
129 metaIndexer.index( repoMetadata );
\r
130 metaIndexer.optimize();
\r
131 metaIndexer.close();
\r
133 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
134 metaIndexer.ARTIFACT_METADATA );
\r
135 metaIndexer.index( repoMetadata );
\r
136 metaIndexer.optimize();
\r
137 metaIndexer.close();
\r
139 repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
\r
140 metaIndexer.SNAPSHOT_METADATA );
\r
141 metaIndexer.index( repoMetadata );
\r
142 metaIndexer.optimize();
\r
143 metaIndexer.close();
\r
145 repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );
\r
146 metaIndexer.index( repoMetadata );
\r
147 metaIndexer.optimize();
\r
148 metaIndexer.close();
\r
150 PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository );
\r
152 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
\r
153 pomIndexer.indexPom( pom );
\r
154 pomIndexer.optimize();
\r
155 pomIndexer.close();
\r
157 pom = getPom( "org.apache.maven", "maven-model", "2.0" );
\r
158 pomIndexer.indexPom( pom );
\r
159 pomIndexer.optimize();
\r
160 pomIndexer.close();
\r
162 pom = getPom( "test", "test-artifactId", "1.0" );
\r
163 pomIndexer.indexPom( pom );
\r
164 pomIndexer.optimize();
\r
165 pomIndexer.close();
\r
167 pom = getPom( "test", "test-artifactId", "1.0" );
\r
168 pomIndexer.indexPom( pom );
\r
169 pomIndexer.optimize();
\r
170 pomIndexer.close();
\r
174 * Method for testing the "query everything" searcher
\r
176 * @throws Exception
\r
178 public void testGeneralSearcher()
\r
182 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
\r
183 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
\r
184 GeneralRepositoryIndexSearcher searcher = factory.createGeneralRepositoryIndexSearcher( indexer );
\r
186 List returnList = searcher.search( "org.apache.maven" );
\r
187 assertEquals( returnList.size(), 7 );
\r
188 for ( Iterator iter = returnList.iterator(); iter.hasNext(); )
\r
190 Object obj = (Object) iter.next();
\r
191 if ( obj instanceof Artifact )
\r
193 Artifact artifact = (Artifact) obj;
\r
194 assertEquals( artifact.getGroupId(), "org.apache.maven" );
\r
196 else if ( obj instanceof RepositoryMetadata )
\r
198 RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;
\r
199 assertEquals( repoMetadata.getGroupId(), "org.apache.maven" );
\r
203 returnList = searcher.search( "test" );
\r
204 assertEquals( returnList.size(), 3 );
\r
205 for ( Iterator iter = returnList.iterator(); iter.hasNext(); )
\r
207 Object obj = (Object) iter.next();
\r
208 if ( obj instanceof Artifact )
\r
210 Artifact artifact = (Artifact) obj;
\r
211 assertEquals( artifact.getGroupId(), "test" );
\r
213 else if ( obj instanceof RepositoryMetadata )
\r
215 RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;
\r
216 assertEquals( repoMetadata.getGroupId(), "test" );
\r
220 returnList = searcher.search( "artifact" );
\r
221 assertEquals( returnList.size(), 4 );
\r
222 for ( Iterator iter = returnList.iterator(); iter.hasNext(); )
\r
224 Object obj = (Object) iter.next();
\r
225 if ( obj instanceof Artifact )
\r
227 Artifact artifact = (Artifact) obj;
\r
228 assertEquals( artifact.getArtifactId(), "maven-artifact" );
\r
230 else if ( obj instanceof RepositoryMetadata )
\r
232 RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;
\r
233 assertEquals( repoMetadata.getArtifactId(), "maven-artifact" );
\r
239 * Method for creating RepositoryMetadata object
\r
241 * @param groupId the groupId to be set
\r
242 * @param artifactId the artifactId to be set
\r
243 * @param version the version to be set
\r
244 * @param filename the name of the metadata file
\r
245 * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)
\r
246 * @return RepositoryMetadata
\r
247 * @throws Exception
\r
249 private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,
\r
250 String metadataType )
\r
253 RepositoryMetadata repoMetadata = null;
\r
255 InputStream is = null;
\r
256 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
\r
259 if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
\r
261 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();
\r
262 is = url.openStream();
\r
263 repoMetadata = new GroupRepositoryMetadata( groupId );
\r
264 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
266 //artifact metadata
\r
267 else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
\r
270 repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();
\r
271 is = url.openStream();
\r
272 repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
273 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
275 //snapshot/version metadata
\r
276 else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
\r
278 url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +
\r
279 "/" + filename ).toURL();
\r
280 is = url.openStream();
\r
281 repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );
\r
282 repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );
\r
285 return repoMetadata;
\r
289 * Method for creating Artifact object
\r
291 * @param groupId the groupId of the artifact to be created
\r
292 * @param artifactId the artifactId of the artifact to be created
\r
293 * @param version the version of the artifact to be created
\r
294 * @return Artifact object
\r
295 * @throws Exception
\r
297 private Artifact getArtifact( String groupId, String artifactId, String version )
\r
300 if ( artifactFactory == null )
\r
302 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
\r
305 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
\r
309 * Method for creating a Model object given the groupId, artifactId and version
\r
311 * @param groupId the groupId of the model to be created
\r
312 * @param artifactId the artifactId of the model to be created
\r
313 * @param version the version of the model to be created
\r
314 * @return Model object
\r
315 * @throws Exception
\r
317 private Model getPom( String groupId, String artifactId, String version )
\r
320 Artifact artifact = getArtifact( groupId, artifactId, version );
\r
322 return getPom( artifact );
\r
326 * Method for creating a Model object given an artifact
\r
328 * @param artifact the artifact to be created a Model object for
\r
329 * @return Model object
\r
330 * @throws Exception
\r
332 private Model getPom( Artifact artifact )
\r
335 File pomFile = getPomFile( artifact );
\r
337 MavenXpp3Reader pomReader = new MavenXpp3Reader();
\r
338 return pomReader.read( new FileReader( pomFile ) );
\r
342 * Method for creating a pom file
\r
347 private File getPomFile( Artifact artifact )
\r
349 String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
\r
350 return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
\r