001package org.apache.maven.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 005 * agreements. See the NOTICE file distributed with this work for additional information regarding 006 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance with the License. You may obtain a 008 * copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed under the License 013 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 014 * or implied. See the License for the specific language governing permissions and limitations under 015 * the License. 016 */ 017 018import java.io.File; 019import java.util.Arrays; 020import java.util.List; 021 022import org.apache.maven.artifact.Artifact; 023import org.apache.maven.artifact.repository.ArtifactRepository; 024import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; 025import org.apache.maven.artifact.resolver.ArtifactResolutionResult; 026import org.apache.maven.artifact.resolver.ResolutionErrorHandler; 027import org.apache.maven.execution.DefaultMavenExecutionRequest; 028import org.apache.maven.execution.DefaultMavenExecutionResult; 029import org.apache.maven.execution.MavenSession; 030import org.apache.maven.model.Dependency; 031import org.apache.maven.model.Repository; 032import org.apache.maven.model.RepositoryPolicy; 033import org.apache.maven.plugin.LegacySupport; 034import org.apache.maven.repository.RepositorySystem; 035import org.codehaus.plexus.ContainerConfiguration; 036import org.codehaus.plexus.PlexusConstants; 037import org.codehaus.plexus.PlexusTestCase; 038import org.eclipse.aether.DefaultRepositorySystemSession; 039import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 040import org.eclipse.aether.repository.LocalRepository; 041 042/** 043 * Tests {@link LegacyRepositorySystem}. 044 * 045 * @author Benjamin Bentmann 046 */ 047public class LegacyRepositorySystemTest 048 extends PlexusTestCase 049{ 050 private RepositorySystem repositorySystem; 051 052 private ResolutionErrorHandler resolutionErrorHandler; 053 054 @Override 055 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) 056 { 057 super.customizeContainerConfiguration( containerConfiguration ); 058 containerConfiguration.setAutoWiring( true ); 059 containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX ); 060 } 061 062 @Override 063 protected void setUp() 064 throws Exception 065 { 066 super.setUp(); 067 repositorySystem = lookup( RepositorySystem.class, "default" ); 068 resolutionErrorHandler = lookup( ResolutionErrorHandler.class ); 069 } 070 071 @Override 072 protected void tearDown() 073 throws Exception 074 { 075 repositorySystem = null; 076 resolutionErrorHandler = null; 077 super.tearDown(); 078 } 079 080 protected List<ArtifactRepository> getRemoteRepositories() 081 throws Exception 082 { 083 File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile(); 084 085 RepositoryPolicy policy = new RepositoryPolicy(); 086 policy.setEnabled( true ); 087 policy.setChecksumPolicy( "ignore" ); 088 policy.setUpdatePolicy( "always" ); 089 090 Repository repository = new Repository(); 091 repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID ); 092 repository.setUrl( "file://" + repoDir.toURI().getPath() ); 093 repository.setReleases( policy ); 094 repository.setSnapshots( policy ); 095 096 return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) ); 097 } 098 099 protected ArtifactRepository getLocalRepository() 100 throws Exception 101 { 102 File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile(); 103 104 return repositorySystem.createLocalRepository( repoDir ); 105 } 106 107 public void testThatASystemScopedDependencyIsNotResolvedFromRepositories() 108 throws Exception 109 { 110 // 111 // We should get a whole slew of dependencies resolving this artifact transitively 112 // 113 Dependency d = new Dependency(); 114 d.setGroupId( "org.apache.maven.its" ); 115 d.setArtifactId( "b" ); 116 d.setVersion( "0.1" ); 117 d.setScope( Artifact.SCOPE_COMPILE ); 118 Artifact artifact = repositorySystem.createDependencyArtifact( d ); 119 120 ArtifactResolutionRequest request = new ArtifactResolutionRequest() 121 .setArtifact( artifact ) 122 .setResolveRoot( true ) 123 .setResolveTransitively( true ) 124 .setRemoteRepositories( getRemoteRepositories() ) 125 .setLocalRepository( getLocalRepository() ); 126 127 DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(); 128 LocalRepository localRepo = new LocalRepository( request.getLocalRepository().getBasedir() ); 129 session.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( session, localRepo ) ); 130 LegacySupport legacySupport = lookup( LegacySupport.class ); 131 legacySupport.setSession( new MavenSession( getContainer(), session, new DefaultMavenExecutionRequest(), 132 new DefaultMavenExecutionResult() ) ); 133 134 ArtifactResolutionResult result = repositorySystem.resolve( request ); 135 resolutionErrorHandler.throwErrors( request, result ); 136 assertEquals( 2, result.getArtifacts().size() ); 137 138 // 139 // System scoped version which should 140 // 141 d.setScope( Artifact.SCOPE_SYSTEM ); 142 File file = new File( getBasedir(), "src/test/repository-system/maven-core-2.1.0.jar" ); 143 assertTrue( file.exists() ); 144 d.setSystemPath( file.getCanonicalPath() ); 145 146 artifact = repositorySystem.createDependencyArtifact( d ); 147 148 // 149 // The request has not set any local or remote repositories as the system scoped dependency being resolved should only 150 // give us the dependency off the disk and nothing more. 151 // 152 request = new ArtifactResolutionRequest() 153 .setArtifact( artifact ) 154 .setResolveRoot( true ) 155 .setResolveTransitively( true ); 156 157 result = repositorySystem.resolve( request ); 158 resolutionErrorHandler.throwErrors( request, result ); 159 assertEquals( 1, result.getArtifacts().size() ); 160 161 // 162 // Put in a bogus file to make sure missing files cause the resolution to fail. 163 // 164 file = new File( getBasedir(), "src/test/repository-system/maven-monkey-2.1.0.jar" ); 165 assertFalse( file.exists() ); 166 d.setSystemPath( file.getCanonicalPath() ); 167 artifact = repositorySystem.createDependencyArtifact( d ); 168 169 // 170 // The request has not set any local or remote repositories as the system scoped dependency being resolved should only 171 // give us the dependency off the disk and nothing more. 172 // 173 request = new ArtifactResolutionRequest() 174 .setArtifact( artifact ) 175 .setResolveRoot( true ) 176 .setResolveTransitively( true ); 177 178 try 179 { 180 result = repositorySystem.resolve( request ); 181 resolutionErrorHandler.throwErrors( request, result ); 182 } 183 catch( Exception e ) 184 { 185 assertTrue( result.hasMissingArtifacts() ); 186 } 187 } 188 189 public void testLocalRepositoryBasedir() 190 throws Exception 191 { 192 File localRepoDir = new File( "" ).getAbsoluteFile(); 193 194 ArtifactRepository localRepo = repositorySystem.createLocalRepository( localRepoDir ); 195 196 String basedir = localRepo.getBasedir(); 197 198 assertFalse( basedir.endsWith( "/" ) ); 199 assertFalse( basedir.endsWith( "\\" ) ); 200 201 assertEquals( localRepoDir, new File( basedir ) ); 202 203 assertEquals( localRepoDir.getPath(), basedir ); 204 } 205 206}