You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2007/11/16 19:34:30 UTC
svn commit: r595769 - in /tapestry/tapestry5/trunk:
tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/
tapestry-core/src/site/apt/guide/
tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/
tapestry-upload/src/test/java/org/...
Author: hlship
Date: Fri Nov 16 10:34:30 2007
New Revision: 595769
URL: http://svn.apache.org/viewvc?rev=595769&view=rev
Log:
TAPESTRY-1476: Component events for input translation and validation
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapper.java
tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/validation.apt
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapperTest.java
tapestry/tapestry5/trunk/tapestry-upload/src/test/java/org/apache/tapestry/upload/components/UploadTest.java
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapper.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapper.java?rev=595769&r1=595768&r2=595769&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapper.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapper.java Fri Nov 16 10:34:30 2007
@@ -25,13 +25,17 @@
}
/**
- * Invokes a "validate" event on the component, passing the value as context.
+ * Invokes a "validate" event on the component, passing the value as context. The
+ * delegate {@link org.apache.tapestry.FieldValidator} is invoked <em>first</em>, so
+ * the event based validation occurs only if the default validations are passed.
*
* @param value
* @throws ValidationException
*/
public void validate(Object value) throws ValidationException
{
+ _validator.validate(value);
+
try
{
_resources.triggerEvent(VALIDATE_EVENT, new Object[]{value}, null);
@@ -44,8 +48,6 @@
throw ex;
}
-
- _validator.validate(value);
}
public void render(MarkupWriter writer)
Modified: tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/validation.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/validation.apt?rev=595769&r1=595768&r2=595769&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/validation.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/validation.apt Fri Nov 16 10:34:30 2007
@@ -299,17 +299,14 @@
+---+
void onValidateFromCount(Integer value) throws ValidationException
{
- if (value == null) return;
-
if (value.equals(13)) throw new ValidationException("Thirteen is an unlucky number.");
}
+---+
- This event gets fired <<before>> the normal validators, therefore you have to watch out
- for nulls, even if the field is marked as required. It gets the <parsed> value (not the string from
+ This event gets fired <<after>> the normal validators. It is passed the <parsed> value (not the string from
the client, but the object value from the translator, or from the "parseclient" event handler).
- The method may not return value, but may throw a ValidationException to indicate a problem with
+ The method may not return a value, but may throw a ValidationException to indicate a problem with
the value.
<<Caution:>> These events are exclusively on the <server side>.
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapperTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapperTest.java?rev=595769&r1=595768&r2=595769&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapperTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/corelib/internal/ComponentValidatorWrapperTest.java Fri Nov 16 10:34:30 2007
@@ -28,7 +28,7 @@
@SuppressWarnings({"unchecked"})
@Test
- public void event_triggered_before_delegate_invoked() throws Exception
+ public void event_triggered_after_delegate_invoked() throws Exception
{
getMocksControl().checkOrder(true);
@@ -37,12 +37,14 @@
Object value = new Object();
+ fv.validate(value);
+
+
ComponentEventHandler handler = null;
expect(resources.triggerEvent(EasyMock.eq(ComponentValidatorWrapper.VALIDATE_EVENT),
EasyMock.aryEq(new Object[]{value}), EasyMock.eq(handler))).andReturn(true);
- fv.validate(value);
replay();
@@ -66,6 +68,8 @@
ComponentEventException cee = new ComponentEventException(ve.getMessage(), null, ve);
ComponentEventHandler handler = null;
+
+ fv.validate(value);
expect(resources.triggerEvent(EasyMock.eq(ComponentValidatorWrapper.VALIDATE_EVENT),
EasyMock.aryEq(new Object[]{value}), EasyMock.eq(handler))).andThrow(cee);
Modified: tapestry/tapestry5/trunk/tapestry-upload/src/test/java/org/apache/tapestry/upload/components/UploadTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-upload/src/test/java/org/apache/tapestry/upload/components/UploadTest.java?rev=595769&r1=595768&r2=595769&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-upload/src/test/java/org/apache/tapestry/upload/components/UploadTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-upload/src/test/java/org/apache/tapestry/upload/components/UploadTest.java Fri Nov 16 10:34:30 2007
@@ -20,7 +20,7 @@
import org.apache.tapestry.test.TapestryTestCase;
import org.apache.tapestry.upload.services.MultipartDecoder;
import org.apache.tapestry.upload.services.UploadedFile;
-import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.*;
import org.testng.annotations.Test;
public class UploadTest extends TapestryTestCase
@@ -151,12 +151,15 @@
FormSupport formSupport = mockFormSupport();
MultipartDecoder decoder = mockMultipartDecoder();
UploadedFile uploadedFile = mockUploadedFile();
+ ComponentResources resources = mockComponentResources();
- Upload component = new Upload(null, null, decoder, null, null);
+ Upload component = new Upload(null, null, decoder, null, resources);
expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
expect(uploadedFile.getFileName()).andReturn("foo").anyTimes();
+ train_validate(resources, uploadedFile);
+
replay();
component.processSubmission(formSupport, "test");
@@ -166,18 +169,28 @@
assertSame(component.getValue(), uploadedFile);
}
+ protected final void train_validate(ComponentResources resources, Object... context)
+ {
+ ComponentEventHandler handler = null;
+ expect(resources.triggerEvent(eq("validate"), aryEq(context), eq(handler))).andReturn(false);
+ }
+
@Test
public void process_submission_ignores_null_value() throws Exception
{
FormSupport formSupport = mockFormSupport();
MultipartDecoder decoder = mockMultipartDecoder();
UploadedFile uploadedFile = mockUploadedFile();
+ ComponentResources resources = mockComponentResources();
- Upload component = new Upload(null, null, decoder, null, null);
+ Upload component = new Upload(null, null, decoder, null, resources);
expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
expect(uploadedFile.getFileName()).andReturn("").atLeastOnce();
+
+ train_validate(resources, new Object[]{null});
+
replay();
component.processSubmission(formSupport, "test");
@@ -195,12 +208,17 @@
MultipartDecoder decoder = mockMultipartDecoder();
UploadedFile uploadedFile = mockUploadedFile();
FieldValidator<Object> validate = mockFieldValidator();
+ ComponentResources resources = mockComponentResources();
- Upload component = new Upload(null, validate, decoder, null, null);
+ Upload component = new Upload(null, validate, decoder, null, resources);
expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
expect(uploadedFile.getFileName()).andReturn("test").atLeastOnce();
+
+ train_validate(resources, uploadedFile);
+
validate.validate(uploadedFile);
+
replay();
component.processSubmission(formSupport, "test");
@@ -217,13 +235,17 @@
UploadedFile uploadedFile = mockUploadedFile();
FieldValidator<Object> validate = mockFieldValidator();
ValidationTracker tracker = mockValidationTracker();
+ ComponentResources resources = mockComponentResources();
- Upload component = new Upload(null, validate, decoder, tracker, null);
+ Upload component = new Upload(null, validate, decoder, tracker, resources);
expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
expect(uploadedFile.getFileName()).andReturn("test").atLeastOnce();
+
validate.validate(uploadedFile);
+
expectLastCall().andThrow(new ValidationException("an error"));
+
tracker.recordError(component, "an error");
replay();