Platform: build numbers: .* is treated identically as .SNAPSHOT, '2016' is parsed...
[idea/community.git] / platform / util / testSrc / com / intellij / openapi / util / BuildNumberTest.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
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 com.intellij.openapi.util;
17
18 import org.junit.Test;
19
20 import static org.junit.Assert.*;
21
22 public class BuildNumberTest {
23   @Test
24   public void historicBuild() {
25     assertEquals(new BuildNumber("", BuildNumber.Format.HISTORIC, 75, 7512), BuildNumber.fromString("7512"));
26     assertEquals("75.7512", BuildNumber.fromString("7512").asString());
27   }
28
29   @Test
30   public void branchBasedBuild() throws Exception {
31     assertParsed(BuildNumber.fromString("145"), 145, 0, BuildNumber.Format.BRANCH_BASED, "145.0");
32     assertParsed(BuildNumber.fromString("145.1"), 145, 1, BuildNumber.Format.BRANCH_BASED, "145.1");
33     assertParsed(BuildNumber.fromString("145.1.2"), 145, 1, BuildNumber.Format.BRANCH_BASED, "145.1.2");
34     assertParsed(BuildNumber.fromString("IU-145.1.2"), 145, 1, BuildNumber.Format.BRANCH_BASED, "IU-145.1.2");
35     assertParsed(BuildNumber.fromString("IU-145.SNAPSHOT"), 145, BuildNumber.SNAPSHOT_VALUE, BuildNumber.Format.BRANCH_BASED, "IU-145.SNAPSHOT");
36     assertParsed(BuildNumber.fromString("IU-145.1.SNAPSHOT"), 145, 1, BuildNumber.Format.BRANCH_BASED, "IU-145.1.SNAPSHOT");
37   }
38
39   @Test
40   public void yearBasedBuild() throws Exception {
41     assertParsed(BuildNumber.fromString("2016"), 20160, -1, BuildNumber.Format.YEAR_BASED, "2016.0");
42     assertParsed(BuildNumber.fromString("2016.1"), 20161, -1, BuildNumber.Format.YEAR_BASED, "2016.1");
43     assertParsed(BuildNumber.fromString("2016.1.2"), 20161, -1, BuildNumber.Format.YEAR_BASED, "2016.1.2");
44     assertParsed(BuildNumber.fromString("IU-2016.1.2"), 20161, -1, BuildNumber.Format.YEAR_BASED, "IU-2016.1.2");
45     assertParsed(BuildNumber.fromString("2016.1.2.3"), 20161, -1, BuildNumber.Format.YEAR_BASED, "2016.1.2.3");
46     assertParsed(BuildNumber.fromString("IU-2016.1.2.3"), 20161, -1, BuildNumber.Format.YEAR_BASED, "IU-2016.1.2.3");
47     assertParsed(BuildNumber.fromString("2016.1.2.3.4"), 20161, -1, BuildNumber.Format.YEAR_BASED, "2016.1.2.3.4");
48     assertParsed(BuildNumber.fromString("IU-2016.1.2.3.4"), 20161, -1, BuildNumber.Format.YEAR_BASED, "IU-2016.1.2.3.4");
49     
50     assertParsed(BuildNumber.fromString("IU-2016.1.SNAPSHOT"), 20161, -1, BuildNumber.Format.YEAR_BASED, "IU-2016.1.SNAPSHOT");
51     assertParsed(BuildNumber.fromString("IU-2016.1.SNAPSHOT.1"), 20161, -1, BuildNumber.Format.YEAR_BASED, "IU-2016.1.SNAPSHOT.1");
52   }
53   
54   private static void assertParsed(BuildNumber n, int expectedBaseline, int expectedBuildNumber, BuildNumber.Format based, String asString) {
55     assertEquals(expectedBaseline, n.getBaselineVersion());
56     assertEquals(expectedBuildNumber, n.getBuildNumber());
57     assertEquals(asString, n.asString());
58     assertEquals(based, n.getFormat());
59   }
60
61   @Test
62   public void comparingVersion() throws Exception {
63     assertTrue(BuildNumber.fromString("2016").compareTo(BuildNumber.fromString("2016.1")) < 0);
64     assertTrue(BuildNumber.fromString("2015.1").compareTo(BuildNumber.fromString("2016")) < 0);
65     assertTrue(BuildNumber.fromString("2016.0").compareTo(BuildNumber.fromString("2016.0")) == 0);
66     
67     assertTrue(BuildNumber.fromString("2016.1").compareTo(BuildNumber.fromString("2016.1")) == 0);
68     assertTrue(BuildNumber.fromString("2016.1.1").compareTo(BuildNumber.fromString("2016.1.1")) == 0);
69     assertTrue(BuildNumber.fromString("2016.1.1.1").compareTo(BuildNumber.fromString("2016.1.1.1")) == 0);
70     
71     assertTrue(BuildNumber.fromString("2016.1").compareTo(BuildNumber.fromString("2016.1.1")) < 0);
72     assertTrue(BuildNumber.fromString("2016.1").compareTo(BuildNumber.fromString("2016.1.1.1")) < 0);
73     assertTrue(BuildNumber.fromString("2016.1.1").compareTo(BuildNumber.fromString("2016.1.1.1")) < 0);
74
75     assertTrue(BuildNumber.fromString("2016.1").compareTo(BuildNumber.fromString("2016.2")) < 0);
76     assertTrue(BuildNumber.fromString("2016.1").compareTo(BuildNumber.fromString("2016.2.1")) < 0);
77     assertTrue(BuildNumber.fromString("2016.1.1").compareTo(BuildNumber.fromString("2016.2")) < 0);
78     assertTrue(BuildNumber.fromString("2016.1.1").compareTo(BuildNumber.fromString("2016.2.1")) < 0);
79
80     assertTrue(BuildNumber.fromString("146.1").compareTo(BuildNumber.fromString("2016.1")) < 0);
81     assertTrue(BuildNumber.fromString("146.9.9").compareTo(BuildNumber.fromString("2016.1")) < 0);
82     
83     assertTrue(BuildNumber.fromString("2016.2").compareTo(BuildNumber.fromString("2016.2.*")) < 0);
84     assertTrue(BuildNumber.fromString("2016.2").compareTo(BuildNumber.fromString("2016.3.*")) < 0);
85     assertTrue(BuildNumber.fromString("2016.2").compareTo(BuildNumber.fromString("2016.1.*")) > 0);
86     assertTrue(BuildNumber.fromString("2016.2.2.2.2").compareTo(BuildNumber.fromString("2016.2.*")) < 0);
87     assertTrue(BuildNumber.fromString("2016.2.*").compareTo(BuildNumber.fromString("2016.2.2.2.2")) > 0);
88
89     assertTrue(BuildNumber.fromString("145.1").compareTo(BuildNumber.fromString("145.*")) < 0);
90     assertTrue(BuildNumber.fromString("145.1.1").compareTo(BuildNumber.fromString("145.*")) < 0);
91     assertTrue(BuildNumber.fromString("145.1").compareTo(BuildNumber.fromString("146.*")) < 0);
92     assertTrue(BuildNumber.fromString("145.1").compareTo(BuildNumber.fromString("144.*")) > 0);
93
94     assertTrue(BuildNumber.fromString("145.SNAPSHOT").compareTo(BuildNumber.fromString("145.*")) == 0);
95     assertTrue(BuildNumber.fromString("145.*").compareTo(BuildNumber.fromString("145.SNAPSHOT")) == 0);
96
97     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.*")) == 0);
98     assertTrue(BuildNumber.fromString("2016.1.*").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT")) == 0);
99     
100     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.*")) < 0);
101     assertTrue(BuildNumber.fromString("2016.*").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT")) > 0);
102
103     assertTrue(BuildNumber.fromString("2016.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.*")) > 0);
104     assertTrue(BuildNumber.fromString("2016.1.*").compareTo(BuildNumber.fromString("2016.SNAPSHOT")) < 0);
105   }
106
107   @Test
108   public void isSnapshot() {
109     assertTrue(BuildNumber.fromString("SNAPSHOT").isSnapshot());
110     assertTrue(BuildNumber.fromString("__BUILD_NUMBER__").isSnapshot());
111     assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").isSnapshot());
112     
113     assertTrue(BuildNumber.fromString("IC-90.*").isSnapshot());
114     assertFalse(BuildNumber.fromString("90.9999999").isSnapshot());
115     
116     assertFalse(BuildNumber.fromString("2016.1").isSnapshot());
117     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT").isSnapshot());
118     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT.1").isSnapshot());
119   }
120
121   @Test
122   public void devSnapshotVersion() throws Exception {
123     BuildNumber b = BuildNumber.fromString("__BUILD_NUMBER__");
124     assertTrue(b.asString(), b.getBaselineVersion() >= 2016);
125     assertEquals(b.asString(), -1, b.getBuildNumber());
126     assertTrue(b.isSnapshot());
127     
128     assertEquals(BuildNumber.fromString("__BUILD_NUMBER__"), BuildNumber.fromString("SNAPSHOT"));
129   }
130
131   @Test
132   public void snapshotDomination() {
133     assertTrue(BuildNumber.fromString("90.SNAPSHOT").compareTo(BuildNumber.fromString("90.12345")) > 0);
134     assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-90.12345")) > 0);
135     assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-100.12345")) < 0);
136     assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-100.SNAPSHOT")) < 0);
137     assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-90.SNAPSHOT")) == 0);
138     
139     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.1")) > 0);
140     assertTrue(BuildNumber.fromString("2016.1.1").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT")) < 0);
141     
142     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT")) == 0);
143     
144     assertTrue(BuildNumber.fromString("2016.1.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.1.1")) > 0);
145     assertTrue(BuildNumber.fromString("2016.1.1.1").compareTo(BuildNumber.fromString("2016.1.1.SNAPSHOT")) < 0);
146     
147     assertTrue(BuildNumber.fromString("2016.1.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.1.SNAPSHOT")) == 0);
148     
149     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT.1").compareTo(BuildNumber.fromString("2016.1.1.1")) > 0);
150     assertTrue(BuildNumber.fromString("2016.1.1.1").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT.1")) < 0);
151     
152     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT.1").compareTo(BuildNumber.fromString("2016.1.1.SNAPSHOT")) > 0);
153     assertTrue(BuildNumber.fromString("2016.1.1.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT.1")) < 0);
154     
155     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT.1").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT.SNAPSHOT")) == 0);
156     assertTrue(BuildNumber.fromString("2016.1.SNAPSHOT.SNAPSHOT").compareTo(BuildNumber.fromString("2016.1.SNAPSHOT.1")) == 0);
157   }
158
159   @Test
160   public void fallbackVersion() throws Exception {
161     assertParsed(BuildNumber.fallback(), 29991, -1, BuildNumber.Format.YEAR_BASED, "2999.1.SNAPSHOT");
162     assertEquals(BuildNumber.Format.YEAR_BASED, BuildNumber.fallback().getFormat());
163     assertTrue(BuildNumber.fallback().isSnapshot());
164     
165     assertTrue(BuildNumber.fallback().compareTo(BuildNumber.fromString("7512")) > 0);
166     assertTrue(BuildNumber.fallback().compareTo(BuildNumber.fromString("145")) > 0);
167     assertTrue(BuildNumber.fallback().compareTo(BuildNumber.fromString("145.12")) > 0);
168     assertTrue(BuildNumber.fallback().compareTo(BuildNumber.fromString("2016.1")) > 0);
169     assertTrue(BuildNumber.fallback().compareTo(BuildNumber.fromString("2016.1.2")) > 0);
170   }
171 }