You can create an behavior doing the check in the
Behavior#onComponentTag. At this point you will have access to the
final ComponentTag object.
The test case would look like:

testSomePageOrComponet(){
  CollectMissingAttributes theBehaviorITalkedAbout = new (...);
  MyPageOrComponentType pageOrComponentUnderTest = (...);
  pageOrComponentUnderTest.visit(
    new visitor(component){ component.add(theBehaviorITalkedAbout); }
  );
  assertEmpty(theBehaviorITalkedAbout.getComponentsMissingSomeAttribute());
}


On Tue, May 24, 2011 at 5:12 PM, Craig Pardey
<craig.par...@intelliware.ca> wrote:
> The IMarkupFilter approach only detects attributes coded into the HTML.
>
> Is there any way to get it to work for attributes created using the 
> SimpleAttributeModifier or AttributeAppender? See code.
>
> FWIW I also tried the onComponentTag approach documented on the wiki
> https://cwiki.apache.org/WICKET/how-to-modify-an-attribute-on-a-html-tag.html
>
> Craig
>
> public class MyTextField<T> extends TextF;ield<T> {
>        ....
>        public MyTextField<T> setAttribute(String name, String value){
>                this.add(new SimpleAttributeModifier(name, value));
>                return this;
>        }
>        ....
> }
>
> public class MarkupRuleFilter extends AbstractMarkupFilter {
>        ....
>        @Override
>        public MarkupElement nextTag() throws ParseException {
>                ComponentTag tag = nextComponentTag();
>                String attrVal = tag.getAttribute("maxlength");
>                if( StringUtils.isBlank(attrVal)){
>                        throw new IllegalStateException("No maxlength defined 
> for " + tag.getId());
>                }
>                return tag;
>        }
>        ....
> }
>
> Craig
>
> -----Original Message-----
> From: Igor Vaynberg [mailto:igor.vaynb...@gmail.com]
> Sent: May-13-11 5:02 PM
> To: users@wicket.apache.org
> Subject: Re: How to check markup attributes?
>
> if you are doing validation you can use imarkupfilter to check the attrs.
>
> -igor
>
>
> On Fri, May 13, 2011 at 1:57 PM, Craig Pardey
> <craig.par...@intelliware.ca> wrote:
>> I'd like to check that particular markup attributes have been set on a 
>> component.
>> My first instinct was to use component.getMarkupAttributes(), but the 
>> JavaDoc quite clearly suggests that it shouldn't be used.
>>
>> For example, all TextFields should have a 'maxlength' defined:
>>
>> public class MyTextField<T> extends TextField<T> {
>>      @Override
>>      public void onAfterRender(){
>>            super.onAfterRender();
>>            ValueMap attrs = getMarkupAttributes();
>>            if( !attrs.containsKey("maxlength")){
>>                  throw new IllegalStateException("No maxlength defined for " 
>> + getId());
>>            }
>>      }
>> }
>>
>> Is there a better way to achieve this?
>> Ideally I'd like to do it in a unit test rather than at runtime.
>>
>> Craig
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>



-- 
Pedro Henrique Oliveira dos Santos

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to