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.eclipse.aether.util.version;
20  
21  import java.util.Collections;
22  
23  import org.eclipse.aether.version.InvalidVersionSpecificationException;
24  import org.eclipse.aether.version.VersionRange;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.*;
28  
29  public class UnionVersionRangeTest {
30  
31      private VersionRange newRange(String range) {
32          try {
33              return new GenericVersionScheme().parseVersionRange(range);
34          } catch (InvalidVersionSpecificationException e) {
35              throw new IllegalArgumentException(e);
36          }
37      }
38  
39      private void assertBound(String version, boolean inclusive, VersionRange.Bound bound) {
40          if (version == null) {
41              assertNull(bound);
42          } else {
43              assertNotNull(bound);
44              assertNotNull(bound.getVersion());
45              assertEquals(inclusive, bound.isInclusive());
46              try {
47                  assertEquals(new GenericVersionScheme().parseVersion(version), bound.getVersion());
48              } catch (InvalidVersionSpecificationException e) {
49                  throw new IllegalArgumentException(e);
50              }
51          }
52      }
53  
54      @Test
55      public void testGetLowerBound() {
56          VersionRange range = UnionVersionRange.from(Collections.<VersionRange>emptySet());
57          assertBound(null, false, range.getLowerBound());
58  
59          range = UnionVersionRange.from(newRange("[1,2]"), newRange("[3,4]"));
60          assertBound("1", true, range.getLowerBound());
61  
62          range = UnionVersionRange.from(newRange("[1,2]"), newRange("(,4]"));
63          assertBound(null, false, range.getLowerBound());
64  
65          range = UnionVersionRange.from(newRange("[1,2]"), newRange("(1,4]"));
66          assertBound("1", true, range.getLowerBound());
67  
68          range = UnionVersionRange.from(newRange("[1,2]"), newRange("(0,4]"));
69          assertBound("0", false, range.getLowerBound());
70      }
71  
72      @Test
73      public void testGetUpperBound() {
74          VersionRange range = UnionVersionRange.from(Collections.<VersionRange>emptySet());
75          assertBound(null, false, range.getUpperBound());
76  
77          range = UnionVersionRange.from(newRange("[1,2]"), newRange("[3,4]"));
78          assertBound("4", true, range.getUpperBound());
79  
80          range = UnionVersionRange.from(newRange("[1,2]"), newRange("[3,)"));
81          assertBound(null, false, range.getUpperBound());
82  
83          range = UnionVersionRange.from(newRange("[1,2]"), newRange("[1,2)"));
84          assertBound("2", true, range.getUpperBound());
85  
86          range = UnionVersionRange.from(newRange("[1,2]"), newRange("[1,3)"));
87          assertBound("3", false, range.getUpperBound());
88      }
89  }