Author: drobiazko
Date: Wed Oct  6 15:57:25 2010
New Revision: 1005093

URL: http://svn.apache.org/viewvc?rev=1005093&view=rev
Log:
TAP5-1294: Validation macros do not work when used in @Validate annotation

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ValidatorMacroDemo.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java
   (with props)
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImpl.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImplTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImpl.java?rev=1005093&r1=1005092&r2=1005093&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImpl.java
 Wed Oct  6 15:57:25 2010
@@ -77,6 +77,26 @@ public class FieldValidatorSourceImpl im
     public FieldValidator createValidator(Field field, String validatorType, 
String constraintValue, String overrideId,
             Messages overrideMessages, Locale locale)
     {
+       
+       ValidatorSpecification originalSpec = new 
ValidatorSpecification(validatorType, constraintValue);
+       
+       List<ValidatorSpecification> specs = 
expandMacros(newList(originalSpec));
+       
+       List<FieldValidator> fieldValidators = CollectionFactory.newList();
+       
+       for (ValidatorSpecification spec : specs) {
+                       fieldValidators.add(createValidator(field, spec, 
overrideId, overrideMessages, locale));
+               }
+       
+       return new CompositeFieldValidator(fieldValidators);
+    }
+    
+    private FieldValidator createValidator(Field field, ValidatorSpecification 
spec, String overrideId,
+            Messages overrideMessages, Locale locale)
+    {
+       
+       String validatorType = spec.getValidatorType();
+       
         assert InternalUtils.isNonBlank(validatorType);
         Validator validator = validators.get(validatorType);
 
@@ -89,7 +109,7 @@ public class FieldValidatorSourceImpl im
 
         String formValidationid = formSupport.getFormValidationId();
 
-        Object coercedConstraintValue = computeConstraintValue(validatorType, 
validator, constraintValue,
+        Object coercedConstraintValue = computeConstraintValue(validatorType, 
validator, spec.getConstraintValue(),
                 formValidationid, overrideId, overrideMessages);
 
         MessageFormatter formatter = findMessageFormatter(formValidationid, 
overrideId, overrideMessages, locale,

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ValidatorMacroDemo.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ValidatorMacroDemo.tml?rev=1005093&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ValidatorMacroDemo.tml 
(added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/ValidatorMacroDemo.tml 
Wed Oct  6 15:57:25 2010
@@ -0,0 +1,18 @@
+<html t:type="Border" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
+       <h1>Validator Macro Demo</h1>
+       <t:form clientValidation="false">
+               <t:errors />
+               <p>
+                       <label t:type="label" for="password"/>
+                       <input t:type="passwordfield" t:id="password" 
validate="password"/>
+               </p>
+               <p>
+                       <label t:type="label" for="password2"/>
+                       <input t:type="passwordfield" t:id="password2" />
+               </p>
+               <input type="submit" />
+       </t:form>
+       
+       <p> Password: ${password}</p>
+       <p> Password2: ${password2}</p>
+</html>
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java?rev=1005093&r1=1005092&r2=1005093&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
 Wed Oct  6 15:57:25 2010
@@ -902,4 +902,39 @@ public class FormTests extends TapestryC
 
         assertTextPresent("Selected track: The Calling, Synaesthetic");
     }
+    
+    @Test
+    public void validation_macro() throws Exception
+    {
+        clickThru("Validator Macro Demo");
+
+        clickAndWait(SUBMIT);
+        
+        assertTextPresent("You must provide a value for Password.");
+        assertTextPresent("You must provide a value for Password2.");
+
+        type("password", "abcdefg");
+        type("password2", "x");
+
+        clickAndWait(SUBMIT);
+        
+        assertTextPresent("You may provide at most 3 characters for 
Password.");
+        assertTextPresent("You must provide at least 2 characters for 
Password2.");
+        
+        type("password", "a");
+        type("password2", "wxyz");
+
+        clickAndWait(SUBMIT);
+
+        assertTextPresent("You must provide at least 2 characters for 
Password.");
+        assertTextPresent("You may provide at most 3 characters for 
Password2.");
+        
+        type("password", "ab");
+        type("password2", "xyz");
+
+        clickAndWait(SUBMIT);
+
+        assertTextPresent("Password: ab");
+        assertTextPresent("Password2: xyz");
+    }
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java?rev=1005093&r1=1005092&r2=1005093&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
 Wed Oct  6 15:57:25 2010
@@ -459,7 +459,9 @@ public class Index
                             "Decorating page render links"),
 
                     new Item("DecorateComponentEventLinkDemo", "Decorate 
Component Event Link Demo",
-                            "Decorating event links")
+                            "Decorating event links"),
+
+                    new Item("ValidatorMacroDemo", "Validator Macro Demo", 
"Using validator macros")
 
             );
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java?rev=1005093&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java
 Wed Oct  6 15:57:25 2010
@@ -0,0 +1,30 @@
+// Copyright 2010 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package org.apache.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.beaneditor.Validate;
+
+public class ValidatorMacroDemo 
+{
+       @Property 
+       @Persist
+       private String password;
+       
+       @Property
+       @Persist
+       @Validate("password")
+       private String password2;
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ValidatorMacroDemo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java?rev=1005093&r1=1005092&r2=1005093&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/services/AppModule.java
 Wed Oct  6 15:57:25 2010
@@ -72,6 +72,12 @@ public class AppModule
         binder.bind(Reloadable.class);
         binder.bind(MessageAccess.class);
     }
+    
+    public static void 
contributeValidatorMacro(MappedConfiguration<String,String> configuration)
+    {
+       configuration.add("password", "required,lengthBetweenTwoAndThree");
+       configuration.add("lengthBetweenTwoAndThree", 
"minlength=2,maxlength=3");
+    }
 
     public void contributeAlias(Configuration<AliasContribution> configuration)
     {

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImplTest.java?rev=1005093&r1=1005092&r2=1005093&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImplTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldValidatorSourceImplTest.java
 Wed Oct  6 15:57:25 2010
@@ -56,18 +56,21 @@ public class FieldValidatorSourceImplTes
         ComponentResources resources = mockComponentResources();
         Messages containerMessages = mockMessages();
         Map<String, Validator> map = newMap();
+        ValidatorMacro macro = mockValidatorMacro();
 
         train_getComponentResources(field, resources);
         train_getId(resources, "fred");
         train_getLocale(resources, Locale.ENGLISH);
         train_getContainerMessages(resources, containerMessages);
+        
+        train_alwaysNull(macro);
 
         map.put("alpha", validator);
         map.put("beta", validator);
 
         replay();
 
-        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, null, map, null);
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, null, map, macro);
 
         try
         {
@@ -96,6 +99,7 @@ public class FieldValidatorSourceImplTes
         ComponentResources resources = mockComponentResources();
         Messages containerMessages = mockMessages();
         FormSupport fs = mockFormSupport();
+        ValidatorMacro macro = mockValidatorMacro();
 
         Map<String, Validator> map = singletonMap("required", validator);
 
@@ -104,9 +108,13 @@ public class FieldValidatorSourceImplTes
         train_getFormValidationId(fs, "form");
 
         train_getComponentResources(field, resources);
+        
 
         train_getId(resources, "fred");
         train_getContainerMessages(resources, containerMessages);
+        
+        train_alwaysNull(macro);
+        
         train_contains(containerMessages, "form-fred-required-message", false);
         train_contains(containerMessages, "fred-required-message", false);
 
@@ -123,7 +131,7 @@ public class FieldValidatorSourceImplTes
 
         replay();
 
-        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, null);
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, macro);
 
         FieldValidator fieldValidator = source.createValidator(field, 
"required", null);
 
@@ -145,6 +153,7 @@ public class FieldValidatorSourceImplTes
         ComponentResources resources = mockComponentResources();
         Messages containerMessages = mockMessages();
         FormSupport fs = mockFormSupport();
+        ValidatorMacro macro = mockValidatorMacro();
 
         Map<String, Validator> map = singletonMap("required", validator);
 
@@ -156,6 +165,8 @@ public class FieldValidatorSourceImplTes
         train_getId(resources, "fred");
         train_getLocale(resources, Locale.ENGLISH);
         train_getContainerMessages(resources, containerMessages);
+        
+        train_alwaysNull(macro);
 
         train_contains(containerMessages, "form-fred-required-message", false);
         train_contains(containerMessages, "fred-required-message", true);
@@ -168,7 +179,7 @@ public class FieldValidatorSourceImplTes
 
         replay();
 
-        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, null);
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, macro);
 
         FieldValidator fieldValidator = source.createValidator(field, 
"required", null);
 
@@ -189,6 +200,7 @@ public class FieldValidatorSourceImplTes
         ComponentResources resources = mockComponentResources();
         Messages containerMessages = mockMessages();
         FormSupport fs = mockFormSupport();
+        ValidatorMacro macro = mockValidatorMacro();
 
         Map<String, Validator> map = singletonMap("required", validator);
 
@@ -200,6 +212,8 @@ public class FieldValidatorSourceImplTes
         train_getId(resources, "fred");
         train_getLocale(resources, Locale.ENGLISH);
         train_getContainerMessages(resources, containerMessages);
+        
+        train_alwaysNull(macro);
 
         train_contains(containerMessages, "form-fred-required-message", true);
 
@@ -211,7 +225,7 @@ public class FieldValidatorSourceImplTes
 
         replay();
 
-        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, null);
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, macro);
 
         FieldValidator fieldValidator = source.createValidator(field, 
"required", null);
 
@@ -607,6 +621,7 @@ public class FieldValidatorSourceImplTes
         Messages containerMessages = mockMessages();
         Integer five = 5;
         FormSupport fs = mockFormSupport();
+        ValidatorMacro macro = mockValidatorMacro();
 
         Map<String, Validator> map = singletonMap("minLength", validator);
 
@@ -619,6 +634,9 @@ public class FieldValidatorSourceImplTes
         train_getComponentResources(field, resources);
         train_getId(resources, "fred");
         train_getContainerMessages(resources, containerMessages);
+        
+        train_alwaysNull(macro);
+        
         train_contains(containerMessages, "myform-fred-minLength-message", 
false);
         train_contains(containerMessages, "fred-minLength-message", false);
 
@@ -635,7 +653,7 @@ public class FieldValidatorSourceImplTes
 
         replay();
 
-        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, null);
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, macro);
 
         FieldValidator fieldValidator = source.createValidator(field, 
"minLength", "5");
 
@@ -643,6 +661,63 @@ public class FieldValidatorSourceImplTes
 
         verify();
     }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void validator_with_constraint_and_macro() throws Exception
+    {
+        ValidationMessagesSource messagesSource = 
mockValidationMessagesSource();
+        Validator validator = mockValidator();
+        TypeCoercer coercer = mockTypeCoercer();
+        FieldComponent field = newFieldComponent();
+        Messages messages = mockMessages();
+        MessageFormatter formatter = mockMessageFormatter();
+        Object inputValue = new Object();
+        ComponentResources resources = mockComponentResources();
+        Messages containerMessages = mockMessages();
+        Integer five = 5;
+        FormSupport fs = mockFormSupport();
+        ValidatorMacro macro = mockValidatorMacro();
+
+        Map<String, Validator> map = singletonMap("minLength", validator);
+
+        train_getConstraintType(validator, Integer.class);
+
+        train_getFormValidationId(fs, "myform");
+
+        train_coerce(coercer, "77", Integer.class, five);
+
+        train_getComponentResources(field, resources);
+        train_getId(resources, "fred");
+        train_getContainerMessages(resources, containerMessages);
+        
+        expect(macro.valueForMacro("foo-bar-baz")).andReturn("minLength=77");
+        expect(macro.valueForMacro("minLength")).andReturn(null);
+        
+        train_contains(containerMessages, "myform-fred-minLength-message", 
false);
+        train_contains(containerMessages, "fred-minLength-message", false);
+
+        train_getLocale(resources, Locale.FRENCH);
+
+        train_getValidationMessages(messagesSource, Locale.FRENCH, messages);
+
+        train_getMessageKey(validator, "key");
+        train_getMessageFormatter(messages, "key", formatter);
+
+        train_isRequired(validator, false);
+        train_getValueType(validator, Object.class);
+        validator.validate(field, five, formatter, inputValue);
+
+        replay();
+
+        FieldValidatorSource source = new 
FieldValidatorSourceImpl(messagesSource, coercer, fs, map, macro);
+
+        FieldValidator fieldValidator = source.createValidator(field, 
"foo-bar-baz", null);
+
+        fieldValidator.validate(inputValue);
+
+        verify();
+    }
 
     private FieldComponent newFieldComponent()
     {


Reply via email to