1 package org.apache.maven.repository.indexing;
4 * Copyright 2001-2005 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import java.util.Iterator;
22 import java.util.List;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
29 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31 import org.codehaus.plexus.PlexusTestCase;
36 public class ArtifactRepositoryIndexingTest
37 extends PlexusTestCase
39 protected ArtifactRepositoryIndexer indexer;
40 protected ArtifactFactory artifactFactory;
41 protected ArtifactRepository repository;
42 protected String indexPath;
43 private RepositoryIndexSearcher repoSearcher;
44 private static final String GROUPID = "groupId";
45 private static final String ARTIFACTID = "artifactId";
46 private static final String VERSION = "version";
47 private static final String SHA1 = "sha1";
48 private static final String MD5 = "md5";
49 private static final String CLASSES = "classes";
50 private static final String PACKAGES = "packages";
51 private static final String FILES = "files";
53 protected void setUp()
58 File repositoryDirectory = getTestFile( "src/test/repository" );
59 String repoDir = repositoryDirectory.toURL().toString();
61 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
62 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
63 RepositoryIndexerFactory factory = (RepositoryIndexerFactory) lookup( RepositoryIndexerFactory.ROLE );
65 String indexPath = "target/index";
66 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
67 indexer = (ArtifactRepositoryIndexer) factory.getArtifactRepositoryIndexer( indexPath, repository );
68 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
70 repoSearcher = new ArtifactRepositoryIndexSearcher(indexPath, repository);
73 public void testIndex()
76 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
77 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
78 indexer.addArtifactIndex( artifact );
80 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
81 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
82 indexer.addArtifactIndex( artifact );
88 artifact = getArtifact( "test", "test-artifactId", "1.0" );
89 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
90 indexer.addObjectIndex( artifact );
94 public void testSearch() throws Exception{
96 //test the search GROUPID
97 List artifacts = repoSearcher.searchArtifact("test", GROUPID);
98 assertEquals( 1, artifacts.size() );
100 artifacts = repoSearcher.searchArtifact("test", ARTIFACTID);
101 assertEquals( 1, artifacts.size() );
103 artifacts = repoSearcher.searchArtifact("1.0", VERSION);
104 assertEquals( 1, artifacts.size() );
106 artifacts = repoSearcher.searchArtifact("App", CLASSES);
107 assertEquals( 1, artifacts.size() );
109 artifacts = repoSearcher.searchArtifact("groupId", PACKAGES);
110 assertEquals( 1, artifacts.size() );
112 artifacts = repoSearcher.searchArtifact("pom.xml", FILES);
113 assertEquals( 3, artifacts.size() );
115 for( Iterator iter = artifacts.iterator(); iter.hasNext(); )
117 Artifact artifact = (Artifact) iter.next();
118 File f = artifact.getFile();
120 assertTrue( f.exists() );
123 //search org.apache.maven jars
124 artifacts = repoSearcher.searchArtifact("org.apache.maven", GROUPID);
125 assertEquals( 2, artifacts.size() );
127 artifacts = repoSearcher.searchArtifact("maven-artifact", ARTIFACTID);
128 assertEquals( 1, artifacts.size() );
130 artifacts = repoSearcher.searchArtifact("2", VERSION);
131 assertEquals( 2, artifacts.size() );
135 public void testIndexerExceptions()
139 Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
140 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
144 indexer.addArtifactIndex( artifact );
145 fail( "Must throw exception on add index with closed index." );
147 catch( RepositoryIndexerException e )
155 fail( "Must throw exception on optimize index with closed index." );
157 catch( RepositoryIndexerException e )
166 indexer.addObjectIndex( "should fail" );
167 fail( "Must throw exception on add non-Artifact object." );
169 catch( RepositoryIndexerException e )
175 protected Artifact getArtifact( String groupId, String artifactId, String version )
177 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );