From 52d4ebc8fe6722cbaa34635f9a868bcb30c81f79 Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Tue, 22 Mar 2005 03:51:56 +0000 Subject: [PATCH] o Added artifact resolver test that uses the new repository layout. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163641 13f79535-47bb-0310-9956-ffa450edef68 --- .../NewLayoutArtifactComponentTestCase.java | 260 ++++++++++++++++++ .../NewLayoutArtifactResolverTest.java | 250 +++++++++++++++++ 2 files changed, 510 insertions(+) create mode 100644 maven-artifact/src/test/java/org/apache/maven/artifact/NewLayoutArtifactComponentTestCase.java create mode 100644 maven-artifact/src/test/java/org/apache/maven/artifact/resolver/NewLayoutArtifactResolverTest.java diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/NewLayoutArtifactComponentTestCase.java b/maven-artifact/src/test/java/org/apache/maven/artifact/NewLayoutArtifactComponentTestCase.java new file mode 100644 index 0000000000..a00f637efc --- /dev/null +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/NewLayoutArtifactComponentTestCase.java @@ -0,0 +1,260 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.maven.artifact; + +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.codehaus.plexus.PlexusTestCase; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Jason van Zyl + * @version $Id: ArtifactComponentTestCase.java,v 1.5 2004/10/23 13:33:59 + * jvanzyl Exp $ + */ +public abstract class NewLayoutArtifactComponentTestCase + extends PlexusTestCase +{ + protected ArtifactHandlerManager artifactHandlerManager; + + protected void setUp() throws Exception + { + super.setUp(); + + artifactHandlerManager = (ArtifactHandlerManager) lookup( ArtifactHandlerManager.ROLE ); + } + + protected abstract String component(); + + /** Return an existing file, not a directory - causes creation to fail. + * @throws Exception*/ + protected ArtifactRepository badLocalRepository() throws Exception + { + String path = "target/test-classes/repositories/" + component() + "/bad-local-repository"; + + File f = new File( getBasedir(), path ); + + f.createNewFile(); + + ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, + "default" ); + + ArtifactRepository localRepository = new ArtifactRepository( "test", "file://" + f.getPath(), repoLayout ); + + return localRepository; + } + + protected ArtifactRepository localRepository() throws Exception + { + String path = "target/test-classes/repositories/" + component() + "/local-repository"; + + File f = new File( getBasedir(), path ); + + ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, + "default" ); + + ArtifactRepository localRepository = new ArtifactRepository( "local", "file://" + f.getPath(), repoLayout ); + + return localRepository; + } + + protected ArtifactRepository remoteRepository() throws Exception + { + String path = "target/test-classes/repositories/" + component() + "/remote-repository"; + + File f = new File( getBasedir(), path ); + + ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, + "default" ); + + ArtifactRepository repository = new ArtifactRepository( "test", "file://" + f.getPath(), repoLayout ); + + return repository; + } + + protected ArtifactRepository badRemoteRepository() throws Exception + { + ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, + "default" ); + + ArtifactRepository repository = new ArtifactRepository( "test", "http://foo.bar/repository", repoLayout ); + + return repository; + } + + protected void assertRemoteArtifactPresent( Artifact artifact ) throws Exception + { + ArtifactRepository remoteRepo = remoteRepository(); + + String path = remoteRepo.pathOf( artifact ); + + File file = new File( remoteRepo.getBasedir(), path ); + + if ( !file.exists() ) + { + fail( "Remote artifact " + file + " should be present." ); + } + } + + protected void assertLocalArtifactPresent( Artifact artifact ) throws Exception + { + ArtifactRepository localRepo = localRepository(); + + String path = localRepo.pathOf( artifact ); + + File file = new File( localRepo.getBasedir(), path ); + + if ( !file.exists() ) + { + fail( "Local artifact " + file + " should be present." ); + } + } + + protected void assertRemoteArtifactNotPresent( Artifact artifact ) throws Exception + { + ArtifactRepository remoteRepo = remoteRepository(); + + String path = remoteRepo.pathOf( artifact ); + + File file = new File( remoteRepo.getBasedir(), path ); + + if ( file.exists() ) + { + fail( "Remote artifact " + file + " should not be present." ); + } + } + + protected void assertLocalArtifactNotPresent( Artifact artifact ) throws Exception + { + ArtifactRepository localRepo = localRepository(); + + String path = localRepo.pathOf( artifact ); + + File file = new File( localRepo.getBasedir(), path ); + + if ( file.exists() ) + { + fail( "Local artifact " + file + " should not be present." ); + } + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + protected List remoteRepositories() throws Exception + { + List remoteRepositories = new ArrayList(); + + remoteRepositories.add( remoteRepository() ); + + return remoteRepositories; + } + + // ---------------------------------------------------------------------- + // Test artifact generation for unit tests + // ---------------------------------------------------------------------- + + protected Artifact createLocalArtifact( String artifactId, String version ) throws Exception + { + Artifact artifact = createArtifact( artifactId, version ); + + createArtifact( artifact, localRepository() ); + + return artifact; + } + + protected Artifact createRemoteArtifact( String artifactId, String version ) throws Exception + { + Artifact artifact = createArtifact( artifactId, version ); + + createArtifact( artifact, remoteRepository() ); + + return artifact; + } + + protected void createLocalArtifact( Artifact artifact ) throws Exception + { + createArtifact( artifact, localRepository() ); + } + + protected void createRemoteArtifact( Artifact artifact ) throws Exception + { + createArtifact( artifact, remoteRepository() ); + } + + protected void createArtifact( Artifact artifact, ArtifactRepository repository ) throws Exception + { + String path = repository.pathOf( artifact ); + + File artifactFile = new File( repository.getBasedir(), path ); + + if ( !artifactFile.getParentFile().exists() ) + { + artifactFile.getParentFile().mkdirs(); + } + + Writer writer = new FileWriter( artifactFile ); + + writer.write( artifact.getId() ); + + writer.close(); + } + + protected Artifact createArtifact( String artifactId, String version ) + { + return createArtifact( artifactId, version, "jar" ); + } + + protected Artifact createArtifact( String artifactId, String version, String type ) + { + return new DefaultArtifact( "maven", artifactId, version, type ); + } + + protected Artifact createArtifact( String groupId, String artifactId, String version, String type ) + { + return new DefaultArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, type, type ); + } + + protected void deleteLocalArtifact( Artifact artifact ) throws Exception + { + deleteArtifact( artifact, localRepository() ); + } + + protected void deleteArtifact( Artifact artifact, ArtifactRepository repository ) throws Exception + { + String path = repository.pathOf( artifact ); + + File artifactFile = new File( repository.getBasedir(), path ); + + if ( artifactFile.exists() ) + { + if ( !artifactFile.delete() ) + { + throw new IOException( "Failure while attempting to delete artifact " + artifactFile ); + } + } + } +} + diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/resolver/NewLayoutArtifactResolverTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/resolver/NewLayoutArtifactResolverTest.java new file mode 100644 index 0000000000..233bbbe7dd --- /dev/null +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/resolver/NewLayoutArtifactResolverTest.java @@ -0,0 +1,250 @@ +package org.apache.maven.artifact.resolver; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactComponentTestCase; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author Jason van Zyl + * @version $Id$ + */ + +// It would be cool if there was a hook that i could use to setup a test environment. +// I want to setup a local/remote repositories for testing but i don't want to have +// to change them when i change the layout of the repositories. So i want to generate +// the structure i want to test by using the artifact handler manager which dictates +// the layout used for a particular artifact type. +/** + * @author Jason van Zyl + * @version $Id$ + */ +public class NewLayoutArtifactResolverTest + extends ArtifactComponentTestCase +{ + private ArtifactResolver artifactResolver; + + protected void setUp() throws Exception + { + super.setUp(); + + artifactResolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE ); + } + + protected String component() + { + return "resolver"; + } + + public void testResolutionOfASingleArtifactWhereTheArtifactIsPresentInTheLocalRepository() throws Exception + { + Artifact a = createLocalArtifact( "a", "1.0" ); + + artifactResolver.resolve( a, remoteRepositories(), localRepository() ); + + assertLocalArtifactPresent( a ); + } + + public void testResolutionOfASingleArtifactWhereTheArtifactIsNotPresentLocallyAndMustBeRetrievedFromTheRemoteRepository() + throws Exception + { + Artifact b = createRemoteArtifact( "b", "1.0" ); + deleteLocalArtifact( b ); + + artifactResolver.resolve( b, remoteRepositories(), localRepository() ); + + assertLocalArtifactPresent( b ); + } + + public void testResolutionOfASetOfArtifactsWhereTheArtifactsArePresentInTheLocalRepository() throws Exception + { + Set artifacts = new HashSet(); + + Artifact c = createLocalArtifact( "c", "1.0" ); + + Artifact d = createLocalArtifact( "d", "1.0" ); + + artifacts.add( c ); + + artifacts.add( d ); + + Set resolvedArtifacts = artifactResolver.resolve( artifacts, remoteRepositories(), localRepository() ); + + assertEquals( 2, resolvedArtifacts.size() ); + + // The artifacts have undergone no transformations and they are present so the original + // artifacts sent into the resolver should be returned as they were sent in. + + assertTrue( resolvedArtifacts.contains( c ) ); + + assertTrue( resolvedArtifacts.contains( d ) ); + } + + public void testResolutionOfASetOfArtifactsWhereTheArtifactsAreNotPresentInTheLocalRepositoryAndMustBeRetrievedFromTheRemoteRepository() + throws Exception + { + Set artifacts = new HashSet(); + + Artifact e = createRemoteArtifact( "e", "1.0" ); + deleteLocalArtifact( e ); + + Artifact f = createRemoteArtifact( "f", "1.0" ); + deleteLocalArtifact( f ); + + artifacts.add( e ); + + artifacts.add( f ); + + Set resolvedArtifacts = artifactResolver.resolve( artifacts, remoteRepositories(), localRepository() ); + + assertEquals( 2, resolvedArtifacts.size() ); + + // The artifacts have undergone no transformations and they are present so the original + // artifacts sent into the resolver should be returned as they were sent in. + + assertTrue( resolvedArtifacts.contains( e ) ); + + assertTrue( resolvedArtifacts.contains( f ) ); + } + + protected Artifact createArtifact( String groupId, String artifactId, String version, String type ) + { + // for the anonymous classes + return super.createArtifact( groupId, artifactId, version, type ); + } + + public void testTransitiveResolutionWhereAllArtifactsArePresentInTheLocalRepository() throws Exception + { + Artifact g = createLocalArtifact( "g", "1.0" ); + + Artifact h = createLocalArtifact( "h", "1.0" ); + + ArtifactMetadataSource mds = new ArtifactMetadataSource() { + public Set retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + { + Set dependencies = new HashSet(); + + if ( artifact.getArtifactId().equals( "g" ) ) + { + dependencies.add( createArtifact( "maven", "h", "1.0", "jar" ) ); + } + + return dependencies; + } + }; + + ArtifactResolutionResult result = artifactResolver.resolveTransitively( g, remoteRepositories(), + localRepository(), mds ); + + assertEquals( 2, result.getArtifacts().size() ); + + assertTrue( result.getArtifacts().containsKey( g.getId() ) ); + + assertTrue( result.getArtifacts().containsKey( h.getId() ) ); + + assertLocalArtifactPresent( g ); + + assertLocalArtifactPresent( h ); + } + + public void testTransitiveResolutionWhereAllArtifactsAreNotPresentInTheLocalRepositoryAndMustBeRetrievedFromTheRemoteRepository() + throws Exception + { + Artifact i = createRemoteArtifact( "i", "1.0" ); + deleteLocalArtifact( i ); + + Artifact j = createRemoteArtifact( "j", "1.0" ); + deleteLocalArtifact( j ); + + ArtifactMetadataSource mds = new ArtifactMetadataSource() { + public Set retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + { + Set dependencies = new HashSet(); + + if ( artifact.getArtifactId().equals( "i" ) ) + { + dependencies.add( createArtifact( "maven", "j", "1.0", "jar" ) ); + } + + return dependencies; + } + }; + + ArtifactResolutionResult result = artifactResolver.resolveTransitively( i, remoteRepositories(), + localRepository(), mds ); + + assertEquals( 2, result.getArtifacts().size() ); + + assertTrue( result.getArtifacts().containsKey( i.getId() ) ); + + assertTrue( result.getArtifacts().containsKey( j.getId() ) ); + + assertLocalArtifactPresent( i ); + + assertLocalArtifactPresent( j ); + } + + public void testResolutionFailureWhenArtifactNotPresentInRemoteRepository() throws Exception + { + Artifact k = createArtifact( "k", "1.0" ); + + try + { + artifactResolver.resolve( k, remoteRepositories(), localRepository() ); + fail( "Resolution succeeded when it should have failed" ); + } + catch ( ArtifactResolutionException expected ) + { + assertTrue( true ); + } + } + + public void testResolutionOfAnArtifactWhereOneRemoteRepositoryIsBadButOneIsGood() throws Exception + { + Artifact l = createRemoteArtifact( "l", "1.0" ); + deleteLocalArtifact( l ); + + List repositories = new ArrayList(); + repositories.add( remoteRepository() ); + repositories.add( badRemoteRepository() ); + + artifactResolver.resolve( l, repositories, localRepository() ); + + assertLocalArtifactPresent( l ); + } + + /* + public void testResolutionOfASingleArtifactWhereTheArtifactIsNotPresentLocallyAndMustBeRetrievedFromTheRemoteRepositoryAndLocalCannotBeCreated() + throws Exception + { + Artifact m = createRemoteArtifact( "m", "1.0" ); + + artifactResolver.resolve( m, remoteRepositories(), badLocalRepository() ); + + // TODO [failing test case]: throw and handle a more informative exception + } + */ + +} +