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