1 package org.apache.maven.repository.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40 public final class DefaultModelResolverTest extends AbstractRepositoryTestCase
41 {
42
43
44
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 }