# HG changeset patch # User Omair Majid # Date 1396025547 14400 # Node ID f64440e971accaebb7f288158592ef5f4c7ea3a0 # Parent b9f5d8a935e913386038d89c4558dbf61afb4c0e Validate thermostat versions in project wizard diff -r b9f5d8a935e9 -r f64440e971ac com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java --- a/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java Fri Mar 28 11:01:04 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java Fri Mar 28 12:52:27 2014 -0400 @@ -76,4 +76,81 @@ assertTrue(result.isOK()); } + @Test + public void testVersionRequiredFailsOnEmptyInput() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result = validator.validate(""); + assertFalse(result.isOK()); + } + + @Test + public void testVersionRequiredFailsOnText() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result = validator.validate("abc"); + assertFalse(result.isOK()); + } + + @Test + public void testVersionRequiredFailsOnLeadingOrTrailingDots() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result; + + result = validator.validate(".1"); + assertFalse(result.isOK()); + + result = validator.validate("1."); + assertFalse(result.isOK()); + + result = validator.validate("1.2."); + assertFalse(result.isOK()); + } + + @Test + public void testVersionRequiredPassesOnMajorDotMinorVersion() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result; + + result = validator.validate("0.1"); + assertTrue(result.isOK()); + + result = validator.validate("1.2"); + assertTrue(result.isOK()); + + result = validator.validate("222222.123"); + assertTrue(result.isOK()); + } + + @Test + public void testVersionRequiredPassesOnMajorDotMinorDotMicroVersion() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result; + + result = validator.validate("1.0.0"); + assertTrue(result.isOK()); + + result = validator.validate("1.0.2"); + assertTrue(result.isOK()); + + result = validator.validate("1.0.2"); + assertTrue(result.isOK()); + } + + @Test + public void testVersionRequiredPassesOnMajorDotMinorDotMicroWithQualifierSuffix() { + IValidator validator = new Validators.VersionRequired(""); + IStatus result; + + result = validator.validate("1.0.0.qualifier"); + assertTrue(result.isOK()); + + result = validator.validate("1.0.0.SNAPSHOT"); + assertTrue(result.isOK()); + + result = validator.validate("1.0.2-suffix"); + assertTrue(result.isOK()); + + result = validator.validate("1.0.2-SNAPSHOT"); + assertTrue(result.isOK()); + } + } diff -r b9f5d8a935e9 -r f64440e971ac com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java Fri Mar 28 11:01:04 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java Fri Mar 28 12:52:27 2014 -0400 @@ -79,4 +79,117 @@ return ValidationStatus.ok(); } } + + /** + * Matches SemVer and OSGi-style versions + * + * @see http://semver.org/ + * @see http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/Version.html + */ + public static class VersionRequired implements IValidator { + + private String errorMessage; + + public VersionRequired(String errorMessage) { + this.errorMessage = errorMessage; + } + + @Override + public IStatus validate(Object value) { + Objects.requireNonNull(value); + if (!(value instanceof String)) { + throw new AssertionError("Expected String, but got '" + value + "' (" + value.getClass() + ")."); + } + String version = (String) value; + if (version.trim().length() == 0) { + return ValidationStatus.error(errorMessage); + } + + final int INDEX_NOT_FOUND = -1; + + String rest = version; + int endIndex; + String versionPart; + + /* major */ + endIndex = rest.indexOf('.'); + + if (endIndex == INDEX_NOT_FOUND) { + if (isValidVersion(rest)) { + return ValidationStatus.ok(); + } else { + return ValidationStatus.error(errorMessage); + } + } + + versionPart = rest.substring(0, endIndex); + if (!isValidVersion(versionPart)) { + return ValidationStatus.error(errorMessage); + } + + rest = rest.substring(endIndex+1); + + /* minor */ + endIndex = rest.indexOf('.'); + + if (endIndex == INDEX_NOT_FOUND) { + if (isValidVersion(rest)) { + return ValidationStatus.ok(); + } else { + return ValidationStatus.error(errorMessage); + } + } + + versionPart = rest.substring(0, endIndex); + if (!isValidVersion(versionPart)) { + return ValidationStatus.error(errorMessage); + } + + rest = rest.substring(endIndex+1); + + /* micro */ + int microEndIndexDot = rest.indexOf('.'); + int microEndIndexDash = rest.indexOf('-'); + + if (microEndIndexDot == INDEX_NOT_FOUND && microEndIndexDash == INDEX_NOT_FOUND) { + if (isValidVersion(rest)) { + return ValidationStatus.ok(); + } else { + return ValidationStatus.error(errorMessage); + } + } + + if (microEndIndexDot == INDEX_NOT_FOUND) { + versionPart = rest.substring(0, microEndIndexDash); + } else { + versionPart = rest.substring(0, microEndIndexDot); + } + + if (!isValidVersion(versionPart)) { + return ValidationStatus.error(errorMessage); + } + + rest = version.substring(endIndex+1); + + if (rest.length() == 0) { + return ValidationStatus.error(errorMessage); + } + + return ValidationStatus.ok(); + } + + private boolean isValidVersion(String part) { + if (part.isEmpty()) { + return false; + } + + try { + Integer.parseInt(part); + } catch (NumberFormatException nfe) { + return false; + } + return true; + } + } + } diff -r b9f5d8a935e9 -r f64440e971ac com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties Fri Mar 28 11:01:04 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties Fri Mar 28 12:52:27 2014 -0400 @@ -73,7 +73,7 @@ ThermostatProjectCreationWizardPage_artifactIdLabel=Artifact Id ThermostatProjectCreationWizardPage_groupIdLabel=Group Id ThermostatProjectCreationWizardPage_packagePrefixLabel=Package Prefix -ThermostatProjectCreationWizardPage_thermostatVersionErrorMessage=Thermostat Version must not be empty +ThermostatProjectCreationWizardPage_thermostatVersionErrorMessage=Invalid thermostat version ThermostatProjectCreationWizardPage_thermostatVersionLabel=Thermostat Version ThermostatProjectCreationWizardPage_versionErrorMessage=Version must not be empty ThermostatProjectCreationWizardPage_versionLabel=Version diff -r b9f5d8a935e9 -r f64440e971ac com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java --- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java Fri Mar 28 11:01:04 2014 -0400 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java Fri Mar 28 12:52:27 2014 -0400 @@ -50,7 +50,7 @@ new Validators.PackageNameRequired()); createLabelAndInputField(container, Messages.ThermostatProjectCreationWizardPage_thermostatVersionLabel, "thermostatVersion", //$NON-NLS-1$ - new Validators.NonEmptyStringRequired(Messages.ThermostatProjectCreationWizardPage_thermostatVersionErrorMessage)); + new Validators.VersionRequired(Messages.ThermostatProjectCreationWizardPage_thermostatVersionErrorMessage)); setControl(container);