View Javadoc
1   package org.apache.maven.repository.internal;
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 java.net.MalformedURLException;
23  import java.util.Arrays;
24  
25  import org.apache.maven.model.Dependency;
26  import org.apache.maven.model.Parent;
27  import org.apache.maven.model.resolution.ModelResolver;
28  import org.apache.maven.model.resolution.UnresolvableModelException;
29  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
30  import org.eclipse.aether.impl.ArtifactResolver;
31  import org.eclipse.aether.impl.RemoteRepositoryManager;
32  import org.eclipse.aether.impl.VersionRangeResolver;
33  
34  /**
35   * Test cases for the default {@code ModelResolver} implementation.
36   *
37   * @author Christian Schulte
38   * @since 3.5.0
39   */
40  public final class DefaultModelResolverTest extends AbstractRepositoryTestCase
41  {
42  
43      /**
44       * Creates a new {@code DefaultModelResolverTest} instance.
45       */
46      public DefaultModelResolverTest()
47      {
48          super();
49      }
50  
51      public void testResolveParentThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
52      {
53          final Parent parent = new Parent();
54          parent.setGroupId( "ut.simple" );
55          parent.setArtifactId( "artifact" );
56          parent.setVersion( "0" );
57  
58          try
59          {
60              this.newModelResolver().resolveModel( parent );
61              fail( "Expected 'UnresolvableModelException' not thrown." );
62          }
63          catch ( final UnresolvableModelException e )
64          {
65              assertNotNull( e.getMessage() );
66              assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) );
67          }
68      }
69  
70      public void testResolveParentThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
71      {
72          final Parent parent = new Parent();
73          parent.setGroupId( "ut.simple" );
74          parent.setArtifactId( "artifact" );
75          parent.setVersion( "[2.0,2.1)" );
76  
77          try
78          {
79              this.newModelResolver().resolveModel( parent );
80              fail( "Expected 'UnresolvableModelException' not thrown." );
81          }
82          catch ( final UnresolvableModelException e )
83          {
84              assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'",
85                            e.getMessage() );
86  
87          }
88      }
89  
90      public void testResolveParentThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
91      {
92          final Parent parent = new Parent();
93          parent.setGroupId( "ut.simple" );
94          parent.setArtifactId( "artifact" );
95          parent.setVersion( "[1.0,)" );
96  
97          try
98          {
99              this.newModelResolver().resolveModel( parent );
100             fail( "Expected 'UnresolvableModelException' not thrown." );
101         }
102         catch ( final UnresolvableModelException e )
103         {
104             assertEquals( "The requested parent version range '[1.0,)' does not specify an upper bound",
105                           e.getMessage() );
106 
107         }
108     }
109 
110     public void testResolveParentSuccessfullyResolvesExistingParentWithoutRange() throws Exception
111     {
112         final Parent parent = new Parent();
113         parent.setGroupId( "ut.simple" );
114         parent.setArtifactId( "artifact" );
115         parent.setVersion( "1.0" );
116 
117         assertNotNull( this.newModelResolver().resolveModel( parent ) );
118         assertEquals( "1.0", parent.getVersion() );
119     }
120 
121     public void testResolveParentSuccessfullyResolvesExistingParentUsingHighestVersion() throws Exception
122     {
123         final Parent parent = new Parent();
124         parent.setGroupId( "ut.simple" );
125         parent.setArtifactId( "artifact" );
126         parent.setVersion( "(,2.0)" );
127 
128         assertNotNull( this.newModelResolver().resolveModel( parent ) );
129         assertEquals( "1.0", parent.getVersion() );
130     }
131 
132     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
133     {
134         final Dependency dependency = new Dependency();
135         dependency.setGroupId( "ut.simple" );
136         dependency.setArtifactId( "artifact" );
137         dependency.setVersion( "0" );
138 
139         try
140         {
141             this.newModelResolver().resolveModel( dependency );
142             fail( "Expected 'UnresolvableModelException' not thrown." );
143         }
144         catch ( final UnresolvableModelException e )
145         {
146             assertNotNull( e.getMessage() );
147             assertTrue( e.getMessage().startsWith( "Could not find artifact ut.simple:artifact:pom:0 in repo" ) );
148         }
149     }
150 
151     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
152     {
153         final Dependency dependency = new Dependency();
154         dependency.setGroupId( "ut.simple" );
155         dependency.setArtifactId( "artifact" );
156         dependency.setVersion( "[2.0,2.1)" );
157 
158         try
159         {
160             this.newModelResolver().resolveModel( dependency );
161             fail( "Expected 'UnresolvableModelException' not thrown." );
162         }
163         catch ( final UnresolvableModelException e )
164         {
165             assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'",
166                           e.getMessage() );
167 
168         }
169     }
170 
171     public void testResolveDependencyThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
172     {
173         final Dependency dependency = new Dependency();
174         dependency.setGroupId( "ut.simple" );
175         dependency.setArtifactId( "artifact" );
176         dependency.setVersion( "[1.0,)" );
177 
178         try
179         {
180             this.newModelResolver().resolveModel( dependency );
181             fail( "Expected 'UnresolvableModelException' not thrown." );
182         }
183         catch ( final UnresolvableModelException e )
184         {
185             assertEquals( "The requested dependency version range '[1.0,)' does not specify an upper bound",
186                           e.getMessage() );
187 
188         }
189     }
190 
191     public void testResolveDependencySuccessfullyResolvesExistingDependencyWithoutRange() throws Exception
192     {
193         final Dependency dependency = new Dependency();
194         dependency.setGroupId( "ut.simple" );
195         dependency.setArtifactId( "artifact" );
196         dependency.setVersion( "1.0" );
197 
198         assertNotNull( this.newModelResolver().resolveModel( dependency ) );
199         assertEquals( "1.0", dependency.getVersion() );
200     }
201 
202     public void testResolveDependencySuccessfullyResolvesExistingDependencyUsingHighestVersion() throws Exception
203     {
204         final Dependency dependency = new Dependency();
205         dependency.setGroupId( "ut.simple" );
206         dependency.setArtifactId( "artifact" );
207         dependency.setVersion( "(,2.0)" );
208 
209         assertNotNull( this.newModelResolver().resolveModel( dependency ) );
210         assertEquals( "1.0", dependency.getVersion() );
211     }
212 
213     private ModelResolver newModelResolver() throws ComponentLookupException, MalformedURLException
214     {
215         return new DefaultModelResolver( this.session, null, this.getClass().getName(),
216                                          lookup( ArtifactResolver.class ), lookup( VersionRangeResolver.class ),
217                                          lookup( RemoteRepositoryManager.class ),
218                                          Arrays.asList( newTestRepository() ) );
219 
220     }
221 
222 }