Skip to content

Commit

Permalink
Improve version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Jan 24, 2025
1 parent deffef9 commit b19d3ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
27 changes: 16 additions & 11 deletions src/main/java/liquibase/ext/neo4j/database/KernelVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ public static KernelVersion parse(String version) {
major = Integer.parseInt(buffer, 10);
} else if (minor == -1) {
minor = parseMinor(buffer);
} else {
throw invalidVersion(version);
}
buffer = "";
}
if (!buffer.isEmpty()) {
if (major == -1) {
major = Integer.parseInt(buffer, 10);
} else if (minor == -1) {
minor = parseMinor(buffer);
} else {
patch = Integer.parseInt(buffer, 10);
}
if (buffer.isEmpty()) {
throw invalidVersion(version);
}
if (major == -1) {
throw new IllegalArgumentException(String.format("Invalid Neo4j version: %s", version));
major = Integer.parseInt(buffer, 10);
} else if (minor == -1) {
minor = parseMinor(buffer);
} else {
patch = Integer.parseInt(buffer, 10);
}

if (minor == -1) {
return new KernelVersion(major);
}
Expand Down Expand Up @@ -118,11 +119,15 @@ public String versionString() {
return String.format("%d.%d.%d", major, minor, patch);
}

private static int parseMinor(String buffer) {
return Integer.parseInt(buffer.replace("-aura", ""), 10);
}

private static int signum(int result) {
return (int) Math.signum(result);
}

private static int parseMinor(String buffer) {
return Integer.parseInt(buffer.replace("-aura", ""), 10);
private static IllegalArgumentException invalidVersion(String version) {
return new IllegalArgumentException(String.format("Invalid Neo4j version: %s", version));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@ class KernelVersionTest extends Specification {
KernelVersion.parse(version) == result

where:
version | result
"1.0.0" | new KernelVersion(1, 0, 0)
"1.0" | new KernelVersion(1, 0)
"5.26-aura" | new KernelVersion(5, 26)
"5" | new KernelVersion(5)
version | result
"1.0.0" | new KernelVersion(1, 0, 0)
"1.0.12" | new KernelVersion(1, 0, 12)
"1.0" | new KernelVersion(1, 0)
"5.26-aura" | new KernelVersion(5, 26)
"5" | new KernelVersion(5)
"2025.01-aura" | new KernelVersion(2025, 1)
"2025.1.2" | new KernelVersion(2025, 1, 2)
}

def "rejects invalid versions"() {
when:
KernelVersion.parse(version)

then:
thrown(exceptionType)

where:
version | exceptionType
"" | IllegalArgumentException.class
"." | NumberFormatException.class
".." | NumberFormatException.class
"5." | IllegalArgumentException.class
"2025.1." | IllegalArgumentException.class
".5.25" | NumberFormatException.class
"2025.2.1.2" | IllegalArgumentException.class
}

def "compares versions"() {
Expand Down

0 comments on commit b19d3ac

Please sign in to comment.