1   package org.apache.maven.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.File;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
25  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
26  import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
27  import org.apache.maven.execution.DefaultMavenExecutionRequest;
28  import org.apache.maven.execution.DefaultMavenExecutionResult;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.Dependency;
31  import org.apache.maven.model.Repository;
32  import org.apache.maven.model.RepositoryPolicy;
33  import org.apache.maven.plugin.LegacySupport;
34  import org.apache.maven.repository.RepositorySystem;
35  import org.codehaus.plexus.PlexusTestCase;
36  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
37  import org.sonatype.aether.util.DefaultRepositorySystemSession;
38  
39  /**
40   * Tests {@link LegacyRepositorySystem}.
41   * 
42   * @author Benjamin Bentmann
43   */
44  public class LegacyRepositorySystemTest
45      extends PlexusTestCase
46  {
47      private RepositorySystem repositorySystem;
48  
49      private ResolutionErrorHandler resolutionErrorHandler;
50      
51      @Override
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56          repositorySystem = lookup( RepositorySystem.class, "default" );
57          resolutionErrorHandler = lookup( ResolutionErrorHandler.class );
58      }
59  
60      @Override
61      protected void tearDown()
62          throws Exception
63      {
64          repositorySystem = null;
65          resolutionErrorHandler = null;
66          super.tearDown();
67      }
68      
69      protected List<ArtifactRepository> getRemoteRepositories()
70          throws Exception
71      {
72          File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
73  
74          RepositoryPolicy policy = new RepositoryPolicy();
75          policy.setEnabled( true );
76          policy.setChecksumPolicy( "ignore" );
77          policy.setUpdatePolicy( "always" );
78  
79          Repository repository = new Repository();
80          repository.setId( RepositorySystem.DEFAULT_REMOTE_REPO_ID );
81          repository.setUrl( "file://" + repoDir.toURI().getPath() );
82          repository.setReleases( policy );
83          repository.setSnapshots( policy );
84  
85          return Arrays.asList( repositorySystem.buildArtifactRepository( repository ) );
86      }
87  
88      protected ArtifactRepository getLocalRepository()
89          throws Exception
90      {
91          File repoDir = new File( getBasedir(), "target/local-repo" ).getAbsoluteFile();
92  
93          return repositorySystem.createLocalRepository( repoDir );
94      }
95  
96      public void testThatASystemScopedDependencyIsNotResolvedFromRepositories()
97          throws Exception
98      {
99          //
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 }