5416749b97750f52471e8a98c01d63141f17f9b9
[controller.git] / third-party / atomix / utils / src / test / java / io / atomix / utils / VersionTest.java
1 /*
2  * Copyright 2018-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils;
17
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 /**
25  * Version test.
26  */
27 public class VersionTest {
28   @Test
29   public void testVersionComparison() {
30     assertTrue(Version.from("1.0.0").compareTo(Version.from("2.0.0")) < 0);
31     assertTrue(Version.from("2.0.0").compareTo(Version.from("1.0.0")) > 0);
32     assertTrue(Version.from("1.0.0").compareTo(Version.from("0.1.0")) > 0);
33     assertTrue(Version.from("0.1.0").compareTo(Version.from("1.0.0")) < 0);
34     assertTrue(Version.from("0.1.0").compareTo(Version.from("0.1.1")) < 0);
35     assertTrue(Version.from("1.0.0").compareTo(Version.from("0.0.1")) > 0);
36     assertTrue(Version.from("1.1.1").compareTo(Version.from("1.0.3")) > 0);
37     assertTrue(Version.from("1.0.0").compareTo(Version.from("1.0.0-beta1")) > 0);
38     assertTrue(Version.from("1.0.0-rc2").compareTo(Version.from("1.0.0-rc1")) > 0);
39     assertTrue(Version.from("1.0.0-rc1").compareTo(Version.from("1.0.0-beta1")) > 0);
40     assertTrue(Version.from("2.0.0-beta1").compareTo(Version.from("1.0.0")) > 0);
41     assertTrue(Version.from("1.0.0-alpha1").compareTo(Version.from("1.0.0-SNAPSHOT")) > 0);
42   }
43
44   @Test
45   public void testVersionToString() {
46     assertEquals("1.0.0", Version.from("1.0.0").toString());
47     assertEquals("1.0.0-alpha1", Version.from("1.0.0-alpha1").toString());
48     assertEquals("1.0.0-beta1", Version.from("1.0.0-beta1").toString());
49     assertEquals("1.0.0-rc1", Version.from("1.0.0-rc1").toString());
50     assertEquals("1.0.0-SNAPSHOT", Version.from("1.0.0-SNAPSHOT").toString());
51   }
52
53   @Test
54   public void testInvalidVersions() {
55     assertIllegalArgument(() -> Version.from("1"));
56     assertIllegalArgument(() -> Version.from("1.0"));
57     assertIllegalArgument(() -> Version.from("1.0-beta1"));
58     assertIllegalArgument(() -> Version.from("1.0.0.0"));
59     assertIllegalArgument(() -> Version.from("1.0.0.0-beta1"));
60     assertIllegalArgument(() -> Version.from("1.0.0-not1"));
61     assertIllegalArgument(() -> Version.from("1.0.0-alpha"));
62     assertIllegalArgument(() -> Version.from("1.0.0-beta"));
63     assertIllegalArgument(() -> Version.from("1.0.0-rc"));
64   }
65
66   private void assertIllegalArgument(Runnable callback) {
67     try {
68       callback.run();
69       fail();
70     } catch (IllegalArgumentException e) {
71     }
72   }
73 }