Skip to content

Commit

Permalink
JAVACLIENT-201: Improve NuxeoVersion parse method in order to work wi…
Browse files Browse the repository at this point in the history
…th 11.x
  • Loading branch information
kevinleturc committed Jun 29, 2020
1 parent 013b5db commit e57a93f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
52 changes: 38 additions & 14 deletions nuxeo-java-client/src/main/java/org/nuxeo/client/NuxeoVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,42 @@
*/
public class NuxeoVersion {

public static final NuxeoVersion LTS_7_10 = new NuxeoVersion(7, 10, 0, false);
public static final NuxeoVersion LTS_7_10 = new NuxeoVersion(7, 10, -1, 0, false);

public static final NuxeoVersion LTS_8_10 = new NuxeoVersion(8, 10, 0, false);
public static final NuxeoVersion LTS_8_10 = new NuxeoVersion(8, 10, -1, 0, false);

public static final NuxeoVersion LTS_9_10 = new NuxeoVersion(9, 10, 0, false);
public static final NuxeoVersion LTS_9_10 = new NuxeoVersion(9, 10, -1, 0, false);

public static final NuxeoVersion LTS_10_10 = new NuxeoVersion(10, 10, 0, false);
public static final NuxeoVersion LTS_10_10 = new NuxeoVersion(10, 10, -1, 0, false);

private static final Pattern NUXEO_VERSION_PATTERN = Pattern.compile(
"(\\d+)\\.(\\d+)(?:-HF(\\d+))?(-SNAPSHOT)?(-I\\d{8}_\\d{4})?");
"(\\d+)\\.(\\d+)(?:\\.(\\d+))?(?:-HF(\\d+))?(-SNAPSHOT)?(-I\\d{8}_\\d{4})?");

private final int majorVersion;

private final int minorVersion;

/** @since 3.6.0 */
private final int buildVersion;

private final int hotfix;

private final boolean snapshot;

protected NuxeoVersion(int majorVersion, int minorVersion, int hotfix, boolean snapshot) {
protected NuxeoVersion(int majorVersion, int minorVersion, int buildVersion, int hotfix, boolean snapshot) {
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.buildVersion = buildVersion;
this.hotfix = hotfix;
this.snapshot = snapshot;
}

public String version() {
StringBuilder version = new StringBuilder();
version.append(majorVersion()).append('.').append(minorVersion());
if (buildVersion() != -1) {
version.append('.').append(buildVersion());
}
if (hotfix() != 0) {
version.append("-HF").append(String.format("%02d", hotfix()));
}
Expand All @@ -77,6 +84,14 @@ public int minorVersion() {
return minorVersion;
}

/**
* @return the build version or -1 if it is not a 11.x
* @since 3.6.0
*/
public int buildVersion() {
return buildVersion;
}

public int hotfix() {
return hotfix;
}
Expand Down Expand Up @@ -117,7 +132,7 @@ public String toString() {
* @return A new instance of {@link NuxeoVersion} with the same version + input hotfix number.
*/
public NuxeoVersion hotfix(int hotfix) {
return new NuxeoVersion(majorVersion, minorVersion, hotfix, false);
return new NuxeoVersion(majorVersion, minorVersion, 0, hotfix, false);
}

/**
Expand All @@ -138,9 +153,14 @@ public boolean isGreaterThan(NuxeoVersion version) {
// - same major and minor is greater
return true;
} else if (majorVersion == version.majorVersion() && minorVersion == version.minorVersion()) {
// Check hotfix only if major and minor are equals
// Here we assume that (X+1.Y') contains the needed fix in X.Y-HFZZ
return hotfix >= version.hotfix();
// Check build/hotfix only if major and minor are equals
if (buildVersion == -1 && version.buildVersion() == -1) {
// Here we assume that (X+1.Y') contains the needed fix in X.Y-HFZZ
return hotfix >= version.hotfix();
} else {
// Here we assume that a build version doesn't have HF
return buildVersion >= version.buildVersion();
}
}
return false;
}
Expand All @@ -153,12 +173,16 @@ public static NuxeoVersion parse(String version) {
}
int majorVersion = Integer.parseInt(matcher.group(1));
int minorVersion = Integer.parseInt(matcher.group(2));
int hotfix = 0;
int buildVersion = -1;
if (matcher.group(3) != null) {
hotfix = Integer.parseInt(matcher.group(3));
buildVersion = Integer.parseInt(matcher.group(3));
}
int hotfix = 0;
if (matcher.group(4) != null) {
hotfix = Integer.parseInt(matcher.group(4));
}
boolean snaphot = matcher.group(4) != null;
return new NuxeoVersion(majorVersion, minorVersion, hotfix, snaphot);
boolean snaphot = matcher.group(5) != null;
return new NuxeoVersion(majorVersion, minorVersion, buildVersion, hotfix, snaphot);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ public class TestNuxeoVersion {

@Test
public void testParseVersion() {
assertVersion(NuxeoVersion.parse("7.10"), 7, 10, 0, false);
assertVersion(NuxeoVersion.parse("7.10-HF05"), 7, 10, 5, false);
assertVersion(NuxeoVersion.parse("7.10-HF05-SNAPSHOT"), 7, 10, 5, true);
assertVersion(NuxeoVersion.parse("7.10-SNAPSHOT"), 7, 10, 0, true);
assertVersion(NuxeoVersion.parse("7.10-I20170818_1955"), 7, 10, 0, false);
assertVersion(NuxeoVersion.parse("7.10"), 7, 10, -1, 0, false);
assertVersion(NuxeoVersion.parse("7.10-HF05"), 7, 10, -1, 5, false);
assertVersion(NuxeoVersion.parse("7.10-HF05-SNAPSHOT"), 7, 10, -1, 5, true);
assertVersion(NuxeoVersion.parse("7.10-SNAPSHOT"), 7, 10, -1, 0, true);
assertVersion(NuxeoVersion.parse("7.10-I20170818_1955"), 7, 10, -1, 0, false);
assertVersion(NuxeoVersion.parse("11.2.2"), 11, 2, 2, 0, false);
}

@Test
public void testVersion() {
assertEquals("7.10", new NuxeoVersion(7, 10, 0, false).version());
assertEquals("7.10-HF05", new NuxeoVersion(7, 10, 5, false).version());
assertEquals("7.10-HF05-SNAPSHOT", new NuxeoVersion(7, 10, 5, true).version());
assertEquals("7.10-SNAPSHOT", new NuxeoVersion(7, 10, 0, true).version());
assertEquals("7.10", new NuxeoVersion(7, 10, -1, 0, false).version());
assertEquals("7.10-HF05", new NuxeoVersion(7, 10, -1, 5, false).version());
assertEquals("7.10-HF05-SNAPSHOT", new NuxeoVersion(7, 10, -1, 5, true).version());
assertEquals("7.10-SNAPSHOT", new NuxeoVersion(7, 10, -1, 0, true).version());
assertEquals("11.2.2", new NuxeoVersion(11, 2, 2, 0, false).version());
}

@Test
Expand All @@ -56,25 +58,32 @@ public void testGreaterThan() {
assertFalse(NuxeoVersion.LTS_7_10.isGreaterThan(NuxeoVersion.LTS_8_10));

// Compare minor
assertTrue(NuxeoVersion.LTS_7_10.isGreaterThan(new NuxeoVersion(7, 1, 0, false)));
assertFalse(new NuxeoVersion(7, 1, 0, false).isGreaterThan(NuxeoVersion.LTS_7_10));
assertTrue(NuxeoVersion.LTS_7_10.isGreaterThan(new NuxeoVersion(7, 1, -1, 0, false)));
assertFalse(new NuxeoVersion(7, 1, -1, 0, false).isGreaterThan(NuxeoVersion.LTS_7_10));

// Compare build
assertTrue(new NuxeoVersion(11, 2, 2, 0, false).isGreaterThan(new NuxeoVersion(11, 2, 0, 0, false)));
assertFalse(new NuxeoVersion(11, 2, 0, 0, false).isGreaterThan(new NuxeoVersion(11, 2, 2, 0, false)));

// Compare hotfix
assertTrue(NuxeoVersion.LTS_7_10.hotfix(5).isGreaterThan(NuxeoVersion.LTS_7_10.hotfix(5)));
assertTrue(NuxeoVersion.LTS_7_10.hotfix(5).isGreaterThan(NuxeoVersion.LTS_7_10));
assertFalse(NuxeoVersion.LTS_7_10.isGreaterThan(NuxeoVersion.LTS_7_10.hotfix(5)));

// Compare both
assertTrue(new NuxeoVersion(8, 1, 0, false).isGreaterThan(NuxeoVersion.LTS_7_10));
assertFalse(NuxeoVersion.LTS_7_10.isGreaterThan(new NuxeoVersion(8, 1, 0, false)));
assertTrue(new NuxeoVersion(8, 1, -1, 0, false).isGreaterThan(NuxeoVersion.LTS_7_10));
assertFalse(NuxeoVersion.LTS_7_10.isGreaterThan(new NuxeoVersion(8, 1, -1, 0, false)));
assertTrue(NuxeoVersion.LTS_8_10.hotfix((5)).isGreaterThan(NuxeoVersion.LTS_7_10.hotfix(10)));
assertTrue(new NuxeoVersion(11, 2, 2, 0, false).isGreaterThan(NuxeoVersion.LTS_10_10.hotfix(29)));
}

public void assertVersion(NuxeoVersion version, int majorVersion, int minorVersion, int hotfix, boolean snapshot) {
assertEquals(version.majorVersion(), majorVersion);
assertEquals(version.minorVersion(), minorVersion);
assertEquals(version.hotfix(), hotfix);
assertEquals(version.snapshot(), snapshot);
public void assertVersion(NuxeoVersion version, int majorVersion, int minorVersion, int buildVersion, int hotfix,
boolean snapshot) {
assertEquals(majorVersion, version.majorVersion());
assertEquals(minorVersion, version.minorVersion());
assertEquals(buildVersion, version.buildVersion());
assertEquals(hotfix, version.hotfix());
assertEquals(snapshot, version.snapshot());
}

}

0 comments on commit e57a93f

Please sign in to comment.