1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.artifact.InvalidRepositoryException;
27 import org.apache.maven.model.Dependency;
28 import org.apache.maven.model.Parent;
29 import org.apache.maven.model.resolution.ModelResolver;
30 import org.apache.maven.model.resolution.UnresolvableModelException;
31 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
32 import org.eclipse.aether.DefaultRepositorySystemSession;
33 import org.eclipse.aether.RepositorySystem;
34 import org.eclipse.aether.impl.RemoteRepositoryManager;
35 import org.eclipse.aether.repository.RemoteRepository;
36
37 import static junit.framework.TestCase.assertEquals;
38 import static junit.framework.TestCase.assertNotNull;
39 import static junit.framework.TestCase.assertTrue;
40 import static junit.framework.TestCase.fail;
41 import static org.codehaus.plexus.PlexusTestCase.getBasedir;
42
43
44
45
46
47
48
49 public class ProjectModelResolverTest extends AbstractMavenProjectTestCase
50 {
51
52
53
54
55 public ProjectModelResolverTest()
56 {
57 super();
58 }
59
60 public void testResolveParentThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
61 {
62 final Parent parent = new Parent();
63 parent.setGroupId( "org.apache" );
64 parent.setArtifactId( "apache" );
65 parent.setVersion( "0" );
66
67 try
68 {
69 this.newModelResolver().resolveModel( parent );
70 fail( "Expected 'UnresolvableModelException' not thrown." );
71 }
72 catch ( final UnresolvableModelException e )
73 {
74 assertNotNull( e.getMessage() );
75 assertTrue( e.getMessage().startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
76 }
77 }
78
79 public void testResolveParentThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
80 {
81 final Parent parent = new Parent();
82 parent.setGroupId( "org.apache" );
83 parent.setArtifactId( "apache" );
84 parent.setVersion( "[2.0,2.1)" );
85
86 try
87 {
88 this.newModelResolver().resolveModel( parent );
89 fail( "Expected 'UnresolvableModelException' not thrown." );
90 }
91 catch ( final UnresolvableModelException e )
92 {
93 assertEquals( "No versions matched the requested parent version range '[2.0,2.1)'",
94 e.getMessage() );
95
96 }
97 }
98
99 public void testResolveParentThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
100 {
101 final Parent parent = new Parent();
102 parent.setGroupId( "org.apache" );
103 parent.setArtifactId( "apache" );
104 parent.setVersion( "[1,)" );
105
106 try
107 {
108 this.newModelResolver().resolveModel( parent );
109 fail( "Expected 'UnresolvableModelException' not thrown." );
110 }
111 catch ( final UnresolvableModelException e )
112 {
113 assertEquals( "The requested parent version range '[1,)' does not specify an upper bound",
114 e.getMessage() );
115
116 }
117 }
118
119 public void testResolveParentSuccessfullyResolvesExistingParentWithoutRange() throws Exception
120 {
121 final Parent parent = new Parent();
122 parent.setGroupId( "org.apache" );
123 parent.setArtifactId( "apache" );
124 parent.setVersion( "1" );
125
126 assertNotNull( this.newModelResolver().resolveModel( parent ) );
127 assertEquals( "1", parent.getVersion() );
128 }
129
130 public void testResolveParentSuccessfullyResolvesExistingParentUsingHighestVersion() throws Exception
131 {
132 final Parent parent = new Parent();
133 parent.setGroupId( "org.apache" );
134 parent.setArtifactId( "apache" );
135 parent.setVersion( "(,2.0)" );
136
137 assertNotNull( this.newModelResolver().resolveModel( parent ) );
138 assertEquals( "1", parent.getVersion() );
139 }
140
141 public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound() throws Exception
142 {
143 final Dependency dependency = new Dependency();
144 dependency.setGroupId( "org.apache" );
145 dependency.setArtifactId( "apache" );
146 dependency.setVersion( "0" );
147
148 try
149 {
150 this.newModelResolver().resolveModel( dependency );
151 fail( "Expected 'UnresolvableModelException' not thrown." );
152 }
153 catch ( final UnresolvableModelException e )
154 {
155 assertNotNull( e.getMessage() );
156 assertTrue( e.getMessage().startsWith( "Could not find artifact org.apache:apache:pom:0 in central" ) );
157 }
158 }
159
160 public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNoMatchingVersionFound() throws Exception
161 {
162 final Dependency dependency = new Dependency();
163 dependency.setGroupId( "org.apache" );
164 dependency.setArtifactId( "apache" );
165 dependency.setVersion( "[2.0,2.1)" );
166
167 try
168 {
169 this.newModelResolver().resolveModel( dependency );
170 fail( "Expected 'UnresolvableModelException' not thrown." );
171 }
172 catch ( final UnresolvableModelException e )
173 {
174 assertEquals( "No versions matched the requested dependency version range '[2.0,2.1)'",
175 e.getMessage() );
176
177 }
178 }
179
180 public void testResolveDependencyThrowsUnresolvableModelExceptionWhenUsingRangesWithoutUpperBound() throws Exception
181 {
182 final Dependency dependency = new Dependency();
183 dependency.setGroupId( "org.apache" );
184 dependency.setArtifactId( "apache" );
185 dependency.setVersion( "[1,)" );
186
187 try
188 {
189 this.newModelResolver().resolveModel( dependency );
190 fail( "Expected 'UnresolvableModelException' not thrown." );
191 }
192 catch ( final UnresolvableModelException e )
193 {
194 assertEquals( "The requested dependency version range '[1,)' does not specify an upper bound",
195 e.getMessage() );
196
197 }
198 }
199
200 public void testResolveDependencySuccessfullyResolvesExistingDependencyWithoutRange() throws Exception
201 {
202 final Dependency dependency = new Dependency();
203 dependency.setGroupId( "org.apache" );
204 dependency.setArtifactId( "apache" );
205 dependency.setVersion( "1" );
206
207 assertNotNull( this.newModelResolver().resolveModel( dependency ) );
208 assertEquals( "1", dependency.getVersion() );
209 }
210
211 public void testResolveDependencySuccessfullyResolvesExistingDependencyUsingHighestVersion() throws Exception
212 {
213 final Dependency dependency = new Dependency();
214 dependency.setGroupId( "org.apache" );
215 dependency.setArtifactId( "apache" );
216 dependency.setVersion( "(,2.0)" );
217
218 assertNotNull( this.newModelResolver().resolveModel( dependency ) );
219 assertEquals( "1", dependency.getVersion() );
220 }
221
222 private ModelResolver newModelResolver() throws Exception
223 {
224 final File localRepo = new File( this.getLocalRepository().getBasedir() );
225 final DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
226 repoSession.setLocalRepositoryManager( new LegacyLocalRepositoryManager( localRepo ) );
227
228 return new ProjectModelResolver( repoSession, null, lookup( RepositorySystem.class ),
229 lookup( RemoteRepositoryManager.class ),
230 this.getRemoteRepositories(),
231 ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT, null );
232
233 }
234
235 private List<RemoteRepository> getRemoteRepositories()
236 throws InvalidRepositoryException
237 {
238 final File repoDir = new File( getBasedir(), "src/test/remote-repo" ).getAbsoluteFile();
239 final RemoteRepository remoteRepository =
240 new RemoteRepository.Builder( org.apache.maven.repository.RepositorySystem.DEFAULT_REMOTE_REPO_ID,
241 "default", repoDir.toURI().toASCIIString() ).build();
242
243 return Collections.singletonList( remoteRepository );
244 }
245
246 }