Skip to content

Commit d3dd94d

Browse files
Added semversioning additional invalid unit Tests. (#393)
Co-authored-by: msohailhussain <mirza.sohailhussain@gmail.com>
1 parent a256a2e commit d3dd94d

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersion.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,20 @@ public boolean isBuild() {
8282
return version.contains(BUILD_SEPERATOR);
8383
}
8484

85+
private int dotCount(String prefixVersion) {
86+
char[] vCharArray = prefixVersion.toCharArray();
87+
int count = 0;
88+
for (char c : vCharArray) {
89+
if (c == '.') {
90+
count++;
91+
}
92+
}
93+
return count;
94+
}
95+
8596
public String[] splitSemanticVersion() throws Exception {
8697
List<String> versionParts = new ArrayList<>();
98+
String versionPrefix = "";
8799
// pre-release or build.
88100
String versionSuffix = "";
89101
// for example: beta.2.1
@@ -103,16 +115,19 @@ public String[] splitSemanticVersion() throws Exception {
103115
throw new Exception("Invalid Semantic Version.");
104116
}
105117
// major.minor.patch
106-
String versionPrefix = partialVersionParts[0];
118+
versionPrefix = partialVersionParts[0];
107119

108120
versionSuffix = partialVersionParts[1];
109121

110-
preVersionParts = versionPrefix.split("\\.");
111122
} else {
112-
preVersionParts = version.split("\\.");
123+
versionPrefix = version;
113124
}
114125

115-
if (preVersionParts.length > 3) {
126+
preVersionParts = versionPrefix.split("\\.");
127+
128+
if (preVersionParts.length > 3 ||
129+
preVersionParts.length == 0 ||
130+
dotCount(versionPrefix) >= preVersionParts.length) {
116131
// Throw error as pre version should only contain major.minor.patch version
117132
throw new Exception("Invalid Semantic Version.");
118133
}

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,84 @@ public class SemanticVersionTest {
2727
@Rule
2828
public ExpectedException thrown = ExpectedException.none();
2929

30+
31+
@Test
32+
public void semanticVersionInvalidOnlyDash() throws Exception {
33+
thrown.expect(Exception.class);
34+
SemanticVersion semanticVersion = new SemanticVersion("-");
35+
semanticVersion.splitSemanticVersion();
36+
}
37+
38+
@Test
39+
public void semanticVersionInvalidOnlyDot() throws Exception {
40+
thrown.expect(Exception.class);
41+
SemanticVersion semanticVersion = new SemanticVersion(".");
42+
semanticVersion.splitSemanticVersion();
43+
}
44+
45+
@Test
46+
public void semanticVersionInvalidDoubleDot() throws Exception {
47+
thrown.expect(Exception.class);
48+
SemanticVersion semanticVersion = new SemanticVersion("..");
49+
semanticVersion.splitSemanticVersion();
50+
}
51+
52+
@Test
53+
public void semanticVersionInvalidPlus() throws Exception {
54+
thrown.expect(Exception.class);
55+
SemanticVersion semanticVersion = new SemanticVersion("+");
56+
semanticVersion.splitSemanticVersion();
57+
}
58+
59+
@Test
60+
public void semanticVersionInvalidPlusTest() throws Exception {
61+
thrown.expect(Exception.class);
62+
SemanticVersion semanticVersion = new SemanticVersion("+test");
63+
semanticVersion.splitSemanticVersion();
64+
}
65+
66+
@Test
67+
public void semanticVersionInvalidOnlySpace() throws Exception {
68+
thrown.expect(Exception.class);
69+
SemanticVersion semanticVersion = new SemanticVersion(" ");
70+
semanticVersion.splitSemanticVersion();
71+
}
72+
73+
@Test
74+
public void semanticVersionInvalidSpaces() throws Exception {
75+
thrown.expect(Exception.class);
76+
SemanticVersion semanticVersion = new SemanticVersion("2 .3. 0");
77+
semanticVersion.splitSemanticVersion();
78+
}
79+
80+
@Test
81+
public void semanticVersionInvalidDotButNoMinorVersion() throws Exception {
82+
thrown.expect(Exception.class);
83+
SemanticVersion semanticVersion = new SemanticVersion("2.");
84+
semanticVersion.splitSemanticVersion();
85+
}
86+
87+
@Test
88+
public void semanticVersionInvalidDotButNoMajorVersion() throws Exception {
89+
thrown.expect(Exception.class);
90+
SemanticVersion semanticVersion = new SemanticVersion(".2.1");
91+
semanticVersion.splitSemanticVersion();
92+
}
93+
94+
@Test
95+
public void semanticVersionInvalidComma() throws Exception {
96+
thrown.expect(Exception.class);
97+
SemanticVersion semanticVersion = new SemanticVersion(",");
98+
semanticVersion.splitSemanticVersion();
99+
}
100+
101+
@Test
102+
public void semanticVersionInvalidMissingMajorMinorPatch() throws Exception {
103+
thrown.expect(Exception.class);
104+
SemanticVersion semanticVersion = new SemanticVersion("+build-prerelease");
105+
semanticVersion.splitSemanticVersion();
106+
}
107+
30108
@Test
31109
public void semanticVersionInvalidMajorShouldBeNumberOnly() throws Exception {
32110
thrown.expect(Exception.class);

0 commit comments

Comments
 (0)