View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.net.MalformedURLException;
22  import java.util.Arrays;
23  
24  import org.apache.maven.model.Dependency;
25  import org.apache.maven.model.Parent;
26  import org.apache.maven.model.resolution.ModelResolver;
27  import org.apache.maven.model.resolution.UnresolvableModelException;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  import org.eclipse.aether.impl.ArtifactResolver;
30  import org.eclipse.aether.impl.RemoteRepositoryManager;
31  import org.eclipse.aether.impl.VersionRangeResolver;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  /**
40   * Test cases for the default {@code ModelResolver} implementation.
41   *
42   * @since 3.5.0
43   */
44  final class DefaultModelResolverTest extends AbstractRepositoryTestCase {
45  
46      /**
47       * Creates a new {@code DefaultModelResolverTest} instance.
48       */
49      public DefaultModelResolverTest() {
50          super();
51      }
52  
53      @Test
54      public void testResolveParentThrowsUnresolvableModelExceptionWhenNotFound() throws Exception {
55          final Parent parent = new Parent();
56          parent.setGroupId("ut.simple");
57          parent.setArtifactId("artifact");
58          parent.setVersion("0");
59  
60          UnresolvableModelException e = assertThrows(
61                  UnresolvableModelException.class,
62                  () -> newModelResolver().resolveModel(parent),
63                  "Expected 'UnresolvableModelException' not thrown.");
64          assertNotNull(e.getMessage());
65          assertTrue(e.getMessage().contains("Could not find artifact ut.simple:artifact:pom:0 in repo"));
66      }
67  
68      @Test
69      public void testResolveParentThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception {
70          final Parent parent = new Parent();
71          parent.setGroupId("ut.simple");
72          parent.setArtifactId("artifact");
73          parent.setVersion("[2.0,2.1)");
74  
75          UnresolvableModelException e = assertThrows(
76                  UnresolvableModelException.class,
77                  () -> newModelResolver().resolveModel(parent),
78                  "Expected 'UnresolvableModelException' not thrown.");
79          assertNotNull(e.getMessage());
80          assertEquals("No versions matched the requested parent version range '[2.0,2.1)'", e.getMessage());
81      }
82  
83      @Test
84      void testResolveParentThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception {
85          final Parent parent = new Parent();
86          parent.setGroupId("ut.simple");
87          parent.setArtifactId("artifact");
88          parent.setVersion("[1.0,)");
89  
90          UnresolvableModelException e = assertThrows(
91                  UnresolvableModelException.class,
92                  () -> newModelResolver().resolveModel(parent),
93                  "Expected 'UnresolvableModelException' not thrown.");
94          assertEquals("The requested parent version range '[1.0,)' does not specify an upper bound", e.getMessage());
95      }
96  
97      @Test
98      void testResolveParentSuccessfullyResolvesExistingParentWithoutRange() throws Exception {
99          final Parent parent = new Parent();
100         parent.setGroupId("ut.simple");
101         parent.setArtifactId("artifact");
102         parent.setVersion("1.0");
103 
104         assertNotNull(this.newModelResolver().resolveModel(parent));
105         assertEquals("1.0", parent.getVersion());
106     }
107 
108     @Test
109     void testResolveParentSuccessfullyResolvesExistingParentUsingHighestVersion() throws Exception {
110         final Parent parent = new Parent();
111         parent.setGroupId("ut.simple");
112         parent.setArtifactId("artifact");
113         parent.setVersion("(,2.0)");
114 
115         assertNotNull(this.newModelResolver().resolveModel(parent));
116         assertEquals("1.0", parent.getVersion());
117     }
118 
119     @Test
120     void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound() throws Exception {
121         final Dependency dependency = new Dependency();
122         dependency.setGroupId("ut.simple");
123         dependency.setArtifactId("artifact");
124         dependency.setVersion("0");
125 
126         UnresolvableModelException e = assertThrows(
127                 UnresolvableModelException.class,
128                 () -> newModelResolver().resolveModel(dependency),
129                 "Expected 'UnresolvableModelException' not thrown.");
130         assertNotNull(e.getMessage());
131         assertTrue(e.getMessage().contains("Could not find artifact ut.simple:artifact:pom:0 in repo"));
132     }
133 
134     @Test
135     void testResolveDependencyThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception {
136         final Dependency dependency = new Dependency();
137         dependency.setGroupId("ut.simple");
138         dependency.setArtifactId("artifact");
139         dependency.setVersion("[2.0,2.1)");
140 
141         UnresolvableModelException e = assertThrows(
142                 UnresolvableModelException.class,
143                 () -> newModelResolver().resolveModel(dependency),
144                 "Expected 'UnresolvableModelException' not thrown.");
145         assertEquals("No versions matched the requested dependency version range '[2.0,2.1)'", e.getMessage());
146     }
147 
148     @Test
149     void testResolveDependencyThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception {
150         final Dependency dependency = new Dependency();
151         dependency.setGroupId("ut.simple");
152         dependency.setArtifactId("artifact");
153         dependency.setVersion("[1.0,)");
154 
155         UnresolvableModelException e = assertThrows(
156                 UnresolvableModelException.class,
157                 () -> newModelResolver().resolveModel(dependency),
158                 "Expected 'UnresolvableModelException' not thrown.");
159         assertEquals("The requested dependency version range '[1.0,)' does not specify an upper bound", e.getMessage());
160     }
161 
162     @Test
163     void testResolveDependencySuccessfullyResolvesExistingDependencyWithoutRange() throws Exception {
164         final Dependency dependency = new Dependency();
165         dependency.setGroupId("ut.simple");
166         dependency.setArtifactId("artifact");
167         dependency.setVersion("1.0");
168 
169         assertNotNull(this.newModelResolver().resolveModel(dependency));
170         assertEquals("1.0", dependency.getVersion());
171     }
172 
173     @Test
174     void testResolveDependencySuccessfullyResolvesExistingDependencyUsingHighestVersion() throws Exception {
175         final Dependency dependency = new Dependency();
176         dependency.setGroupId("ut.simple");
177         dependency.setArtifactId("artifact");
178         dependency.setVersion("(,2.0)");
179 
180         assertNotNull(this.newModelResolver().resolveModel(dependency));
181         assertEquals("1.0", dependency.getVersion());
182     }
183 
184     private ModelResolver newModelResolver() throws ComponentLookupException, MalformedURLException {
185         return new DefaultModelResolver(
186                 this.session,
187                 null,
188                 this.getClass().getName(),
189                 getContainer().lookup(ArtifactResolver.class),
190                 getContainer().lookup(VersionRangeResolver.class),
191                 getContainer().lookup(RemoteRepositoryManager.class),
192                 Arrays.asList(newTestRepository()));
193     }
194 }