Skip to content

Commit

Permalink
Avoid throwing pointless parsing exceptions in TeiidVersion
Browse files Browse the repository at this point in the history
* isGreaterThan and isLessThan methods do not need to parse the
  components to integers as comparing the strings achieves the
  same result

* Tests to confirm silly version components that are not readily
  parseable as integers
  • Loading branch information
Paul Richardson committed Mar 21, 2014
1 parent fe91d28 commit df52ff6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
Expand Up @@ -278,63 +278,47 @@ public ITeiidServerVersion getMaximumVersion() {

@Override
public boolean isGreaterThan(ITeiidServerVersion otherVersion) {
try {
int myMajor = Integer.parseInt(getMajor());
int otherMajor = Integer.parseInt(otherVersion.getMajor());
if (myMajor > otherMajor)
return true;

if (getMinor().equals(WILDCARD) || otherVersion.getMinor().equals(WILDCARD))
return false;

int myMinor = Integer.parseInt(getMinor());
int otherMinor = Integer.parseInt(otherVersion.getMinor());
if (((myMajor * 100) + (myMinor * 10)) > ((otherMajor * 100) + (otherMinor * 10)))
return true;

if (getMicro().equals(WILDCARD) || otherVersion.getMicro().equals(WILDCARD))
return false;

int myMicro = Integer.parseInt(getMicro());
int otherMicro = Integer.parseInt(otherVersion.getMicro());
if (((myMajor * 100) + (myMinor * 10) + myMicro) > ((otherMajor * 100) + (otherMinor * 10) + otherMicro))
return true;
}
catch (Exception ex) {
DesignerSPIPlugin.log(ex);
}
int majCompResult = getMajor().compareTo(otherVersion.getMajor());
if (majCompResult > 0)
return true;

if (getMinor().equals(WILDCARD) || otherVersion.getMinor().equals(WILDCARD))
return false;

int minCompResult = getMinor().compareTo(otherVersion.getMinor());
if (majCompResult == 0 && minCompResult > 0)
return true;

if (getMicro().equals(WILDCARD) || otherVersion.getMicro().equals(WILDCARD))
return false;

int micCompResult = getMicro().compareTo(otherVersion.getMicro());
if (majCompResult == 0 && minCompResult == 0 && micCompResult > 0)
return true;

return false;
}

@Override
public boolean isLessThan(ITeiidServerVersion otherVersion) {
try {
int myMajor = Integer.parseInt(getMajor());
int otherMajor = Integer.parseInt(otherVersion.getMajor());
if (myMajor < otherMajor)
return true;

if (getMinor().equals(WILDCARD) || otherVersion.getMinor().equals(WILDCARD))
return false;

int myMinor = Integer.parseInt(getMinor());
int otherMinor = Integer.parseInt(otherVersion.getMinor());
if (((myMajor * 100) + (myMinor * 10)) < ((otherMajor * 100) + (otherMinor * 10)))
return true;

if (getMicro().equals(WILDCARD) || otherVersion.getMicro().equals(WILDCARD))
return false;

int myMicro = Integer.parseInt(getMicro());
int otherMicro = Integer.parseInt(otherVersion.getMicro());
if (((myMajor * 100) + (myMinor * 10) + myMicro) < ((otherMajor * 100) + (otherMinor * 10) + otherMicro))
return true;
}
catch (Exception ex) {
DesignerSPIPlugin.log(ex);
}
int majCompResult = getMajor().compareTo(otherVersion.getMajor());
if (majCompResult < 0)
return true;

if (getMinor().equals(WILDCARD) || otherVersion.getMinor().equals(WILDCARD))
return false;

int minCompResult = getMinor().compareTo(otherVersion.getMinor());
if (majCompResult == 0 && minCompResult < 0)
return true;

if (getMicro().equals(WILDCARD) || otherVersion.getMicro().equals(WILDCARD))
return false;

int micCompResult = getMicro().compareTo(otherVersion.getMicro());
if (majCompResult == 0 && minCompResult == 0 && micCompResult < 0)
return true;

return false;
}

Expand Down
Expand Up @@ -107,6 +107,19 @@ public void testIsGreaterThan() {

assertFalse(version("8.x.0").isGreaterThan(version("8.0.0"))); //$NON-NLS-1$ //$NON-NLS-2$
assertFalse(version("8.0.0").isGreaterThan(version("8.x.0"))); //$NON-NLS-1$ //$NON-NLS-2$

// silly micro version should be ignored since minor versions should be enough for the comparison
assertTrue(version("8.1.extendedmicroversionid").isGreaterThan(version("8.0.0"))); //$NON-NLS-1$//$NON-NLS-2$

// same minor versions up until 1 and 2
assertTrue(version("8.designer-2.0").isGreaterThan(version("8.designer-1.0"))); //$NON-NLS-1$//$NON-NLS-2$

// Comparing 1 and 10
assertTrue(version("8.designer-10.0").isGreaterThan(version("8.designer-1.0"))); //$NON-NLS-1$//$NON-NLS-2$

// 20 < 18 but designer > teiidteiid
assertTrue(version("8.teiidteiid-18.0").isGreaterThan(version("8.designer-20.0"))); //$NON-NLS-1$//$NON-NLS-2$
assertFalse(version("8.designer-20.0").isGreaterThan(version("8.teiidteiid-18.0"))); //$NON-NLS-1$//$NON-NLS-2$
}

/**
Expand Down Expand Up @@ -153,5 +166,18 @@ public void testIsLessThan() {

assertFalse(version("8.0.0").isLessThan(version("8.x.0"))); //$NON-NLS-1$ //$NON-NLS-2$
assertFalse(version("8.x.0").isLessThan(version("8.0.0"))); //$NON-NLS-1$ //$NON-NLS-2$

// silly micro version should be ignored since minor versions should be enough for the comparison
assertTrue(version("8.0.0").isLessThan(version("8.1.extendedmicroversionid"))); //$NON-NLS-1$//$NON-NLS-2$

// same minor versions up until 1 and 2
assertTrue(version("8.designer-1.0").isLessThan(version("8.designer-2.0"))); //$NON-NLS-1$//$NON-NLS-2$

// Comparing 1 and 10
assertTrue(version("8.designer-1.0").isLessThan(version("8.designer-10.0"))); //$NON-NLS-1$//$NON-NLS-2$

// 20 > 18 but designer < teiidteiid
assertTrue(version("8.designer-20.0").isLessThan(version("8.teiidteiid-18.0"))); //$NON-NLS-1$//$NON-NLS-2$
assertFalse(version("8.teiidteiid-18.0").isLessThan(version("8.designer-20.0"))); //$NON-NLS-1$//$NON-NLS-2$
}
}

0 comments on commit df52ff6

Please sign in to comment.