View Javadoc
1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.hamcrest.Matchers.startsWith;
23  import static org.junit.Assert.assertThat;
24  
25  import java.io.File;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.maven.artifact.InvalidRepositoryException;
30  import org.apache.maven.model.Dependency;
31  import org.apache.maven.model.Parent;
32  import org.apache.maven.model.resolution.ModelResolver;
33  import org.apache.maven.model.resolution.UnresolvableModelException;
34  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
35  import org.eclipse.aether.DefaultRepositorySystemSession;
36  import org.eclipse.aether.RepositorySystem;
37  import org.eclipse.aether.impl.RemoteRepositoryManager;
38  import org.eclipse.aether.repository.RemoteRepository;
39  
40  /**
41   * Test cases for the project {@code ModelResolver} implementation.
42   *
43   * @author Christian Schulte
44   * @since 3.5.0
45   */
46  public class ProjectModelResolverTest extends AbstractMavenProjectTestCase
47  {
48  
49      /**
50       * Creates a new {@code ProjectModelResolverTest} instance.
51       */
52      public ProjectModelResolverTest()
53      {
54          super();
55      }
56  
57      public void testResolveParentThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
58      {
59          final Parent parent = new Parent();
60          parent.setGroupId( "org.apache" );
61          parent.setArtifactId( "apache" );
62          parent.setVersion( "0" );
63  
64          try
65          {
66              this.newModelResolver().resolveModel( parent );
67              fail( "Expected 'UnresolvableModelException' not thrown." );
68          }
69          catch ( final UnresolvableModelException e )
70          {
71              assertNotNull( e.getMessage() );
72              assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
73          }
74      }
75  
76      public void testResolveParentThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
77      {
78          final Parent parent = new Parent();
79          parent.setGroupId( "org.apache" );
80          parent.setArtifactId( "apache" );
81          parent.setVersion( "[2.0,2.1)" );
82  
83          try
84          {
85              this.newModelResolver().resolveModel( parent );
86              fail( "Expected 'UnresolvableModelException' not thrown." );
87          }
88          catch ( final UnresolvableModelException e )
89          {
90              assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'",
91                            e.getMessage() );
92  
93          }
94      }
95  
96      public void testResolveParentThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
97      {
98          final Parent parent = new Parent();
99          parent.setGroupId( "org.apache" );
100         parent.setArtifactId( "apache" );
101         parent.setVersion( "[1,)" );
102 
103         try
104         {
105             this.newModelResolver().resolveModel( parent );
106             fail( "Expected 'UnresolvableModelException' not thrown." );
107         }
108         catch ( final UnresolvableModelException e )
109         {
110             assertEquals( "The requested parent version range '[1,)' does not specify an upper bound",
111                           e.getMessage() );
112 
113         }
114     }
115 
116     public void testResolveParentSuccessfullyResolvesExistingParentWithoutRange() throws Exception
117     {
118         final Parent parent = new Parent();
119         parent.setGroupId( "org.apache" );
120         parent.setArtifactId( "apache" );
121         parent.setVersion( "1" );
122 
123         assertNotNull( this.newModelResolver().resolveModel( parent ) );
124         assertEquals( "1", parent.getVersion() );
125     }
126 
127     public void testResolveParentSuccessfullyResolvesExistingParentUsingHighestVersion() throws Exception
128     {
129         final Parent parent = new Parent();
130         parent.setGroupId( "org.apache" );
131         parent.setArtifactId( "apache" );
132         parent.setVersion( "(,2.0)" );
133 
134         assertNotNull( this.newModelResolver().resolveModel( parent ) );
135         assertEquals( "1", parent.getVersion() );
136     }
137 
138     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
139     {
140         final Dependency dependency = new Dependency();
141         dependency.setGroupId( "org.apache" );
142         dependency.setArtifactId( "apache" );
143         dependency.setVersion( "0" );
144 
145         try
146         {
147             this.newModelResolver().resolveModel( dependency );
148             fail( "Expected 'UnresolvableModelException' not thrown." );
149         }
150         catch ( final UnresolvableModelException e )
151         {
152             assertNotNull( e.getMessage() );
153             assertThat( e.getMessage(), startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
154         }
155     }
156 
157     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
158     {
159         final Dependency dependency = new Dependency();
160         dependency.setGroupId( "org.apache" );
161         dependency.setArtifactId( "apache" );
162         dependency.setVersion( "[2.0,2.1)" );
163 
164         try
165         {
166             this.newModelResolver().resolveModel( dependency );
167             fail( "Expected 'UnresolvableModelException' not thrown." );
168         }
169         catch ( final UnresolvableModelException e )
170         {
171             assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'",
172                           e.getMessage() );
173 
174         }
175     }
176 
177     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
178     {
179         final Dependency dependency = new Dependency();
180         dependency.setGroupId( "org.apache" );
181         dependency.setArtifactId( "apache" );
182         dependency.setVersion( "[1,)" );
183 
184         try
185         {
186             this.newModelResolver().resolveModel( dependency );
187             fail( "Expected 'UnresolvableModelException' not thrown." );
188         }
189         catch ( final UnresolvableModelException e )
190         {
191             assertEquals( "The requested dependency version range '[1,)' does not specify an upper bound",
192                           e.getMessage() );
193 
194         }
195     }
196 
197     public void testResolveDependencySuccessfullyResolvesExistingDependencyWithoutRange() throws Exception
198     {
199         final Dependency dependency = new Dependency();
200         dependency.setGroupId( "org.apache" );
201         dependency.setArtifactId( "apache" );
202         dependency.setVersion( "1" );
203 
204         assertNotNull( this.newModelResolver().resolveModel( dependency ) );
205         assertEquals( "1", dependency.getVersion() );
206     }
207 
208     public void testResolveDependencySuccessfullyResolvesExistingDependencyUsingHighestVersion() throws Exception
209     {
210         final Dependency dependency = new Dependency();
211         dependency.setGroupId( "org.apache" );
212         dependency.setArtifactId( "apache" );
213         dependency.setVersion( "(,2.0)" );
214 
215         assertNotNull( this.newModelResolver().resolveModel( dependency ) );
216         assertEquals( "1", dependency.getVersion() );
217     }
218 
219     private ModelResolver newModelResolver() throws Exception
220     {
221         final File localRepo = new File( this.getLocalRepository().getBasedir() );
222         final DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
223         repoSession.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
224 
225         return new ProjectModelResolver( repoSession, null, lookup( RepositorySystem.class ),
226                                          lookup( RemoteRepositoryManager.class ),
227                                          this.getRemoteRepositories(),
228                                          ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT, null );
229 
230     }
231 
232     private List<RemoteRepository> getRemoteRepositories()
233         throws InvalidRepositoryException
234     {
235         final File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
236         final RemoteRepository remoteRepository =
237             new RemoteRepository.Builder( org.apache.maven.repository.RepositorySystem.DEFAULT_REMOTE_REPO_ID,
238                                           "default", repoDir.toURI().toASCIIString() ).build();
239 
240         return Collections.singletonList( remoteRepository );
241     }
242 
243 }