tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/form FormComponentContributorContextImpl.java ValidationMessagesImpl.java ValidatableFieldSupportImpl.java
Date Tue, 02 Aug 2005 14:59:41 GMT
hlship      2005/08/02 07:59:41

  Modified:    .        status.xml
               framework/src/test/org/apache/tapestry/form
                        TestValidationMessages.java
                        TestValidatableFieldSupportImpl.java
               framework/src/java/org/apache/tapestry/form
                        FormComponentContributorContextImpl.java
                        ValidationMessagesImpl.java
                        ValidatableFieldSupportImpl.java
  Log:
  TAPESTRY-413: Allow access to component message catalog from validators: binding string
  
  Revision  Changes    Path
  1.181     +1 -0      jakarta-tapestry/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/status.xml,v
  retrieving revision 1.180
  retrieving revision 1.181
  diff -u -r1.180 -r1.181
  --- status.xml	30 Jul 2005 12:39:16 -0000	1.180
  +++ status.xml	2 Aug 2005 14:59:41 -0000	1.181
  @@ -60,6 +60,7 @@
         <action type="update" dev="HLS">Remove default-binding attribute from &lt;parameter&gt;
element</action>
   	    <action type="fix" dev="HLS" fixes-bug="TAPESTRY-515">Deprecate out-of-date
contrib components</action>
         <action type="fix" dev="HLS" fixes-bug="TAPESTRY-481">Enhancement Workers that
create read-only properties should flag setter methods as an error</action>
  +      <action type="fix" dev="HLS" fixes-bug="TAPESTRY-413">Allow access to component
message catalog from validators: binding string</action>
       </release>
       <release version="4.0-beta-3" date="Jul 22 2005">
         <action type="fix" dev="HLS" fixes-bug="TAPESTRY-398" due-to="Jonas Maurus">HiveMind
configuration error breaks the useage of the state: binding prefix</action>
  
  
  
  1.2       +78 -3     jakarta-tapestry/framework/src/test/org/apache/tapestry/form/TestValidationMessages.java
  
  Index: TestValidationMessages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/TestValidationMessages.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestValidationMessages.java	18 Jun 2005 12:54:39 -0000	1.1
  +++ TestValidationMessages.java	2 Aug 2005 14:59:41 -0000	1.2
  @@ -16,8 +16,11 @@
   
   import java.util.Locale;
   
  +import org.apache.hivemind.Messages;
   import org.apache.hivemind.test.HiveMindTestCase;
  +import org.apache.tapestry.IComponent;
   import org.apache.tapestry.valid.ValidationStrings;
  +import org.easymock.MockControl;
   
   /**
    * Tests for {@link org.apache.tapestry.form.ValidationMessagesImpl}.
  @@ -27,9 +30,18 @@
    */
   public class TestValidationMessages extends HiveMindTestCase
   {
  +    private IFormComponent newField()
  +    {
  +        return (IFormComponent) newMock(IFormComponent.class);
  +    }
  +
       public void testMessageOverrideNull()
       {
  -        ValidationMessages m = new ValidationMessagesImpl(Locale.ENGLISH);
  +        IFormComponent field = newField();
  +
  +        ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
  +
  +        replayControls();
   
           assertEquals("You must enter a value for My Field.", m.formatValidationMessage(
                   null,
  @@ -37,24 +49,87 @@
                   new Object[]
                   { "My Field" }));
   
  -        m = new ValidationMessagesImpl(new Locale("es"));
  +        m = new ValidationMessagesImpl(field, new Locale("es"));
   
           assertEquals("Tiene que ingresar un valor para My Field.", m.formatValidationMessage(
                   null,
                   ValidationStrings.REQUIRED_TEXT_FIELD,
                   new Object[]
                   { "My Field" }));
  +
  +        verifyControls();
       }
   
       public void testMessageOverride()
       {
  -        ValidationMessages m = new ValidationMessagesImpl(Locale.ENGLISH);
  +        IFormComponent field = newField();
  +
  +        ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
  +
  +        replayControls();
   
           assertEquals("Gimme data for My Field.", m.formatValidationMessage(
                   "Gimme data for {0}.",
                   ValidationStrings.REQUIRED_TEXT_FIELD,
                   new Object[]
                   { "My Field" }));
  +
  +        verifyControls();
  +    }
  +
  +    /**
  +     * Test the use of the '%key' construct as the message.
  +     */
  +
  +    public void testMessageOverrideAsReference()
  +    {
  +        Messages messages = newMessage("myfield-required", "Yo Dawg! Gimme a piece of {0}.");
  +        IComponent container = newComponent(messages);
  +        IFormComponent field = newField(container);
  +
  +        ValidationMessages m = new ValidationMessagesImpl(field, Locale.ENGLISH);
  +
  +        replayControls();
  +
  +        assertEquals("Yo Dawg! Gimme a piece of My Field.", m.formatValidationMessage(
  +                "%myfield-required",
  +                ValidationStrings.REQUIRED_TEXT_FIELD,
  +                new Object[]
  +                { "My Field" }));
  +
  +        verifyControls();
       }
   
  +    private IFormComponent newField(IComponent container)
  +    {
  +        MockControl control = newControl(IFormComponent.class);
  +        IFormComponent field = (IFormComponent) control.getMock();
  +
  +        field.getContainer();
  +        control.setReturnValue(container);
  +
  +        return field;
  +    }
  +
  +    private IComponent newComponent(Messages messages)
  +    {
  +        MockControl control = newControl(IComponent.class);
  +        IComponent component = (IComponent) control.getMock();
  +
  +        component.getMessages();
  +        control.setReturnValue(messages);
  +
  +        return component;
  +    }
  +
  +    private Messages newMessage(String key, String message)
  +    {
  +        MockControl control = newControl(Messages.class);
  +        Messages messages = (Messages) control.getMock();
  +
  +        messages.getMessage(key);
  +        control.setReturnValue(message);
  +
  +        return messages;
  +    }
   }
  
  
  
  1.5       +18 -18    jakarta-tapestry/framework/src/test/org/apache/tapestry/form/TestValidatableFieldSupportImpl.java
  
  Index: TestValidatableFieldSupportImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/form/TestValidatableFieldSupportImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestValidatableFieldSupportImpl.java	18 Jun 2005 18:09:13 -0000	1.4
  +++ TestValidatableFieldSupportImpl.java	2 Aug 2005 14:59:41 -0000	1.5
  @@ -274,11 +274,8 @@
           _component.getTranslator();
           _componentControl.setReturnValue(_translator);
   
  -        _translator.renderContribution(
  -                _writer,
  -                cycle,
  -                new FormComponentContributorContextImpl(),
  -                _component);
  +        _translator.renderContribution(_writer, cycle, new FormComponentContributorContextImpl(
  +                _component), _component);
           _translatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, null, new TypeMatcher(), null }));
   
  @@ -354,11 +351,8 @@
           _component.getTranslator();
           _componentControl.setReturnValue(_translator);
   
  -        _translator.renderContribution(
  -                _writer,
  -                cycle,
  -                new FormComponentContributorContextImpl(),
  -                _component);
  +        _translator.renderContribution(_writer, cycle, new FormComponentContributorContextImpl(
  +                _component), _component);
           _translatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, null, new TypeMatcher(), null }));
   
  @@ -368,11 +362,8 @@
           _valueConverter.coerceValue(_validator, Iterator.class);
           _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
   
  -        _validator.renderContribution(
  -                _writer,
  -                cycle,
  -                new FormComponentContributorContextImpl(),
  -                _component);
  +        _validator.renderContribution(_writer, cycle, new FormComponentContributorContextImpl(
  +                _component), _component);
           _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, null, new TypeMatcher(), null }));
           replay();
  @@ -472,7 +463,10 @@
           _valueConverter.coerceValue(_validator, Iterator.class);
           _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
   
  -        _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), object);
  +        _validator.validate(
  +                _component,
  +                new ValidationMessagesImpl(_component, Locale.ENGLISH),
  +                object);
           _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, new TypeMatcher(), null }));
   
  @@ -512,7 +506,10 @@
           _validator.getAcceptsNull();
           _validatorControl.setReturnValue(true);
   
  -        _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), null);
  +        _validator.validate(
  +                _component,
  +                new ValidationMessagesImpl(_component, Locale.ENGLISH),
  +                null);
           _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, new TypeMatcher(), null }));
   
  @@ -589,7 +586,10 @@
           _valueConverter.coerceValue(_validator, Iterator.class);
           _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
   
  -        _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), object);
  +        _validator.validate(
  +                _component,
  +                new ValidationMessagesImpl(_component, Locale.ENGLISH),
  +                object);
           _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
           { null, new TypeMatcher(), null }));
           _validatorControl.setThrowable(ex);
  
  
  
  1.3       +9 -9      jakarta-tapestry/framework/src/java/org/apache/tapestry/form/FormComponentContributorContextImpl.java
  
  Index: FormComponentContributorContextImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/form/FormComponentContributorContextImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FormComponentContributorContextImpl.java	21 Jul 2005 13:17:55 -0000	1.2
  +++ FormComponentContributorContextImpl.java	2 Aug 2005 14:59:41 -0000	1.3
  @@ -49,11 +49,11 @@
        * Used for testing.
        */
   
  -    FormComponentContributorContextImpl()
  +    FormComponentContributorContextImpl(IFormComponent field)
       {
  -        super(Locale.ENGLISH);
  +        super(field, Locale.ENGLISH);
   
  -        _field = null;
  +        _field = field;
           _resolver = null;
           _formDOM = null;
           _fieldDOM = null;
  @@ -62,19 +62,19 @@
       }
   
       public FormComponentContributorContextImpl(Locale locale, IRequestCycle cycle,
  -            IFormComponent component)
  +            IFormComponent field)
       {
  -        super(locale);
  +        super(field, locale);
   
  -        _field = component;
  -        _form = component.getForm();
  +        _field = field;
  +        _form = field.getForm();
   
           _resolver = cycle.getInfrastructure().getClassResolver();
   
           _formDOM = "document." + _form.getName();
  -        _fieldDOM = _formDOM + "." + component.getName();
  +        _fieldDOM = _formDOM + "." + field.getName();
   
  -        _pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, component);
  +        _pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, field);
       }
   
       public String getFieldDOM()
  
  
  
  1.2       +23 -5     jakarta-tapestry/framework/src/java/org/apache/tapestry/form/ValidationMessagesImpl.java
  
  Index: ValidationMessagesImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/form/ValidationMessagesImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ValidationMessagesImpl.java	18 Jun 2005 12:54:39 -0000	1.1
  +++ ValidationMessagesImpl.java	2 Aug 2005 14:59:41 -0000	1.2
  @@ -30,23 +30,41 @@
    */
   public class ValidationMessagesImpl implements ValidationMessages
   {
  -    private Locale _locale;
  +    private final IFormComponent _field;
   
  -    public ValidationMessagesImpl(Locale locale)
  +    private final Locale _locale;
  +
  +    public ValidationMessagesImpl(IFormComponent field, Locale locale)
       {
  +        Defense.notNull(field, "field");
           Defense.notNull(locale, "locale");
   
  +        _field = field;
           _locale = locale;
       }
   
       public String formatValidationMessage(String messageOverride, String messageKey,
               Object[] arguments)
       {
  -        String message = (messageOverride == null) ? ValidationStrings.getMessagePattern(
  -                messageKey,
  -                _locale) : messageOverride;
  +        String message = extractLocalizedMessage(messageOverride, messageKey);
   
           return MessageFormat.format(message, arguments);
       }
   
  +    private String extractLocalizedMessage(String messageOverride, String messageKey)
  +    {
  +        if (messageOverride == null)
  +            return ValidationStrings.getMessagePattern(messageKey, _locale);
  +
  +        if (messageOverride.startsWith("%"))
  +        {
  +            String key = messageOverride.substring(1);
  +
  +            return _field.getContainer().getMessages().getMessage(key);
  +        }
  +
  +        // Otherwise, a literal string
  +
  +        return messageOverride;
  +    }
   }
  
  
  
  1.6       +1 -1      jakarta-tapestry/framework/src/java/org/apache/tapestry/form/ValidatableFieldSupportImpl.java
  
  Index: ValidatableFieldSupportImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/form/ValidatableFieldSupportImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ValidatableFieldSupportImpl.java	18 Jun 2005 18:09:13 -0000	1.5
  +++ ValidatableFieldSupportImpl.java	2 Aug 2005 14:59:41 -0000	1.6
  @@ -122,7 +122,7 @@
   
               Iterator validators = getValidatorsIterator(component);
   
  -            ValidationMessages messages = new ValidationMessagesImpl(_threadLocale.getLocale());
  +            ValidationMessages messages = new ValidationMessagesImpl(component, _threadLocale.getLocale());
   
               while (validators.hasNext())
               {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message