From 0aab336612f6b0e56cd85d8cd41472287d106177 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 3 Feb 2009 09:04:26 +0000 Subject: [PATCH] Fix and testcases for #2466 and #2532 - commit now throws InvalidValueException svn changeset:6710/svn branch:trunk --- src/com/itmill/toolkit/data/Buffered.java | 12 +++- .../AbstractFieldCommitWithInvalidValues.java | 69 ++++++++++++++++++ .../form/FormCommitWithInvalidValues.java | 71 +++++++++++++++++++ src/com/itmill/toolkit/ui/AbstractField.java | 35 +++++---- src/com/itmill/toolkit/ui/Form.java | 11 ++- 5 files changed, 179 insertions(+), 19 deletions(-) create mode 100644 src/com/itmill/toolkit/tests/components/abstractfield/AbstractFieldCommitWithInvalidValues.java create mode 100644 src/com/itmill/toolkit/tests/components/form/FormCommitWithInvalidValues.java diff --git a/src/com/itmill/toolkit/data/Buffered.java b/src/com/itmill/toolkit/data/Buffered.java index 8fd9b1c5be..4aba27376d 100644 --- a/src/com/itmill/toolkit/data/Buffered.java +++ b/src/com/itmill/toolkit/data/Buffered.java @@ -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 index 0000000000..524fe272f8 --- /dev/null +++ b/src/com/itmill/toolkit/tests/components/abstractfield/AbstractFieldCommitWithInvalidValues.java @@ -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 index 0000000000..77ec4b3a15 --- /dev/null +++ b/src/com/itmill/toolkit/tests/components/form/FormCommitWithInvalidValues.java @@ -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); + } + +} diff --git a/src/com/itmill/toolkit/ui/AbstractField.java b/src/com/itmill/toolkit/ui/AbstractField.java index 5dd80ab61b..c998236f32 100644 --- a/src/com/itmill/toolkit/ui/AbstractField.java +++ b/src/com/itmill/toolkit/ui/AbstractField.java @@ -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; } diff --git a/src/com/itmill/toolkit/ui/Form.java b/src/com/itmill/toolkit/ui/Form.java index ffa180746e..b31feaea68 100644 --- a/src/com/itmill/toolkit/ui/Form.java +++ b/src/com/itmill/toolkit/ui/Form.java @@ -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();) { -- 2.39.5