001 package 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 018 import java.io.File; 019 import java.util.Arrays; 020 import java.util.List; 021 022 import org.apache.maven.artifact.Artifact; 023 import org.apache.maven.artifact.repository.ArtifactRepository; 024 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; 025 import org.apache.maven.artifact.resolver.ArtifactResolutionResult; 026 import org.apache.maven.artifact.resolver.ResolutionErrorHandler; 027 import org.apache.maven.execution.DefaultMavenExecutionRequest; 028 import org.apache.maven.execution.DefaultMavenExecutionResult; 029 import org.apache.maven.execution.MavenSession; 030 import org.apache.maven.model.Dependency; 031 import org.apache.maven.model.Repository; 032 import org.apache.maven.model.RepositoryPolicy; 033 import org.apache.maven.plugin.LegacySupport; 034 import org.apache.maven.repository.RepositorySystem; 035 import org.codehaus.plexus.PlexusTestCase; 036 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager; 037 import org.sonatype.aether.util.DefaultRepositorySystemSession; 038 039 /** 040 * Tests {@link LegacyRepositorySystem}. 041 * 042 * @author Benjamin Bentmann 043 */ 044 public class LegacyRepositorySystemTest 045 extends PlexusTestCase 046 { 047 private RepositorySystem repositorySystem; 048 049 private ResolutionErrorHandler resolutionErrorHandler; 050 051 @Override 052 protected void setUp() 053 throws Exception 054 { 055 super.setUp(); 056 repositorySystem = lookup( RepositorySystem.class, "default" ); 057 resolutionErrorHandler = lookup( ResolutionErrorHandler.class ); 058 } 059 060 @Override 061 protected void tearDown() 062 throws Exception 063 { 064 repositorySystem = null; 065 resolutionErrorHandler = null; 066 super.tearDown(); 067 } 068 069 protected List<ArtifactRepository> getRemoteRepositories() 070 throws Exception 071 { 072 File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile(); 073 074 RepositoryPolicy policy = new RepositoryPolicy(); 075 policy.setEnabled( true ); 076 policy.setChecksumPolicy( "ignore" ); 077 policy.setUpdatePolicy( "always" ); 078 079 Repository repository = new Repository(); 080 repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID ); 081 repository.setUrl( "file://" + repoDir.toURI().getPath() ); 082 repository.setReleases( policy ); 083 repository.setSnapshots( policy ); 084 085 return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) ); 086 } 087 088 protected ArtifactRepository getLocalRepository() 089 throws Exception 090 { 091 File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile(); 092 093 return repositorySystem.createLocalRepository( repoDir ); 094 } 095 096 public void testThatASystemScopedDependencyIsNotResolvedFromRepositories() 097 throws Exception 098 { 099 // 100 // We should get a whole slew of dependencies resolving this artifact transitively 101 // 102 Dependency d = new Dependency(); 103 d.setGroupId( "org.apache.maven.its" ); 104 d.setArtifactId( "b" ); 105 d.setVersion( "0.1" ); 106 d.setScope( Artifact.SCOPE_COMPILE ); 107 Artifact artifact = repositorySystem.createDependencyArtifact( d ); 108 109 ArtifactResolutionRequest request = new ArtifactResolutionRequest() 110 .setArtifact( artifact ) 111 .setResolveRoot( true ) 112 .setResolveTransitively( true ) 113 .setRemoteRepositories( getRemoteRepositories() ) 114 .setLocalRepository( getLocalRepository() ); 115 116 DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(); 117 session.setLocalRepositoryManager( new SimpleLocalRepositoryManager( request.getLocalRepository().getBasedir() ) ); 118 LegacySupport legacySupport = lookup( LegacySupport.class ); 119 legacySupport.setSession( new MavenSession( getContainer(), session, new DefaultMavenExecutionRequest(), 120 new DefaultMavenExecutionResult() ) ); 121 122 ArtifactResolutionResult result = repositorySystem.resolve( request ); 123 resolutionErrorHandler.throwErrors( request, result ); 124 assertEquals( 2, result.getArtifacts().size() ); 125 126 // 127 // System scoped version which should 128 // 129 d.setScope( Artifact.SCOPE_SYSTEM ); 130 File file = new File( getBasedir(), "src/test/repository-system/maven-core-2.1.0.jar" ); 131 assertTrue( file.exists() ); 132 d.setSystemPath( file.getCanonicalPath() ); 133 134 artifact = repositorySystem.createDependencyArtifact( d ); 135 136 // 137 // The request has not set any local or remote repositories as the system scoped dependency being resolved should only 138 // give us the dependency off the disk and nothing more. 139 // 140 request = new ArtifactResolutionRequest() 141 .setArtifact( artifact ) 142 .setResolveRoot( true ) 143 .setResolveTransitively( true ); 144 145 result = repositorySystem.resolve( request ); 146 resolutionErrorHandler.throwErrors( request, result ); 147 assertEquals( 1, result.getArtifacts().size() ); 148 149 // 150 // Put in a bogus file to make sure missing files cause the resolution to fail. 151 // 152 file = new File( getBasedir(), "src/test/repository-system/maven-monkey-2.1.0.jar" ); 153 assertFalse( file.exists() ); 154 d.setSystemPath( file.getCanonicalPath() ); 155 artifact = repositorySystem.createDependencyArtifact( d ); 156 157 // 158 // The request has not set any local or remote repositories as the system scoped dependency being resolved should only 159 // give us the dependency off the disk and nothing more. 160 // 161 request = new ArtifactResolutionRequest() 162 .setArtifact( artifact ) 163 .setResolveRoot( true ) 164 .setResolveTransitively( true ); 165 166 try 167 { 168 result = repositorySystem.resolve( request ); 169 resolutionErrorHandler.throwErrors( request, result ); 170 } 171 catch( Exception e ) 172 { 173 assertTrue( result.hasMissingArtifacts() ); 174 } 175 } 176 177 public void testLocalRepositoryBasedir() 178 throws Exception 179 { 180 File localRepoDir = new File( "" ).getAbsoluteFile(); 181 182 ArtifactRepository localRepo = repositorySystem.createLocalRepository( localRepoDir ); 183 184 String basedir = localRepo.getBasedir(); 185 186 assertFalse( basedir.endsWith( "/" ) ); 187 assertFalse( basedir.endsWith( "\\" ) ); 188 189 assertEquals( localRepoDir, new File( basedir ) ); 190 191 assertEquals( localRepoDir.getPath(), basedir ); 192 } 193 194 }