|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2020, Optimizely and contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * 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 com.optimizely.ab.config.audience; |
| 18 | + |
| 19 | +import com.optimizely.ab.config.audience.match.SemanticVersion; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.ExpectedException; |
| 23 | + |
| 24 | +import java.text.ParseException; |
| 25 | + |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +public class SemanticVersionTest { |
| 29 | + @Rule |
| 30 | + public ExpectedException thrown = ExpectedException.none(); |
| 31 | + |
| 32 | + @Test |
| 33 | + public void semanticVersionEmptyStringThrowsException() throws ParseException { |
| 34 | + thrown.expect(ParseException.class); |
| 35 | + // Semantic version must not be empty |
| 36 | + new SemanticVersion(""); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void semanticVersionInvalidLeadingZerosThrowsException() throws ParseException { |
| 41 | + thrown.expect(ParseException.class); |
| 42 | + // Semantic version must not contain leading zeros |
| 43 | + new SemanticVersion("03.7.1"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void semanticVersionInvalidMajorShouldBeNumberOnly() throws ParseException { |
| 48 | + thrown.expect(ParseException.class); |
| 49 | + new SemanticVersion("a.2.1"); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + public void semanticVersionInvalidMinorShouldBeNumberOnly() throws ParseException { |
| 55 | + thrown.expect(ParseException.class); |
| 56 | + new SemanticVersion("1.b.1"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void semanticVersionInvalidPatchShouldBeNumberOnly() throws ParseException { |
| 61 | + thrown.expect(ParseException.class); |
| 62 | + new SemanticVersion("1.2.c"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void semanticVersionEqualsTrue() throws ParseException { |
| 67 | + SemanticVersion targetSV = new SemanticVersion("3.7.0"); |
| 68 | + SemanticVersion actualSV = new SemanticVersion("3.7.0"); |
| 69 | + assertTrue(actualSV.equals(targetSV)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void semanticVersionEqualsWithPreReleaseAndMetaTrue() throws ParseException { |
| 74 | + SemanticVersion targetSV = new SemanticVersion("3.7.0-beta+2.3"); |
| 75 | + SemanticVersion actualSV = new SemanticVersion("3.7.0-beta+2.3"); |
| 76 | + assertTrue(actualSV.equals(targetSV)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void semanticVersionEqualsWithPreReleaseAndMetaFalse() throws ParseException { |
| 81 | + SemanticVersion targetSV = new SemanticVersion("3.7.0-beta+2.3"); |
| 82 | + SemanticVersion actualSV = new SemanticVersion("3.7.0-beta+3"); |
| 83 | + assertFalse(actualSV.equals(targetSV)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void semanticVersionEqualsSameObjectTrue() throws ParseException { |
| 88 | + SemanticVersion targetSV = new SemanticVersion("3.7.0"); |
| 89 | + assertTrue(targetSV.equals(targetSV)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void semanticVersionSame() throws ParseException { |
| 94 | + SemanticVersion targetSV = new SemanticVersion("3.7"); |
| 95 | + SemanticVersion actualSV = new SemanticVersion("3.7"); |
| 96 | + assertTrue(actualSV.equals(targetSV)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void semanticVersionEqualsComparesPreRelease() throws ParseException { |
| 101 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-beta"); |
| 102 | + SemanticVersion actualSV = new SemanticVersion("3.7.1-alpha"); |
| 103 | + assertFalse(actualSV.equals(targetSV)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void semanticVersionEqualsComparesMeta() throws ParseException { |
| 108 | + SemanticVersion targetSV = new SemanticVersion("3.7.1+beta"); |
| 109 | + SemanticVersion actualSV = new SemanticVersion("3.7.1+alpha"); |
| 110 | + assertFalse(actualSV.equals(targetSV)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void semanticVersionTargetIsNotSemanticVersionObject() throws ParseException { |
| 115 | + SemanticVersion actualSV = new SemanticVersion("3.7.0"); |
| 116 | + assertFalse(actualSV.equals(3.7)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void semanticVersionTargetToString() throws ParseException { |
| 121 | + SemanticVersion targetSV = new SemanticVersion("3.7"); |
| 122 | + assertEquals(targetSV.toString(), "3.7"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void semanticVersionTargetToStringComplete() throws ParseException { |
| 127 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-2.2.e+b.d2"); |
| 128 | + assertEquals(targetSV.toString(), "3.7.1-2.2.e+b.d2"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void semanticVersionTargetHashCode() throws ParseException { |
| 133 | + String str = "3.7.1"; |
| 134 | + SemanticVersion targetSV = new SemanticVersion("3.7.1"); |
| 135 | + assertEquals(targetSV.hashCode(), str.hashCode()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void semanticVersionCompareTo() throws ParseException { |
| 140 | + SemanticVersion targetSV = new SemanticVersion("3.7.1"); |
| 141 | + SemanticVersion actualSV = new SemanticVersion("3.7.1"); |
| 142 | + assertTrue(actualSV.compareTo(targetSV) == 0); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void semanticVersionCompareToActualLess() throws ParseException { |
| 147 | + SemanticVersion targetSV = new SemanticVersion("3.7.1"); |
| 148 | + SemanticVersion actualSV = new SemanticVersion("3.7.0"); |
| 149 | + assertTrue(actualSV.compareTo(targetSV) < 0); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void semanticVersionCompareToActualGreater() throws ParseException { |
| 154 | + SemanticVersion targetSV = new SemanticVersion("3.7.1"); |
| 155 | + SemanticVersion actualSV = new SemanticVersion("3.7.2"); |
| 156 | + assertTrue(actualSV.compareTo(targetSV) > 0); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void semanticVersionCompareToPatchMissing() throws ParseException { |
| 161 | + SemanticVersion targetSV = new SemanticVersion("3.7"); |
| 162 | + SemanticVersion actualSV = new SemanticVersion("3.7.1"); |
| 163 | + assertTrue(actualSV.compareTo(targetSV) == 0); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void semanticVersionCompareToActualPatchMissing() throws ParseException { |
| 168 | + SemanticVersion targetSV = new SemanticVersion("3.7.1"); |
| 169 | + SemanticVersion actualSV = new SemanticVersion("3.7"); |
| 170 | + assertTrue(actualSV.compareTo(targetSV) < 0); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void semanticVersionCompareToActualPreReleaseMissing() throws ParseException { |
| 175 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-beta"); |
| 176 | + SemanticVersion actualSV = new SemanticVersion("3.7.1"); |
| 177 | + assertTrue(actualSV.compareTo(targetSV) > 0); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void semanticVersionCompareToAlphaBetaAsciiComparision() throws ParseException { |
| 182 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-alpha"); |
| 183 | + SemanticVersion actualSV = new SemanticVersion("3.7.1-beta"); |
| 184 | + assertTrue(actualSV.compareTo(targetSV) > 0); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void semanticVersionCompareToIgnoreMetaComparision() throws ParseException { |
| 189 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-beta.1+2.3"); |
| 190 | + SemanticVersion actualSV = new SemanticVersion("3.7.1-beta.1+3.45"); |
| 191 | + assertTrue(actualSV.compareTo(targetSV) == 0); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void semanticVersionCompareToPreReleaseComparision() throws ParseException { |
| 196 | + SemanticVersion targetSV = new SemanticVersion("3.7.1-beta.1"); |
| 197 | + SemanticVersion actualSV = new SemanticVersion("3.7.1-beta.2"); |
| 198 | + assertTrue(actualSV.compareTo(targetSV) > 0); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void semanticVersionCompareToDoNotCompareBuildVersionComparision() throws ParseException { |
| 203 | + SemanticVersion targetSV = new SemanticVersion("3.7.1+beta.1"); |
| 204 | + SemanticVersion actualSV = new SemanticVersion("3.7.1+beta.2"); |
| 205 | + assertTrue(actualSV.compareTo(targetSV) == 0); |
| 206 | + } |
| 207 | +} |
0 commit comments