Skip to content

Feat: Added Semantic versioning invalid scenarios unit Tests. #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ public boolean isBuild() {
return version.contains(BUILD_SEPERATOR);
}

private int dotCount(String prefixVersion) {
char[] vCharArray = prefixVersion.toCharArray();
int count = 0;
for (char c : vCharArray) {
if (c == '.') {
count++;
}
}
return count;
}

public String[] splitSemanticVersion() throws Exception {
List<String> versionParts = new ArrayList<>();
String versionPrefix = "";
// pre-release or build.
String versionSuffix = "";
// for example: beta.2.1
Expand All @@ -103,16 +115,19 @@ public String[] splitSemanticVersion() throws Exception {
throw new Exception("Invalid Semantic Version.");
}
// major.minor.patch
String versionPrefix = partialVersionParts[0];
versionPrefix = partialVersionParts[0];

versionSuffix = partialVersionParts[1];

preVersionParts = versionPrefix.split("\\.");
} else {
preVersionParts = version.split("\\.");
versionPrefix = version;
}

if (preVersionParts.length > 3) {
preVersionParts = versionPrefix.split("\\.");

if (preVersionParts.length > 3 ||
preVersionParts.length == 0 ||
dotCount(versionPrefix) >= preVersionParts.length) {
// Throw error as pre version should only contain major.minor.patch version
throw new Exception("Invalid Semantic Version.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,84 @@ public class SemanticVersionTest {
@Rule
public ExpectedException thrown = ExpectedException.none();


@Test
public void semanticVersionInvalidOnlyDash() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("-");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidOnlyDot() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion(".");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidDoubleDot() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("..");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidPlus() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("+");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidPlusTest() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("+test");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidOnlySpace() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion(" ");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidSpaces() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("2 .3. 0");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidDotButNoMinorVersion() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("2.");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidDotButNoMajorVersion() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion(".2.1");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidComma() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion(",");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidMissingMajorMinorPatch() throws Exception {
thrown.expect(Exception.class);
SemanticVersion semanticVersion = new SemanticVersion("+build-prerelease");
semanticVersion.splitSemanticVersion();
}

@Test
public void semanticVersionInvalidMajorShouldBeNumberOnly() throws Exception {
thrown.expect(Exception.class);
Expand Down