1 package org.apache.maven.repository.indexing;
4 * Copyright 2005-2006 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.codehaus.plexus.PlexusTestCase;
25 import org.codehaus.plexus.util.FileUtils;
28 import java.util.Iterator;
29 import java.util.List;
34 public class ArtifactRepositoryIndexingTest
35 extends PlexusTestCase
37 private static final String GROUPID = "groupId";
39 private static final String ARTIFACTID = "artifactId";
41 private static final String VERSION = "version";
43 private static final String SHA1 = "sha1";
45 private static final String MD5 = "md5";
47 private static final String CLASSES = "classes";
49 private static final String PACKAGES = "packages";
51 private static final String FILES = "files";
53 protected ArtifactRepositoryIndex indexer;
55 protected ArtifactFactory artifactFactory;
57 protected ArtifactRepository repository;
59 protected String indexPath;
61 private RepositoryIndexSearcher repoSearcher;
63 protected void setUp()
68 File repositoryDirectory = getTestFile( "src/test/repository" );
69 String repoDir = repositoryDirectory.toURL().toString();
70 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
71 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
72 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
74 indexPath = "target/index";
76 FileUtils.deleteDirectory( indexPath );
79 public void testIndexerExceptions()
84 String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
85 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
86 indexer.open( notIndexDir );
87 fail( "Must throw exception on non-directory index directory" );
89 catch ( RepositoryIndexException e )
96 String notIndexDir = new File( "" ).getAbsolutePath();
97 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
98 indexer.open( notIndexDir );
99 fail( "Must throw an exception on a non-index directory" );
101 catch ( RepositoryIndexException e )
106 //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
108 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
109 Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
110 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
114 indexer.indexArtifact( artifact );
115 fail( "Must throw exception on add index with closed index." );
117 catch ( RepositoryIndexException e )
125 fail( "Must throw exception on optimize index with closed index." );
127 catch ( RepositoryIndexException e )
132 indexer.open( indexPath );
136 indexer.index( "should fail" );
137 fail( "Must throw exception on add non-Artifact object." );
139 catch ( RepositoryIndexException e )
147 public void testIndex()
150 //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
151 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
152 indexer.open( indexPath );
154 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
155 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
156 indexer.indexArtifact( artifact );
158 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
159 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
160 indexer.indexArtifact( artifact );
165 indexer.open( indexPath );
166 artifact = getArtifact( "test", "test-artifactId", "1.0" );
167 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
168 indexer.index( artifact );
172 public void testSearch()
175 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
176 indexer.open( getTestPath( "src/test/index" ) );
178 //repoSearcher = new ArtifactRepositoryIndexSearcher( indexer, indexPath, repository );
179 repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE, "artifact" );
181 List artifacts = repoSearcher.search( indexer, "test", GROUPID );
182 assertEquals( 1, artifacts.size() );
184 artifacts = repoSearcher.search( indexer, "test", ARTIFACTID );
185 assertEquals( 1, artifacts.size() );
187 artifacts = repoSearcher.search( indexer, "1.0", VERSION );
188 assertEquals( 1, artifacts.size() );
190 artifacts = repoSearcher.search( indexer, "App", CLASSES );
191 assertEquals( 1, artifacts.size() );
193 artifacts = repoSearcher.search( indexer, "groupId", PACKAGES );
194 assertEquals( 1, artifacts.size() );
196 artifacts = repoSearcher.search( indexer, "pom.xml", FILES );
197 assertEquals( 3, artifacts.size() );
199 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
201 Artifact artifact = (Artifact) iter.next();
202 File f = artifact.getFile();
203 //assertNotNull( f );
204 //assertTrue( f.exists() );
207 artifacts = repoSearcher.search( indexer, "org.apache.maven", GROUPID );
208 assertEquals( 2, artifacts.size() );
210 artifacts = repoSearcher.search( indexer, "maven-artifact", ARTIFACTID );
211 assertEquals( 1, artifacts.size() );
213 artifacts = repoSearcher.search( indexer, "2", VERSION );
214 assertEquals( 2, artifacts.size() );
217 protected Artifact getArtifact( String groupId, String artifactId, String version )
220 if ( artifactFactory == null )
222 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
225 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );