]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix and testcases for #2466 and #2532 - commit now throws InvalidValueException
authorArtur Signell <artur.signell@itmill.com>
Tue, 3 Feb 2009 09:04:26 +0000 (09:04 +0000)
committerArtur Signell <artur.signell@itmill.com>
Tue, 3 Feb 2009 09:04:26 +0000 (09:04 +0000)
svn changeset:6710/svn branch:trunk

src/com/itmill/toolkit/data/Buffered.java
src/com/itmill/toolkit/tests/components/abstractfield/AbstractFieldCommitWithInvalidValues.java [new file with mode: 0644]
src/com/itmill/toolkit/tests/components/form/FormCommitWithInvalidValues.java [new file with mode: 0644]
src/com/itmill/toolkit/ui/AbstractField.java
src/com/itmill/toolkit/ui/Form.java

index 8fd9b1c5bed0f7835e24fa7cedb362a48de9e29e..4aba27376dad76639526ee1cd121bbb2a560ddd2 100644 (file)
@@ -4,6 +4,7 @@
 
 package com.itmill.toolkit.data;
 
+import com.itmill.toolkit.data.Validator.InvalidValueException;
 import com.itmill.toolkit.terminal.ErrorMessage;
 import com.itmill.toolkit.terminal.PaintException;
 import com.itmill.toolkit.terminal.PaintTarget;
@@ -51,8 +52,11 @@ public interface Buffered {
      * @throws SourceException
      *             if the operation fails because of an exception is thrown by
      *             the data source. The cause is included in the exception.
+     * @throws InvalidValueException
+     *             if the operation fails because validation is enabled and the
+     *             values do not validate
      */
-    public void commit() throws SourceException;
+    public void commit() throws SourceException, InvalidValueException;
 
     /**
      * Discards all changes since last commit. The object updates its value from
@@ -85,9 +89,13 @@ public interface Buffered {
      * @throws SourceException
      *             If the operation fails because of an exception is thrown by
      *             the data source.
+     * @throws InvalidValueException
+     *             If the implicit commit operation fails because of a
+     *             validation error.
      * 
      */
-    public void setWriteThrough(boolean writeThrough) throws SourceException;
+    public void setWriteThrough(boolean writeThrough) throws SourceException,
+            InvalidValueException;
 
     /**
      * Tests if the object is in read-through mode. If the object is in
diff --git a/src/com/itmill/toolkit/tests/components/abstractfield/AbstractFieldCommitWithInvalidValues.java b/src/com/itmill/toolkit/tests/components/abstractfield/AbstractFieldCommitWithInvalidValues.java
new file mode 100644 (file)
index 0000000..524fe27
--- /dev/null
@@ -0,0 +1,69 @@
+package com.itmill.toolkit.tests.components.abstractfield;
+
+import com.itmill.toolkit.data.util.ObjectProperty;
+import com.itmill.toolkit.data.validator.StringLengthValidator;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+import com.itmill.toolkit.ui.Window.Notification;
+
+public class AbstractFieldCommitWithInvalidValues extends TestBase {
+
+    private TextField tf;
+
+    @Override
+    protected String getDescription() {
+        return "Commiting a field with invalid values should throw an exception";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 2532;
+    }
+
+    @Override
+    protected void setup() {
+        tf = new TextField("A field, must contain 1-2 chars",
+                new ObjectProperty("a"));
+        tf
+                .addValidator(new StringLengthValidator("Invalid length", 1, 2,
+                        false));
+        tf.setWriteThrough(false);
+        tf.setRequired(true);
+
+        Button b = new Button("Commit", new ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                try {
+                    tf.commit();
+                    if (tf.isValid()) {
+                        getMainWindow().showNotification(
+                                "OK! Form validated and no error was thrown",
+                                Notification.TYPE_HUMANIZED_MESSAGE);
+                    } else {
+                        getMainWindow().showNotification(
+                                "Form is invalid but no exception was thrown",
+                                Notification.TYPE_ERROR_MESSAGE);
+                    }
+                } catch (Exception e) {
+                    if (tf.isValid()) {
+                        getMainWindow().showNotification(
+                                "Form is valid but an exception was thrown",
+                                Notification.TYPE_ERROR_MESSAGE);
+                    } else {
+                        getMainWindow().showNotification(
+                                "OK! Error was thrown for an invalid input",
+                                Notification.TYPE_HUMANIZED_MESSAGE);
+
+                    }
+                }
+            }
+
+        });
+
+        addComponent(tf);
+        addComponent(b);
+    }
+}
diff --git a/src/com/itmill/toolkit/tests/components/form/FormCommitWithInvalidValues.java b/src/com/itmill/toolkit/tests/components/form/FormCommitWithInvalidValues.java
new file mode 100644 (file)
index 0000000..77ec4b3
--- /dev/null
@@ -0,0 +1,71 @@
+package com.itmill.toolkit.tests.components.form;
+
+import com.itmill.toolkit.data.validator.StringLengthValidator;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Form;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+import com.itmill.toolkit.ui.Window.Notification;
+
+public class FormCommitWithInvalidValues extends TestBase {
+
+    private Form form;
+
+    @Override
+    protected String getDescription() {
+        return "Commiting a form with invalid values should throw an exception";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 2466;
+    }
+
+    @Override
+    protected void setup() {
+        form = new Form();
+        TextField tf = new TextField("A field, must contain 1-2 chars");
+        tf
+                .addValidator(new StringLengthValidator("Invalid length", 1, 2,
+                        false));
+        tf.setRequired(true);
+
+        form.addField("a", tf);
+
+        Button b = new Button("Commit", new ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                try {
+                    form.commit();
+                    if (form.isValid()) {
+                        getMainWindow().showNotification(
+                                "OK! Form validated and no error was thrown",
+                                Notification.TYPE_HUMANIZED_MESSAGE);
+                    } else {
+                        getMainWindow().showNotification(
+                                "Form is invalid but no exception was thrown",
+                                Notification.TYPE_ERROR_MESSAGE);
+                    }
+                } catch (Exception e) {
+                    if (form.isValid()) {
+                        getMainWindow().showNotification(
+                                "Form is valid but an exception was thrown",
+                                Notification.TYPE_ERROR_MESSAGE);
+                    } else {
+                        getMainWindow().showNotification(
+                                "OK! Error was thrown for an invalid input",
+                                Notification.TYPE_HUMANIZED_MESSAGE);
+
+                    }
+                }
+            }
+
+        });
+
+        addComponent(form);
+        addComponent(b);
+    }
+
+}
index 5dd80ab61ba538c2630e9f32cee136dddc59a9f9..c998236f321e54a225e6abc923de7b6390c561f4 100644 (file)
@@ -17,6 +17,7 @@ import com.itmill.toolkit.data.Buffered;
 import com.itmill.toolkit.data.Property;
 import com.itmill.toolkit.data.Validatable;
 import com.itmill.toolkit.data.Validator;
+import com.itmill.toolkit.data.Validator.InvalidValueException;
 import com.itmill.toolkit.terminal.CompositeErrorMessage;
 import com.itmill.toolkit.terminal.ErrorMessage;
 import com.itmill.toolkit.terminal.PaintException;
@@ -201,24 +202,28 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      * Saves the current value to the data source Don't add a JavaDoc comment
      * here, we use the default documentation from the implemented interface.
      */
-    public void commit() throws Buffered.SourceException {
-        if (dataSource != null && (isInvalidCommitted() || isValid())
-                && !dataSource.isReadOnly()) {
-            final Object newValue = getValue();
-            try {
+    public void commit() throws Buffered.SourceException, InvalidValueException {
+        if (dataSource != null && !dataSource.isReadOnly()) {
+            if ((isInvalidCommitted() || isValid())) {
+                final Object newValue = getValue();
+                try {
 
-                // Commits the value to datasource.
-                dataSource.setValue(newValue);
+                    // Commits the value to datasource.
+                    dataSource.setValue(newValue);
 
-            } catch (final Throwable e) {
+                } catch (final Throwable e) {
 
-                // Sets the buffering state.
-                currentBufferedSourceException = new Buffered.SourceException(
-                        this, e);
-                requestRepaint();
+                    // Sets the buffering state.
+                    currentBufferedSourceException = new Buffered.SourceException(
+                            this, e);
+                    requestRepaint();
 
-                // Throws the source exception.
-                throw currentBufferedSourceException;
+                    // Throws the source exception.
+                    throw currentBufferedSourceException;
+                }
+            } else {
+                /* An invalid value and we don't allow them, throw the exception */
+                validate();
             }
         }
 
@@ -312,7 +317,7 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      * implemented interface.
      */
     public void setWriteThrough(boolean writeTrough)
-            throws Buffered.SourceException {
+            throws Buffered.SourceException, InvalidValueException {
         if (writeTroughMode == writeTrough) {
             return;
         }
index ffa180746e837a4f83059064a63cd22eb48745fd..b31feaea6872be500dc4a942aeb37f719606190a 100644 (file)
@@ -277,10 +277,16 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item,
 
         // Only commit on valid state if so requested
         if (!isInvalidCommitted() && !isValid()) {
+            /*
+             * The values are not ok and we are told not to commit invalid
+             * values
+             */
             if (validationVisibleOnCommit) {
                 setValidationVisible(true);
             }
-            return;
+
+            // Find the first invalid value and throw the exception
+            validate();
         }
 
         // Try to commit all
@@ -417,7 +423,8 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item,
      * JavaDoc comment here, we use the default one from the interface.
      */
     @Override
-    public void setWriteThrough(boolean writeThrough) {
+    public void setWriteThrough(boolean writeThrough) throws SourceException,
+            InvalidValueException {
         if (writeThrough != this.writeThrough) {
             this.writeThrough = writeThrough;
             for (final Iterator i = propertyIds.iterator(); i.hasNext();) {