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