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 org.eclipse.aether.version.InvalidVersionSpecificationException;
22  import org.eclipse.aether.version.VersionConstraint;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.*;
27  
28  /**
29   */
30  public class GenericVersionSchemeTest {
31  
32      private GenericVersionScheme scheme;
33  
34      @BeforeEach
35      void setUp() {
36          scheme = new GenericVersionScheme();
37      }
38  
39      private InvalidVersionSpecificationException parseInvalid(String constraint) {
40          try {
41              scheme.parseVersionConstraint(constraint);
42              fail("expected exception for constraint " + constraint);
43              return null;
44          } catch (InvalidVersionSpecificationException e) {
45              return e;
46          }
47      }
48  
49      @Test
50      void testEnumeratedVersions() throws InvalidVersionSpecificationException {
51          VersionConstraint c = scheme.parseVersionConstraint("1.0");
52          assertEquals("1.0", c.getVersion().toString());
53          assertTrue(c.containsVersion(new GenericVersion("1.0")));
54  
55          c = scheme.parseVersionConstraint("[1.0]");
56          assertNull(c.getVersion());
57          assertTrue(c.containsVersion(new GenericVersion("1.0")));
58  
59          c = scheme.parseVersionConstraint("[1.0],[2.0]");
60          assertTrue(c.containsVersion(new GenericVersion("1.0")));
61          assertTrue(c.containsVersion(new GenericVersion("2.0")));
62  
63          c = scheme.parseVersionConstraint("[1.0],[2.0],[3.0]");
64          assertContains(c, "1.0", "2.0", "3.0");
65          assertNotContains(c, "1.5");
66  
67          c = scheme.parseVersionConstraint("[1,3),(3,5)");
68          assertContains(c, "1", "2", "4");
69          assertNotContains(c, "3", "5");
70  
71          c = scheme.parseVersionConstraint("[1,3),(3,)");
72          assertContains(c, "1", "2", "4");
73          assertNotContains(c, "3");
74      }
75  
76      private void assertNotContains(VersionConstraint c, String... versions) {
77          assertContains(String.format("%s: %%s should not be contained\n", c.toString()), c, false, versions);
78      }
79  
80      private void assertContains(String msg, VersionConstraint c, boolean b, String... versions) {
81          for (String v : versions) {
82              assertEquals(b, c.containsVersion(new GenericVersion(v)), String.format(msg, v));
83          }
84      }
85  
86      private void assertContains(VersionConstraint c, String... versions) {
87          assertContains(String.format("%s: %%s should be contained\n", c.toString()), c, true, versions);
88      }
89  
90      @Test
91      void testInvalid() {
92          parseInvalid("[1,");
93          parseInvalid("[1,2],(3,");
94          parseInvalid("[1,2],3");
95      }
96  
97      @Test
98      void testSameUpperAndLowerBound() throws InvalidVersionSpecificationException {
99          VersionConstraint c = scheme.parseVersionConstraint("[1.0]");
100         assertEquals("[1.0,1.0]", c.toString());
101         VersionConstraint c2 = scheme.parseVersionConstraint(c.toString());
102         assertEquals(c, c2);
103         assertTrue(c.containsVersion(new GenericVersion("1.0")));
104     }
105 }