/
DefaultVersionsHelperTest.java
231 lines (194 loc) · 10 KB
/
DefaultVersionsHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package org.codehaus.mojo.versions.api;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.resolver.DefaultArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.MavenMetadataSource;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.execution.MavenSession;
import org.codehaus.mojo.versions.Property;
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsIterableContaining.hasItems;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.same;
/**
* Test {@link DefaultVersionsHelper}
*/
public class DefaultVersionsHelperTest extends AbstractMojoTestCase
{
@Test
public void testPerRuleVersionsIgnored() throws Exception
{
final ArtifactMetadataSource metadataSource = mock( ArtifactMetadataSource.class );
final Artifact artifact = mock( Artifact.class );
when( artifact.getGroupId() ).thenReturn( "com.mycompany.maven" );
when( artifact.getArtifactId() ).thenReturn( "artifact-one" );
final List<ArtifactVersion> artifactVersions = new ArrayList<>();
artifactVersions.add( new DefaultArtifactVersion( "one" ) );
artifactVersions.add( new DefaultArtifactVersion( "two" ) );
final ArtifactVersion three = new DefaultArtifactVersion( "three" );
artifactVersions.add( three );
final ArtifactVersion oneTwoHundred = new DefaultArtifactVersion( "1.200" );
artifactVersions.add( oneTwoHundred );
final ArtifactVersion illegal = new DefaultArtifactVersion( "illegalVersion" );
artifactVersions.add( illegal );
when( metadataSource.retrieveAvailableVersions( same( artifact ), any( ArtifactRepository.class ), anyList() ) )
.thenReturn( artifactVersions );
VersionsHelper helper = createHelper( metadataSource );
final ArtifactVersions versions = helper.lookupArtifactVersions( artifact, true );
final List<ArtifactVersion> actual = asList( versions.getVersions( true ) );
assertEquals( 3, actual.size() );
assertThat( actual, hasItems( three, oneTwoHundred, illegal ) );
}
@Test
public void testGlobalRuleVersionsIgnored() throws Exception
{
final ArtifactMetadataSource metadataSource = mock( ArtifactMetadataSource.class );
final Artifact artifact = mock( Artifact.class );
when( artifact.getGroupId() ).thenReturn( "other.company" );
when( artifact.getArtifactId() ).thenReturn( "artifact-two" );
final List<ArtifactVersion> artifactVersions = new ArrayList<>();
final ArtifactVersion one = new DefaultArtifactVersion( "one" );
final ArtifactVersion two = new DefaultArtifactVersion( "two" );
final ArtifactVersion three = new DefaultArtifactVersion( "three" );
artifactVersions.add( one );
artifactVersions.add( two );
artifactVersions.add( new DefaultArtifactVersion( "three-alpha" ) );
artifactVersions.add( new DefaultArtifactVersion( "three-beta" ) );
artifactVersions.add( three );
final ArtifactVersion illegal = new DefaultArtifactVersion( "illegalVersion" );
artifactVersions.add( illegal );
when(metadataSource.retrieveAvailableVersions( same( artifact ), any( ArtifactRepository.class ), anyList() ) )
.thenReturn( artifactVersions );
VersionsHelper helper = createHelper( metadataSource );
final ArtifactVersions versions = helper.lookupArtifactVersions( artifact, true );
final List<ArtifactVersion> actual = asList( versions.getVersions( true ) );
assertEquals( 4, actual.size() );
assertThat( actual, hasItems( one, two, three, illegal ) );
}
@Test
public void testWildcardMatching()
throws Exception
{
assertTrue( DefaultVersionsHelper.exactMatch( "*", "com.foo.bar" ) );
assertFalse( DefaultVersionsHelper.exactMatch( "com.bar*", "com-bar" ) );
assertTrue( DefaultVersionsHelper.exactMatch( "com?foo.bar", "com.foo.bar" ) );
assertTrue( DefaultVersionsHelper.exactMatch( "co*.foo.b?r", "com.foo.bar" ) );
assertTrue( DefaultVersionsHelper.exactMatch( "c*oo*r", "com.foo.bar" ) );
}
@Test
public void testRuleSets()
throws Exception
{
VersionsHelper helper = createHelper();
assertEquals( "no match gives default", VersionComparators.getVersionComparator( "maven" ),
helper.getVersionComparator( "net.foo", "bar" ) );
assertEquals( "matches wildcard", VersionComparators.getVersionComparator( "mercury" ),
helper.getVersionComparator( "org.apache.maven", "plugins" ) );
assertEquals( "exact match wins over initial match", VersionComparators.getVersionComparator( "mercury" ),
helper.getVersionComparator( "com.mycompany.custom.maven", "plugins" ) );
assertEquals( "non-wildcard prefix wins over wildcard prefix match",
VersionComparators.getVersionComparator( "maven" ),
helper.getVersionComparator( "com.mycompany.maven.plugins", "plugins" ) );
assertEquals( VersionComparators.getVersionComparator( "maven" ),
helper.getVersionComparator( "com.mycompany.maven", "new-maven-plugin" ) );
assertEquals( VersionComparators.getVersionComparator( "mercury" ),
helper.getVersionComparator( "com.mycompany.maven", "old-maven-plugin" ) );
}
@Test
public void testMVERSIONS159_ExcludedAndNotIncluded()
throws Exception
{
VersionsHelper helper = createHelper();
MavenProject project = null;
Property[] propertyDefinitions = new Property[] {
new Property( "bar.version" )
};
// should not throw an IllegalStateException
Map result = helper.getVersionPropertiesMap( project, propertyDefinitions, "foo.version", "bar.version", false );
assertTrue( result.isEmpty() );
}
@Test
public void testIsClasspathUriDetectsClassPathProtocol() throws Exception {
DefaultVersionsHelper helper = createHelper();
String uri = "classpath:/p/a/c/k/a/g/e/resource.res";
assertThat(DefaultVersionsHelper.isClasspathUri(uri), CoreMatchers.is(true));
}
@Test
public void testIsClasspathUriDetectsThatItIsDifferentProtocol() throws Exception {
DefaultVersionsHelper helper = createHelper();
String uri = "http://10.10.10.10/p/a/c/k/a/g/e/resource.res";
assertThat(DefaultVersionsHelper.isClasspathUri(uri), CoreMatchers.is(false));
}
private DefaultVersionsHelper createHelper()
throws Exception
{
return createHelper( new MavenMetadataSource() );
}
private DefaultVersionsHelper createHelper( ArtifactMetadataSource metadataSource ) throws Exception
{
final String resourcePath = "/" + getClass().getPackage().getName().replace( '.', '/' ) + "/rules.xml";
final String rulesUri = getClass().getResource( resourcePath ).toExternalForm();
DefaultVersionsHelper helper = createHelper( rulesUri, metadataSource );
return helper;
}
private DefaultVersionsHelper createHelper( String rulesUri, ArtifactMetadataSource metadataSource )
throws Exception
{
WagonManager wagonManager = lookup(WagonManager.class);
// new DefaultWagonManager()
// {
// @Override
// public Wagon getWagon( String protocol )
// throws UnsupportedProtocolException
// {
// return new FileWagon();
// }
// };
DefaultVersionsHelper helper =
new DefaultVersionsHelper( lookup( RepositorySystem.class ), new DefaultArtifactResolver(), metadataSource, new ArrayList(),
new ArrayList(),
new DefaultArtifactRepository( "", "", new DefaultRepositoryLayout() ),
wagonManager, new Settings(), "", rulesUri, mock( Log.class ),
mock( MavenSession.class ), mock( MojoExecution.class ) );
return helper;
}
}