Skip to content
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

Fix version helper classes #1532

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 28 additions & 34 deletions leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public interface LwM2m {
*/
public class LwM2mVersion extends Version {

public static LwM2mVersion V1_0 = new LwM2mVersion("1.0", true);
public static LwM2mVersion V1_1 = new LwM2mVersion("1.1", true);
private static LwM2mVersion[] supportedVersions = new LwM2mVersion[] { V1_0, V1_1 };
public static final LwM2mVersion V1_0 = new LwM2mVersion("1.0", true);
public static final LwM2mVersion V1_1 = new LwM2mVersion("1.1", true);
private static final LwM2mVersion[] supportedVersions = new LwM2mVersion[] { V1_0, V1_1 };

private final boolean supported;

Expand Down Expand Up @@ -91,35 +91,35 @@ public boolean equals(Object obj) {
*/
public class Version implements Comparable<Version> {

public static Version V1_0 = new Version("1.0");
public static final Version MAX = new Version(Short.MAX_VALUE, Short.MIN_VALUE);
public static final Version V1_0 = new Version("1.0");
public static final Version MAX = new Version(Short.MAX_VALUE, Short.MAX_VALUE);

protected final Short major;
protected final Short minor;
protected final short major;
protected final short minor;

public Version(int major, int minor) {
this.major = (short) major;
this.minor = (short) minor;
this((short) major, (short) minor);
if (this.major != major) {
throw new ArithmeticException("short overflow");
}
if (this.minor != minor) {
throw new ArithmeticException("short overflow");
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
}
}

public Version(Short major, Short minor) {
public Version(short major, short minor) {
this.major = major;
this.minor = minor;
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
}

public Version(String version) {
try {
String[] versionPart = version.split("\\.");
this.major = Short.parseShort(versionPart[0]);
this.minor = Short.parseShort(versionPart[1]);
} catch (RuntimeException e) {
String err = Version.validate(version);
if (err != null) {
throw new IllegalArgumentException(err);
} else {
throw e;
}
String err = Version.validate(version);
if (err != null) {
throw new IllegalArgumentException(err);
}
String[] versionPart = version.split("\\.");
this.major = Short.parseShort(versionPart[0]);
this.minor = Short.parseShort(versionPart[1]);
}

@Override
Expand All @@ -133,16 +133,16 @@ public static Version getDefault() {

public static String validate(String version) {
if (version == null || version.isEmpty())
return "version MUST NOT be null or empty";
return "version MUST NOT be null or empty";
String[] versionPart = version.split("\\.");
if (versionPart.length != 2) {
return String.format("version (%s) MUST be composed of 2 parts", version);
}
for (int i = 0; i < 2; i++) {
try {
Short parsedShort = Short.parseShort(versionPart[i]);
short parsedShort = Short.parseShort(versionPart[i]);
if (parsedShort < 0) {
return String.format("version (%s) part %d (%s) is not a valid short", version, i + 1,
return String.format("version (%s) part %d (%s) must not be negative", version, i + 1,
versionPart[i]);
}
} catch (Exception e) {
Expand All @@ -157,8 +157,8 @@ public static String validate(String version) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((major == null) ? 0 : major.hashCode());
result = prime * result + ((minor == null) ? 0 : minor.hashCode());
result = prime * result + (int) major;
result = prime * result + (int) minor;
sbernard31 marked this conversation as resolved.
Show resolved Hide resolved
return result;
}

Expand All @@ -171,15 +171,9 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
Version other = (Version) obj;
if (major == null) {
if (other.major != null)
return false;
} else if (!major.equals(other.major))
if (major != other.major)
return false;
if (minor == null) {
if (other.minor != null)
return false;
} else if (!minor.equals(other.minor))
if (minor != other.minor)
return false;
return true;
}
Expand Down
18 changes: 18 additions & 0 deletions leshan-core/src/test/java/org/eclipse/leshan/core/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.core;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
Expand All @@ -39,5 +40,22 @@ public void compare_tests() {
assertTrue(new Version("1.2").compareTo(new Version("1.2")) == 0);
assertTrue(new Version("1.3").compareTo(new Version("1.2")) > 0);
assertTrue(new Version("2.0").compareTo(new Version("1.2")) > 0);
assertTrue(new Version("128.0").compareTo(new Version("128.2")) < 0);
assertTrue(new Version("128.0").compareTo(new Version("128.0")) == 0);
assertTrue(new Version("128.2").compareTo(new Version("128.0")) > 0);
}

@Test
public void short_overflow_tests() {
assertThrows(ArithmeticException.class, () -> new Version(Short.MIN_VALUE - 1, Short.MIN_VALUE));
assertThrows(ArithmeticException.class, () -> new Version(Short.MIN_VALUE, Short.MIN_VALUE - 1));
assertThrows(ArithmeticException.class, () -> new Version(Short.MAX_VALUE + 1, Short.MAX_VALUE));
assertThrows(ArithmeticException.class, () -> new Version(Short.MAX_VALUE, Short.MAX_VALUE + 1));
}

@Test
public void negative_number_tests() {
assertThrows(IllegalArgumentException.class, () -> new Version("-1.0"));
assertThrows(IllegalArgumentException.class, () -> new Version("1.-1"));
}
}