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.artifact.versioning;
20  
21  import java.util.List;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertFalse;
28  import static org.junit.jupiter.api.Assertions.assertNull;
29  import static org.junit.jupiter.api.Assertions.assertSame;
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Tests version range construction.
35   *
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   */
38  public class VersionRangeTest {
39      private static final String CHECK_NUM_RESTRICTIONS = "check number of restrictions";
40  
41      private static final String CHECK_UPPER_BOUND = "check upper bound";
42  
43      private static final String CHECK_UPPER_BOUND_INCLUSIVE = "check upper bound is inclusive";
44  
45      private static final String CHECK_LOWER_BOUND = "check lower bound";
46  
47      private static final String CHECK_LOWER_BOUND_INCLUSIVE = "check lower bound is inclusive";
48  
49      private static final String CHECK_VERSION_RECOMMENDATION = "check version recommended";
50  
51      private static final String CHECK_SELECTED_VERSION_KNOWN = "check selected version known";
52  
53      private static final String CHECK_SELECTED_VERSION = "check selected version";
54  
55      @Test
56      public void testRange() throws InvalidVersionSpecificationException, OverConstrainedVersionException {
57          Artifact artifact = null;
58  
59          VersionRange range = VersionRange.createFromVersionSpec("(,1.0]");
60          List<Restriction> restrictions = range.getRestrictions();
61          assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
62          Restriction restriction = restrictions.get(0);
63          assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
64          assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
65          assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
66          assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
67          assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
68          assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
69          assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
70  
71          range = VersionRange.createFromVersionSpec("1.0");
72          assertEquals("1.0", range.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
73          restrictions = range.getRestrictions();
74          assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
75          restriction = restrictions.get(0);
76          assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
77          assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
78          assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
79          assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
80          assertTrue(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
81          assertEquals("1.0", range.getSelectedVersion(artifact).toString(), CHECK_SELECTED_VERSION);
82  
83          range = VersionRange.createFromVersionSpec("[1.0]");
84          restrictions = range.getRestrictions();
85          assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
86          restriction = restrictions.get(0);
87          assertEquals("1.0", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
88          assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
89          assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
90          assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
91          assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
92          assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
93          assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
94  
95          range = VersionRange.createFromVersionSpec("[1.2,1.3]");
96          restrictions = range.getRestrictions();
97          assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
98          restriction = restrictions.get(0);
99          assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
100         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
101         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
102         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
103         assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
104         assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
105         assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
106 
107         range = VersionRange.createFromVersionSpec("[1.0,2.0)");
108         restrictions = range.getRestrictions();
109         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
110         restriction = restrictions.get(0);
111         assertEquals("1.0", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
112         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
113         assertEquals("2.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
114         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
115         assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
116         assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
117         assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
118 
119         range = VersionRange.createFromVersionSpec("[1.5,)");
120         restrictions = range.getRestrictions();
121         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
122         restriction = restrictions.get(0);
123         assertEquals("1.5", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
124         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
125         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
126         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
127         assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
128         assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
129         assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
130 
131         range = VersionRange.createFromVersionSpec("(,1.0],[1.2,)");
132         restrictions = range.getRestrictions();
133         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
134         restriction = restrictions.get(0);
135         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
136         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
137         assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
138         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
139         assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
140         restriction = restrictions.get(1);
141         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
142         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
143         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
144         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
145         assertNull(range.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
146         assertFalse(range.isSelectedVersionKnown(artifact), CHECK_SELECTED_VERSION_KNOWN);
147         assertNull(range.getSelectedVersion(artifact), CHECK_SELECTED_VERSION);
148 
149         range = VersionRange.createFromVersionSpec("[1.0,)");
150         assertFalse(range.containsVersion(new DefaultArtifactVersion("1.0-SNAPSHOT")));
151 
152         range = VersionRange.createFromVersionSpec("[1.0,1.1-SNAPSHOT]");
153         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.1-SNAPSHOT")));
154 
155         range = VersionRange.createFromVersionSpec("[5.0.9.0,5.0.10.0)");
156         assertTrue(range.containsVersion(new DefaultArtifactVersion("5.0.9.0")));
157     }
158 
159     @Test
160     public void testSameUpperAndLowerBoundRoundtrip() throws InvalidVersionSpecificationException {
161         VersionRange range = VersionRange.createFromVersionSpec("[1.0]");
162         VersionRange range2 = VersionRange.createFromVersionSpec(range.toString());
163         assertEquals(range, range2);
164     }
165 
166     @Test
167     public void testInvalidRanges() {
168         checkInvalidRange("(1.0)");
169         checkInvalidRange("[1.0)");
170         checkInvalidRange("(1.0]");
171         checkInvalidRange("(1.0,1.0]");
172         checkInvalidRange("[1.0,1.0)");
173         checkInvalidRange("(1.0,1.0)");
174         checkInvalidRange("[1.1,1.0]");
175         checkInvalidRange("[1.0,1.2),1.3");
176         // overlap
177         checkInvalidRange("[1.0,1.2),(1.1,1.3]");
178         // overlap
179         checkInvalidRange("[1.1,1.3),(1.0,1.2]");
180         // ordering
181         checkInvalidRange("(1.1,1.2],[1.0,1.1)");
182     }
183 
184     @Test
185     public void testIntersections() throws InvalidVersionSpecificationException {
186         VersionRange range1 = VersionRange.createFromVersionSpec("1.0");
187         VersionRange range2 = VersionRange.createFromVersionSpec("1.1");
188         VersionRange mergedRange = range1.restrict(range2);
189         // TODO current policy is to retain the original version - is this correct, do we need strategies or is that
190         // handled elsewhere?
191         //        assertEquals( "1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION );
192         assertEquals("1.0", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
193         List<Restriction> restrictions = mergedRange.getRestrictions();
194         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
195         Restriction restriction = restrictions.get(0);
196         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
197         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
198         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
199         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
200 
201         mergedRange = range2.restrict(range1);
202         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
203         restrictions = mergedRange.getRestrictions();
204         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
205         restriction = restrictions.get(0);
206         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
207         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
208         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
209         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
210 
211         // TODO test reversed restrictions on all below
212         range1 = VersionRange.createFromVersionSpec("[1.0,)");
213         range2 = VersionRange.createFromVersionSpec("1.1");
214         mergedRange = range1.restrict(range2);
215         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
216         restrictions = mergedRange.getRestrictions();
217         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
218         restriction = restrictions.get(0);
219         assertEquals("1.0", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
220         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
221         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
222         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
223 
224         range1 = VersionRange.createFromVersionSpec("[1.1,)");
225         range2 = VersionRange.createFromVersionSpec("1.1");
226         mergedRange = range1.restrict(range2);
227         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
228         restrictions = mergedRange.getRestrictions();
229         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
230         restriction = restrictions.get(0);
231         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
232         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
233         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
234         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
235 
236         range1 = VersionRange.createFromVersionSpec("[1.1]");
237         range2 = VersionRange.createFromVersionSpec("1.1");
238         mergedRange = range1.restrict(range2);
239         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
240         restrictions = mergedRange.getRestrictions();
241         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
242         restriction = restrictions.get(0);
243         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
244         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
245         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_UPPER_BOUND);
246         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
247 
248         range1 = VersionRange.createFromVersionSpec("(1.1,)");
249         range2 = VersionRange.createFromVersionSpec("1.1");
250         mergedRange = range1.restrict(range2);
251         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
252         restrictions = mergedRange.getRestrictions();
253         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
254         restriction = restrictions.get(0);
255         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
256         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
257         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
258         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
259 
260         range1 = VersionRange.createFromVersionSpec("[1.2,)");
261         range2 = VersionRange.createFromVersionSpec("1.1");
262         mergedRange = range1.restrict(range2);
263         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
264         restrictions = mergedRange.getRestrictions();
265         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
266         restriction = restrictions.get(0);
267         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
268         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
269         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
270         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
271 
272         range1 = VersionRange.createFromVersionSpec("(,1.2]");
273         range2 = VersionRange.createFromVersionSpec("1.1");
274         mergedRange = range1.restrict(range2);
275         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
276         restrictions = mergedRange.getRestrictions();
277         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
278         restriction = restrictions.get(0);
279         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
280         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
281         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
282         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
283 
284         range1 = VersionRange.createFromVersionSpec("(,1.1]");
285         range2 = VersionRange.createFromVersionSpec("1.1");
286         mergedRange = range1.restrict(range2);
287         assertEquals("1.1", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
288         restrictions = mergedRange.getRestrictions();
289         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
290         restriction = restrictions.get(0);
291         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
292         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
293         assertEquals("1.1", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
294         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
295 
296         range1 = VersionRange.createFromVersionSpec("(,1.1)");
297         range2 = VersionRange.createFromVersionSpec("1.1");
298         mergedRange = range1.restrict(range2);
299         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
300         restrictions = mergedRange.getRestrictions();
301         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
302         restriction = restrictions.get(0);
303         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
304         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
305         assertEquals("1.1", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
306         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
307 
308         range1 = VersionRange.createFromVersionSpec("(,1.0]");
309         range2 = VersionRange.createFromVersionSpec("1.1");
310         mergedRange = range1.restrict(range2);
311         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
312         restrictions = mergedRange.getRestrictions();
313         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
314         restriction = restrictions.get(0);
315         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
316         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
317         assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
318         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
319 
320         range1 = VersionRange.createFromVersionSpec("(,1.0], [1.1,)");
321         range2 = VersionRange.createFromVersionSpec("1.2");
322         mergedRange = range1.restrict(range2);
323         assertEquals("1.2", mergedRange.getRecommendedVersion().toString(), CHECK_VERSION_RECOMMENDATION);
324         restrictions = mergedRange.getRestrictions();
325         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
326         restriction = restrictions.get(0);
327         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
328         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
329         assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
330         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
331         restriction = restrictions.get(1);
332         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
333         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
334         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
335         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
336 
337         range1 = VersionRange.createFromVersionSpec("(,1.0], [1.1,)");
338         range2 = VersionRange.createFromVersionSpec("1.0.5");
339         mergedRange = range1.restrict(range2);
340         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
341         restrictions = mergedRange.getRestrictions();
342         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
343         restriction = restrictions.get(0);
344         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
345         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
346         assertEquals("1.0", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
347         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
348         restriction = restrictions.get(1);
349         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
350         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
351         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
352         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
353 
354         range1 = VersionRange.createFromVersionSpec("(,1.1), (1.1,)");
355         range2 = VersionRange.createFromVersionSpec("1.1");
356         mergedRange = range1.restrict(range2);
357         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
358         restrictions = mergedRange.getRestrictions();
359         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
360         restriction = restrictions.get(0);
361         assertNull(restriction.getLowerBound(), CHECK_LOWER_BOUND);
362         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
363         assertEquals("1.1", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
364         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
365         restriction = restrictions.get(1);
366         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
367         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
368         assertNull(restriction.getUpperBound(), CHECK_UPPER_BOUND);
369         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
370 
371         range1 = VersionRange.createFromVersionSpec("[1.1,1.3]");
372         range2 = VersionRange.createFromVersionSpec("(1.1,)");
373         mergedRange = range1.restrict(range2);
374         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
375         restrictions = mergedRange.getRestrictions();
376         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
377         restriction = restrictions.get(0);
378         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
379         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
380         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
381         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
382 
383         range1 = VersionRange.createFromVersionSpec("(,1.3)");
384         range2 = VersionRange.createFromVersionSpec("[1.2,1.3]");
385         mergedRange = range1.restrict(range2);
386         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
387         restrictions = mergedRange.getRestrictions();
388         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
389         restriction = restrictions.get(0);
390         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
391         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
392         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
393         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
394 
395         range1 = VersionRange.createFromVersionSpec("[1.1,1.3]");
396         range2 = VersionRange.createFromVersionSpec("[1.2,)");
397         mergedRange = range1.restrict(range2);
398         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
399         restrictions = mergedRange.getRestrictions();
400         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
401         restriction = restrictions.get(0);
402         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
403         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
404         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
405         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
406 
407         range1 = VersionRange.createFromVersionSpec("(,1.3]");
408         range2 = VersionRange.createFromVersionSpec("[1.2,1.4]");
409         mergedRange = range1.restrict(range2);
410         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
411         restrictions = mergedRange.getRestrictions();
412         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
413         restriction = restrictions.get(0);
414         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
415         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
416         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
417         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
418 
419         range1 = VersionRange.createFromVersionSpec("(1.2,1.3]");
420         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
421         mergedRange = range1.restrict(range2);
422         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
423         restrictions = mergedRange.getRestrictions();
424         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
425         restriction = restrictions.get(0);
426         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
427         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
428         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
429         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
430 
431         range1 = VersionRange.createFromVersionSpec("(1.2,1.3)");
432         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
433         mergedRange = range1.restrict(range2);
434         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
435         restrictions = mergedRange.getRestrictions();
436         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
437         restriction = restrictions.get(0);
438         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
439         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
440         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
441         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
442 
443         range1 = VersionRange.createFromVersionSpec("[1.2,1.3)");
444         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
445         mergedRange = range1.restrict(range2);
446         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
447         restrictions = mergedRange.getRestrictions();
448         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
449         restriction = restrictions.get(0);
450         assertEquals("1.2", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
451         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
452         assertEquals("1.3", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
453         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
454 
455         range1 = VersionRange.createFromVersionSpec("[1.0,1.1]");
456         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
457         mergedRange = range1.restrict(range2);
458         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
459         restrictions = mergedRange.getRestrictions();
460         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
461         restriction = restrictions.get(0);
462         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
463         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
464         assertEquals("1.1", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
465         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
466 
467         range1 = VersionRange.createFromVersionSpec("[1.0,1.1)");
468         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
469         mergedRange = range1.restrict(range2);
470         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
471         restrictions = mergedRange.getRestrictions();
472         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
473 
474         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
475         range2 = VersionRange.createFromVersionSpec("[1.1]");
476         mergedRange = range1.restrict(range2);
477         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
478         restrictions = mergedRange.getRestrictions();
479         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
480         restriction = restrictions.get(0);
481         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
482         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
483         assertEquals("1.1", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
484         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
485 
486         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
487         range2 = VersionRange.createFromVersionSpec("[1.4]");
488         mergedRange = range1.restrict(range2);
489         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
490         restrictions = mergedRange.getRestrictions();
491         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
492         restriction = restrictions.get(0);
493         assertEquals("1.4", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
494         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
495         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
496         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
497 
498         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
499         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
500         mergedRange = range1.restrict(range2);
501         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
502         restrictions = mergedRange.getRestrictions();
503         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
504         restriction = restrictions.get(0);
505         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
506         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
507         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
508         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
509         restriction = restrictions.get(1);
510         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
511         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
512         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
513         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
514 
515         range1 = VersionRange.createFromVersionSpec("[1.0,1.2),(1.3,1.5]");
516         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
517         mergedRange = range1.restrict(range2);
518         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
519         restrictions = mergedRange.getRestrictions();
520         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
521         restriction = restrictions.get(0);
522         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
523         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
524         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
525         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
526         restriction = restrictions.get(1);
527         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
528         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
529         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
530         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
531 
532         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
533         range2 = VersionRange.createFromVersionSpec("(1.1,1.4)");
534         mergedRange = range1.restrict(range2);
535         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
536         restrictions = mergedRange.getRestrictions();
537         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
538         restriction = restrictions.get(0);
539         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
540         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
541         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
542         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
543         restriction = restrictions.get(1);
544         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
545         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
546         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
547         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
548 
549         range1 = VersionRange.createFromVersionSpec("[1.0,1.2),(1.3,1.5]");
550         range2 = VersionRange.createFromVersionSpec("(1.1,1.4)");
551         mergedRange = range1.restrict(range2);
552         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
553         restrictions = mergedRange.getRestrictions();
554         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
555         restriction = restrictions.get(0);
556         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
557         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
558         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
559         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
560         restriction = restrictions.get(1);
561         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
562         assertFalse(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
563         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
564         assertFalse(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
565 
566         range1 = VersionRange.createFromVersionSpec("(,1.1),(1.4,)");
567         range2 = VersionRange.createFromVersionSpec("[1.1,1.4]");
568         mergedRange = range1.restrict(range2);
569         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
570         restrictions = mergedRange.getRestrictions();
571         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
572 
573         range1 = VersionRange.createFromVersionSpec("(,1.1],[1.4,)");
574         range2 = VersionRange.createFromVersionSpec("(1.1,1.4)");
575         mergedRange = range1.restrict(range2);
576         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
577         restrictions = mergedRange.getRestrictions();
578         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
579 
580         range1 = VersionRange.createFromVersionSpec("[,1.1],[1.4,]");
581         range2 = VersionRange.createFromVersionSpec("[1.2,1.3]");
582         mergedRange = range1.restrict(range2);
583         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
584         restrictions = mergedRange.getRestrictions();
585         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
586 
587         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
588         range2 = VersionRange.createFromVersionSpec("[1.1,1.4],[1.6,]");
589         mergedRange = range1.restrict(range2);
590         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
591         restrictions = mergedRange.getRestrictions();
592         assertEquals(2, restrictions.size(), CHECK_NUM_RESTRICTIONS);
593         restriction = restrictions.get(0);
594         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
595         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
596         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
597         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
598         restriction = restrictions.get(1);
599         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
600         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
601         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
602         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
603 
604         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.5]");
605         range2 = VersionRange.createFromVersionSpec("[1.1,1.4],[1.5,]");
606         mergedRange = range1.restrict(range2);
607         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
608         restrictions = mergedRange.getRestrictions();
609         assertEquals(3, restrictions.size(), CHECK_NUM_RESTRICTIONS);
610         restriction = restrictions.get(0);
611         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
612         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
613         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
614         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
615         restriction = restrictions.get(1);
616         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
617         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
618         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
619         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
620         restriction = restrictions.get(2);
621         assertEquals("1.5", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
622         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
623         assertEquals("1.5", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
624         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
625 
626         range1 = VersionRange.createFromVersionSpec("[1.0,1.2],[1.3,1.7]");
627         range2 = VersionRange.createFromVersionSpec("[1.1,1.4],[1.5,1.6]");
628         mergedRange = range1.restrict(range2);
629         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
630         restrictions = mergedRange.getRestrictions();
631         assertEquals(3, restrictions.size(), CHECK_NUM_RESTRICTIONS);
632         restriction = restrictions.get(0);
633         assertEquals("1.1", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
634         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
635         assertEquals("1.2", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
636         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
637         restriction = restrictions.get(1);
638         assertEquals("1.3", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
639         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
640         assertEquals("1.4", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
641         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
642         restriction = restrictions.get(2);
643         assertEquals("1.5", restriction.getLowerBound().toString(), CHECK_LOWER_BOUND);
644         assertTrue(restriction.isLowerBoundInclusive(), CHECK_LOWER_BOUND_INCLUSIVE);
645         assertEquals("1.6", restriction.getUpperBound().toString(), CHECK_UPPER_BOUND);
646         assertTrue(restriction.isUpperBoundInclusive(), CHECK_UPPER_BOUND_INCLUSIVE);
647 
648         // test restricting empty sets
649         range1 = VersionRange.createFromVersionSpec("[,1.1],[1.4,]");
650         range2 = VersionRange.createFromVersionSpec("[1.2,1.3]");
651         range1 = range1.restrict(range2);
652         mergedRange = range1.restrict(range2);
653         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
654         restrictions = mergedRange.getRestrictions();
655         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
656 
657         range1 = VersionRange.createFromVersionSpec("[,1.1],[1.4,]");
658         range2 = VersionRange.createFromVersionSpec("[1.2,1.3]");
659         range2 = range1.restrict(range2);
660         mergedRange = range1.restrict(range2);
661         assertNull(mergedRange.getRecommendedVersion(), CHECK_VERSION_RECOMMENDATION);
662         restrictions = mergedRange.getRestrictions();
663         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
664     }
665 
666     @Test
667     public void testReleaseRangeBoundsContainsSnapshots() throws InvalidVersionSpecificationException {
668         VersionRange range = VersionRange.createFromVersionSpec("[1.0,1.2]");
669 
670         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.1-SNAPSHOT")));
671         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.2-SNAPSHOT")));
672         assertFalse(range.containsVersion(new DefaultArtifactVersion("1.0-SNAPSHOT")));
673     }
674 
675     @Test
676     public void testSnapshotRangeBoundsCanContainSnapshots() throws InvalidVersionSpecificationException {
677         VersionRange range = VersionRange.createFromVersionSpec("[1.0,1.2-SNAPSHOT]");
678 
679         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.1-SNAPSHOT")));
680         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.2-SNAPSHOT")));
681 
682         range = VersionRange.createFromVersionSpec("[1.0-SNAPSHOT,1.2]");
683 
684         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.0-SNAPSHOT")));
685         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.1-SNAPSHOT")));
686     }
687 
688     @Test
689     public void testSnapshotSoftVersionCanContainSnapshot() throws InvalidVersionSpecificationException {
690         VersionRange range = VersionRange.createFromVersionSpec("1.0-SNAPSHOT");
691 
692         assertTrue(range.containsVersion(new DefaultArtifactVersion("1.0-SNAPSHOT")));
693     }
694 
695     private void checkInvalidRange(String version) {
696         assertThrows(
697                 InvalidVersionSpecificationException.class,
698                 () -> VersionRange.createFromVersionSpec(version),
699                 "Version " + version + " should have failed to construct");
700     }
701 
702     @Test
703     public void testContains() throws InvalidVersionSpecificationException {
704         ArtifactVersion actualVersion = new DefaultArtifactVersion("2.0.5");
705         assertTrue(enforceVersion("2.0.5", actualVersion));
706         assertTrue(enforceVersion("2.0.4", actualVersion));
707         assertTrue(enforceVersion("[2.0.5]", actualVersion));
708         assertFalse(enforceVersion("[2.0.6,)", actualVersion));
709         assertFalse(enforceVersion("[2.0.6]", actualVersion));
710         assertTrue(enforceVersion("[2.0,2.1]", actualVersion));
711         assertFalse(enforceVersion("[2.0,2.0.3]", actualVersion));
712         assertTrue(enforceVersion("[2.0,2.0.5]", actualVersion));
713         assertFalse(enforceVersion("[2.0,2.0.5)", actualVersion));
714     }
715 
716     public boolean enforceVersion(String requiredVersionRange, ArtifactVersion actualVersion)
717             throws InvalidVersionSpecificationException {
718         VersionRange vr = null;
719 
720         vr = VersionRange.createFromVersionSpec(requiredVersionRange);
721 
722         return vr.containsVersion(actualVersion);
723     }
724 
725     @Test
726     public void testOrder0() {
727         // assertTrue( new DefaultArtifactVersion( "1.0-alpha10" ).compareTo( new DefaultArtifactVersion( "1.0-alpha1" )
728         // ) > 0 );
729     }
730 
731     @Test
732     public void testCache() throws InvalidVersionSpecificationException {
733         VersionRange range = VersionRange.createFromVersionSpec("[1.0,1.2]");
734         assertSame(range, VersionRange.createFromVersionSpec("[1.0,1.2]")); // same instance from spec cache
735 
736         VersionRange spec = VersionRange.createFromVersionSpec("1.0");
737         assertSame(spec, VersionRange.createFromVersionSpec("1.0")); // same instance from spec cache
738         List<Restriction> restrictions = spec.getRestrictions();
739         assertEquals(1, restrictions.size(), CHECK_NUM_RESTRICTIONS);
740 
741         VersionRange version = VersionRange.createFromVersion("1.0");
742         assertSame(version, VersionRange.createFromVersion("1.0")); // same instance from version cache
743         restrictions = version.getRestrictions();
744         assertEquals(0, restrictions.size(), CHECK_NUM_RESTRICTIONS);
745 
746         assertFalse(
747                 spec.equals(version),
748                 "check !VersionRange.createFromVersionSpec(x).equals(VersionRange.createFromVersion(x))");
749     }
750 }