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.List;
33 public class ArtifactRepositoryIndexingTest
34 extends PlexusTestCase
36 private static final String GROUPID = "groupId";
38 private static final String ARTIFACTID = "artifactId";
40 private static final String VERSION = "version";
42 private static final String CLASSES = "classes";
44 private static final String PACKAGES = "packages";
46 private static final String FILES = "files";
48 private ArtifactFactory artifactFactory;
50 private ArtifactRepository repository;
52 private String indexPath;
54 protected void setUp()
59 File repositoryDirectory = getTestFile( "src/test/repository" );
60 String repoDir = repositoryDirectory.toURL().toString();
61 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
62 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
63 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
65 indexPath = "target/index";
67 FileUtils.deleteDirectory( indexPath );
70 public void testIndexerExceptions()
73 ArtifactRepositoryIndex indexer;
76 String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
77 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
78 indexer.open( notIndexDir );
79 fail( "Must throw exception on non-directory index directory" );
81 catch ( RepositoryIndexException e )
88 String notIndexDir = new File( "" ).getAbsolutePath();
89 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
90 indexer.open( notIndexDir );
91 fail( "Must throw an exception on a non-index directory" );
93 catch ( RepositoryIndexException e )
98 //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
100 indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
101 Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
102 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
106 indexer.indexArtifact( artifact );
107 fail( "Must throw exception on add index with closed index." );
109 catch ( RepositoryIndexException e )
117 fail( "Must throw exception on optimize index with closed index." );
119 catch ( RepositoryIndexException e )
124 indexer.open( indexPath );
128 indexer.index( "should fail" );
129 fail( "Must throw exception on add non-Artifact object." );
131 catch ( RepositoryIndexException e )
139 public void testIndex()
142 //indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
143 ArtifactRepositoryIndex indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
144 indexer.open( indexPath );
146 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
147 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
148 indexer.indexArtifact( artifact );
150 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
151 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
152 indexer.indexArtifact( artifact );
157 indexer.open( indexPath );
158 artifact = getArtifact( "test", "test-artifactId", "1.0" );
159 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
160 indexer.index( artifact );
163 // TODO: assert something!
166 public void testSearch()
169 ArtifactRepositoryIndex indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
170 indexer.open( getTestPath( "src/test/index" ) );
172 RepositoryIndexSearcher repoSearcher =
173 (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE, "artifact" );
175 List artifacts = repoSearcher.search( indexer, "test", GROUPID );
176 assertEquals( 1, artifacts.size() );
178 artifacts = repoSearcher.search( indexer, "test", ARTIFACTID );
179 assertEquals( 1, artifacts.size() );
181 artifacts = repoSearcher.search( indexer, "1.0", VERSION );
182 assertEquals( 1, artifacts.size() );
184 artifacts = repoSearcher.search( indexer, "App", CLASSES );
185 assertEquals( 1, artifacts.size() );
187 artifacts = repoSearcher.search( indexer, "groupId", PACKAGES );
188 assertEquals( 1, artifacts.size() );
190 artifacts = repoSearcher.search( indexer, "pom.xml", FILES );
191 assertEquals( 3, artifacts.size() );
193 artifacts = repoSearcher.search( indexer, "org.apache.maven", GROUPID );
194 assertEquals( 2, artifacts.size() );
196 artifacts = repoSearcher.search( indexer, "maven-artifact", ARTIFACTID );
197 assertEquals( 1, artifacts.size() );
199 artifacts = repoSearcher.search( indexer, "2", VERSION );
200 assertEquals( 2, artifacts.size() );
203 private Artifact getArtifact( String groupId, String artifactId, String version )
206 if ( artifactFactory == null )
208 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
211 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );