Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 94   Methods: 7
NCLOC: 60   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Required.java 100% 92.9% 85.7% 91.3%
coverage coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.form.validator;
 16   
 17    import java.util.Collection;
 18   
 19    import org.apache.tapestry.IMarkupWriter;
 20    import org.apache.tapestry.IRequestCycle;
 21    import org.apache.tapestry.TapestryUtils;
 22    import org.apache.tapestry.form.FormComponentContributorContext;
 23    import org.apache.tapestry.form.IFormComponent;
 24    import org.apache.tapestry.form.ValidationMessages;
 25    import org.apache.tapestry.valid.ValidationConstants;
 26    import org.apache.tapestry.valid.ValidationConstraint;
 27    import org.apache.tapestry.valid.ValidationStrings;
 28    import org.apache.tapestry.valid.ValidatorException;
 29   
 30    /**
 31    * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
 32    *
 33    * @author Howard Lewis Ship
 34    * @since 4.0
 35    */
 36    public class Required extends BaseValidator
 37    {
 38  7 public Required()
 39    {
 40    }
 41   
 42  1 public Required(String initializer)
 43    {
 44  1 super(initializer);
 45    }
 46   
 47  0 public boolean getAcceptsNull()
 48    {
 49  0 return true;
 50    }
 51   
 52  5 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 53    throws ValidatorException
 54    {
 55  5 if ((object == null)
 56    || (String.class.isInstance(object) && (((String) object).length() == 0))
 57    || (Collection.class.isInstance(object) && ((Collection) object).isEmpty()))
 58    {
 59  4 String message = buildMessage(messages, field);
 60  4 throw new ValidatorException(message, ValidationConstraint.REQUIRED);
 61    }
 62    }
 63   
 64  5 private String buildMessage(ValidationMessages messages, IFormComponent field)
 65    {
 66  5 return messages.formatValidationMessage(
 67    getMessage(),
 68    ValidationStrings.REQUIRED_FIELD,
 69    new Object[]
 70    { field.getDisplayName() });
 71    }
 72   
 73  1 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 74    FormComponentContributorContext context, IFormComponent field)
 75    {
 76  1 context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
 77   
 78  1 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.require_field(event, '");
 79  1 buffer.append(field.getClientId());
 80  1 buffer.append("', ");
 81  1 buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
 82  1 buffer.append("); }");
 83   
 84  1 context.addSubmitHandler(buffer.toString());
 85    }
 86   
 87    /**
 88    * Returns true, that's what Required means!
 89    */
 90  1 public boolean isRequired()
 91    {
 92  1 return true;
 93    }
 94    }