Skip to content

Commit f30e088

Browse files
author
Mike Davis
committed
Add and refactor tests.
1 parent ee83421 commit f30e088

File tree

2 files changed

+94
-97
lines changed

2 files changed

+94
-97
lines changed

core-api/src/test/java/com/optimizely/ab/config/audience/SemanticVersionTest.java

Lines changed: 19 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.optimizely.ab.config.audience;
1818

1919
import com.optimizely.ab.config.audience.match.SemanticVersion;
20+
import com.optimizely.ab.config.audience.match.UnexpectedValueTypeException;
2021
import org.junit.Rule;
2122
import org.junit.Test;
2223
import org.junit.rules.ExpectedException;
@@ -141,108 +142,29 @@ public void semanticVersionInvalidShouldBeOfSizeLessThan3() throws Exception {
141142
}
142143

143144
@Test
144-
public void semanticVersionCompareTo() throws Exception {
145-
SemanticVersion targetSV = new SemanticVersion("3.7.1");
146-
SemanticVersion actualSV = new SemanticVersion("3.7.1");
147-
assertTrue(actualSV.compare(targetSV) == 0);
145+
public void testEquals() throws Exception {
146+
assertEquals(0, SemanticVersion.compare("3.7.1", "3.7.1"));
147+
assertEquals(0, SemanticVersion.compare("3.7.1", "3.7"));
148+
assertEquals(0, SemanticVersion.compare("2.1.3+build", "2.1.3"));
149+
assertEquals(0, SemanticVersion.compare("3.7.1-beta.1+2.3", "3.7.1-beta.1+2.3"));
148150
}
149151

150152
@Test
151-
public void semanticVersionCompareToActualLess() throws Exception {
152-
SemanticVersion targetSV = new SemanticVersion("3.7.1");
153-
SemanticVersion actualSV = new SemanticVersion("3.7.0");
154-
assertTrue(actualSV.compare(targetSV) < 0);
153+
public void testLessThan() throws Exception {
154+
assertTrue(SemanticVersion.compare("3.7.0", "3.7.1") < 0);
155+
assertTrue(SemanticVersion.compare("3.7", "3.7.1") < 0);
156+
assertTrue(SemanticVersion.compare("2.1.3-beta+1", "2.1.3-beta+1.2.3") < 0);
157+
assertTrue(SemanticVersion.compare("2.1.3-beta-1", "2.1.3-beta-1.2.3") < 0);
155158
}
156159

157160
@Test
158-
public void semanticVersionCompareToActualGreater() throws Exception {
159-
SemanticVersion targetSV = new SemanticVersion("3.7.1");
160-
SemanticVersion actualSV = new SemanticVersion("3.7.2");
161-
assertTrue(actualSV.compare(targetSV) > 0);
162-
}
163-
164-
@Test
165-
public void semanticVersionCompareToPatchMissing() throws Exception {
166-
SemanticVersion targetSV = new SemanticVersion("3.7");
167-
SemanticVersion actualSV = new SemanticVersion("3.7.1");
168-
assertTrue(actualSV.compare(targetSV) == 0);
169-
}
170-
171-
@Test
172-
public void semanticVersionCompareToActualPatchMissing() throws Exception {
173-
SemanticVersion targetSV = new SemanticVersion("3.7.1");
174-
SemanticVersion actualSV = new SemanticVersion("3.7");
175-
assertTrue(actualSV.compare(targetSV) < 0);
176-
}
177-
178-
@Test
179-
public void semanticVersionCompareToActualPreReleaseMissing() throws Exception {
180-
SemanticVersion targetSV = new SemanticVersion("3.7.1-beta");
181-
SemanticVersion actualSV = new SemanticVersion("3.7.1");
182-
assertTrue(actualSV.compare(targetSV) > 0);
183-
}
184-
185-
@Test
186-
public void semanticVersionCompareTargetBetaComplex() throws Exception {
187-
SemanticVersion targetSV = new SemanticVersion("2.1.3-beta+1");
188-
SemanticVersion actualSV = new SemanticVersion("2.1.3-beta+1.2.3");
189-
assertTrue(actualSV.compare(targetSV) > 0);
190-
}
191-
192-
@Test
193-
public void semanticVersionCompareTargetBuildIgnores() throws Exception {
194-
SemanticVersion targetSV = new SemanticVersion("2.1.3");
195-
SemanticVersion actualSV = new SemanticVersion("2.1.3+build");
196-
assertTrue(actualSV.compare(targetSV) == 0);
197-
}
198-
199-
@Test
200-
public void semanticVersionCompareTargetBuildComplex() throws Exception {
201-
SemanticVersion targetSV = new SemanticVersion("2.1.3-beta+1.2.3");
202-
SemanticVersion actualSV = new SemanticVersion("2.1.3-beta+1");
203-
assertTrue(actualSV.compare(targetSV) < 0);
204-
}
205-
206-
@Test
207-
public void semanticVersionCompareMultipleDash() throws Exception {
208-
SemanticVersion targetSV = new SemanticVersion("2.1.3-beta-1.2.3");
209-
SemanticVersion actualSV = new SemanticVersion("2.1.3-beta-1");
210-
assertTrue(actualSV.compare(targetSV) < 0);
211-
}
212-
213-
@Test
214-
public void semanticVersionCompareToAlphaBetaAsciiComparision() throws Exception {
215-
SemanticVersion targetSV = new SemanticVersion("3.7.1-alpha");
216-
SemanticVersion actualSV = new SemanticVersion("3.7.1-beta");
217-
assertTrue(actualSV.compare(targetSV) > 0);
218-
}
219-
220-
@Test
221-
public void semanticVersionComparePrereleaseSmallerThanBuild() throws Exception {
222-
SemanticVersion targetSV = new SemanticVersion("3.7.1-prerelease");
223-
SemanticVersion actualSV = new SemanticVersion("3.7.1+build");
224-
assertTrue(actualSV.compare(targetSV) > 0);
225-
}
226-
227-
228-
@Test
229-
public void semanticVersionCompareAgainstPreReleaseToPreRelease() throws Exception {
230-
SemanticVersion targetSV = new SemanticVersion("3.7.1-prerelease+build");
231-
SemanticVersion actualSV = new SemanticVersion("3.7.1-prerelease-prerelease+rc");
232-
assertTrue(actualSV.compare(targetSV) > 0);
233-
}
234-
235-
@Test
236-
public void semanticVersionCompareToIgnoreMetaComparision() throws Exception {
237-
SemanticVersion targetSV = new SemanticVersion("3.7.1-beta.1+2.3");
238-
SemanticVersion actualSV = new SemanticVersion("3.7.1-beta.1+2.3");
239-
assertTrue(actualSV.compare(targetSV) == 0);
240-
}
241-
242-
@Test
243-
public void semanticVersionCompareToPreReleaseComparision() throws Exception {
244-
SemanticVersion targetSV = new SemanticVersion("3.7.1-beta.1");
245-
SemanticVersion actualSV = new SemanticVersion("3.7.1-beta.2");
246-
assertTrue(actualSV.compare(targetSV) > 0);
161+
public void testGreaterThan() throws Exception {
162+
assertTrue(SemanticVersion.compare("3.7.2", "3.7.1") > 0);
163+
assertTrue(SemanticVersion.compare("3.7.1", "3.7.1-beta") > 0);
164+
assertTrue(SemanticVersion.compare("2.1.3-beta+1.2.3", "2.1.3-beta+1") > 0);
165+
assertTrue(SemanticVersion.compare("3.7.1-beta", "3.7.1-alpha") > 0);
166+
assertTrue(SemanticVersion.compare("3.7.1+build", "3.7.1-prerelease") > 0);
167+
assertTrue(SemanticVersion.compare("3.7.1-prerelease-prerelease+rc", "3.7.1-prerelease+build") > 0);
168+
assertTrue(SemanticVersion.compare("3.7.1-beta.2", "3.7.1-beta.1") > 0);
247169
}
248170
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.match;
18+
19+
import org.junit.Test;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
25+
import static org.junit.Assert.*;
26+
27+
public class NumberComparatorTest {
28+
29+
private static final List<Object> INVALIDS = Collections.unmodifiableList(Arrays.asList(null, "test", "", true));
30+
31+
@Test
32+
public void testLessThan() throws UnknownValueTypeException {
33+
assertTrue(NumberComparator.compare(0,1) < 0);
34+
assertTrue(NumberComparator.compare(0,1.0) < 0);
35+
assertTrue(NumberComparator.compare(0,1L) < 0);
36+
}
37+
38+
@Test
39+
public void testGreaterThan() throws UnknownValueTypeException {
40+
assertTrue(NumberComparator.compare(1,0) > 0);
41+
assertTrue(NumberComparator.compare(1.0,0) > 0);
42+
assertTrue(NumberComparator.compare(1L,0) > 0);
43+
}
44+
45+
@Test
46+
public void testEquals() throws UnknownValueTypeException {
47+
assertEquals(0, NumberComparator.compare(1, 1));
48+
assertEquals(0, NumberComparator.compare(1, 1.0));
49+
assertEquals(0, NumberComparator.compare(1L, 1));
50+
}
51+
52+
@Test
53+
public void testInvalidRight() {
54+
for (Object invalid: INVALIDS) {
55+
try {
56+
NumberComparator.compare(0, invalid);
57+
fail("should have failed for invalid object");
58+
} catch (UnknownValueTypeException e) {
59+
// pass
60+
}
61+
}
62+
}
63+
64+
@Test
65+
public void testInvalidLeft() {
66+
for (Object invalid: INVALIDS) {
67+
try {
68+
NumberComparator.compare(invalid, 0);
69+
fail("should have failed for invalid object");
70+
} catch (UnknownValueTypeException e) {
71+
// pass
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)