View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.Version.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.junit.jupiter.api.*;
26  
27  class Version_Test extends TestBase {
28  
29  	@Test void a01_basic() {
30  		assertNull(of(null));
31  		assertString("0", of(""));
32  
33  		var x = of("1.2.3");
34  		assertEquals(1, x.getMajor().orElse(null));
35  		assertEquals(2, x.getMinor().orElse(null));
36  		assertEquals(3, x.getMaintenance().orElse(null));
37  		assertEquals(1, x.getPart(0).orElse(null));
38  		assertEquals(2, x.getPart(1).orElse(null));
39  		assertEquals(3, x.getPart(2).orElse(null));
40  		assertNull(x.getPart(-1).orElse(null));
41  		assertNull(x.getPart(3).orElse(null));
42  
43  		x = of("1..x");
44  		assertString("1.0.2147483647", x);
45  	}
46  
47  	@Test void a02_isAtLeast() {
48  		var x = of("1.2.3");
49  
50  		assertTrue(x.isAtLeast(of("1.2.2")));
51  		assertTrue(x.isAtLeast(of("1.2.3")));
52  		assertFalse(x.isAtLeast(of("1.2.4")));
53  		assertTrue(x.isAtLeast(of("1.2.2"), true));
54  		assertFalse(x.isAtLeast(of("1.2.3"), true));
55  		assertFalse(x.isAtLeast(of("1.2.4"), true));
56  		assertTrue(x.isAtLeast(of("1.2")));
57  		assertFalse(x.isAtLeast(of("1.3")));
58  		assertTrue(x.isAtLeast(of("1.1.3.1")));
59  		assertFalse(x.isAtLeast(of("1.2.3.1")));
60  		assertTrue(x.isAtLeast(of("1.2.3.0")));
61  		assertFalse(x.isAtLeast(of("1.3.0.1")));
62  	}
63  
64  	@Test void a03_isAtMost() {
65  		var x = of("1.2.3");
66  
67  		assertFalse(x.isAtMost(of("1.2.2")));
68  		assertTrue(x.isAtMost(of("1.2.3")));
69  		assertTrue(x.isAtMost(of("1.2.4")));
70  		assertFalse(x.isAtMost(of("1.2.2"), true));
71  		assertFalse(x.isAtMost(of("1.2.3"), true));
72  		assertTrue(x.isAtMost(of("1.2.4"), true));
73  		assertTrue(x.isAtMost(of("1.2")));
74  		assertTrue(x.isAtMost(of("1.3")));
75  		assertFalse(x.isAtMost(of("1.1.3.1")));
76  		assertTrue(x.isAtMost(of("1.2.3.1")));
77  		assertTrue(x.isAtMost(of("1.2.3.0")));
78  		assertTrue(x.isAtMost(of("1.3.0.1")));
79  	}
80  
81  	@Test void a04_isEqualsTo() {
82  		var x = of("1.2.3");
83  
84  		assertTrue(x.equals(of("1.2.3")));
85  		assertTrue(x.equals(of("1.2")));
86  		assertTrue(x.equals(of("1.2.3.4")));
87  		assertFalse(x.equals(of("1.2.4")));
88  	}
89  
90  	@Test void a05_compareTo() {
91  		var l = list(
92  			of("1.2.3"),
93  			of("1.2"),
94  			of(""),
95  			of("1.2.3.4"),
96  			of("2.0"),
97  			of("2")
98  		);
99  		Collections.sort(l);
100 		assertList(l, "0", "1.2", "1.2.3", "1.2.3.4", "2", "2.0");
101 		Collections.reverse(l);
102 		assertList(l, "2.0", "2", "1.2.3.4", "1.2.3", "1.2", "0");
103 	}
104 }