changeset 127:f64440e971ac

Validate thermostat versions in project wizard
author Omair Majid <omajid@redhat.com>
date Fri, 28 Mar 2014 12:52:27 -0400
parents b9f5d8a935e9
children 89b9cf5db67a
files com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/ValidatorsTest.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/Validators.java com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/messages.properties com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java
diffstat 4 files changed, 192 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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());
+    }
+
 }
--- 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;
+        }
+    }
+
 }
--- 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
--- 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);