Clover coverage report - Code Coverage for tapestry release 4.0-beta-4
Coverage timestamp: Wed Aug 10 2005 21:19:31 EDT
file stats: LOC: 93   Methods: 7
NCLOC: 59   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.form.FormComponentContributorContext;
 22    import org.apache.tapestry.form.IFormComponent;
 23    import org.apache.tapestry.form.ValidationMessages;
 24    import org.apache.tapestry.valid.ValidationConstants;
 25    import org.apache.tapestry.valid.ValidationConstraint;
 26    import org.apache.tapestry.valid.ValidationStrings;
 27    import org.apache.tapestry.valid.ValidatorException;
 28   
 29    /**
 30    * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
 31    *
 32    * @author Howard Lewis Ship
 33    * @since 4.0
 34    */
 35    public class Required extends BaseValidator
 36    {
 37  7 public Required()
 38    {
 39    }
 40   
 41  1 public Required(String initializer)
 42    {
 43  1 super(initializer);
 44    }
 45   
 46  0 public boolean getAcceptsNull()
 47    {
 48  0 return true;
 49    }
 50   
 51  5 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 52    throws ValidatorException
 53    {
 54  5 if ((object == null)
 55    || (String.class.isInstance(object) && (((String) object).length() == 0))
 56    || (Collection.class.isInstance(object) && ((Collection) object).isEmpty()))
 57    {
 58  4 String message = buildMessage(messages, field);
 59  4 throw new ValidatorException(message, ValidationConstraint.REQUIRED);
 60    }
 61    }
 62   
 63  5 private String buildMessage(ValidationMessages messages, IFormComponent field)
 64    {
 65  5 return messages.formatValidationMessage(
 66    getMessage(),
 67    ValidationStrings.REQUIRED_FIELD,
 68    new Object[]
 69    { field.getDisplayName() });
 70    }
 71   
 72  1 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 73    FormComponentContributorContext context, IFormComponent field)
 74    {
 75  1 context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
 76   
 77  1 StringBuffer buffer = new StringBuffer("function(event) { require(event, ");
 78  1 buffer.append(context.getFieldDOM());
 79  1 buffer.append(", '");
 80  1 buffer.append(ValidatorUtils.escapeReservedCharacters(buildMessage(context, field)));
 81  1 buffer.append("'); }");
 82   
 83  1 context.addSubmitListener(buffer.toString());
 84    }
 85   
 86    /**
 87    * Returns true, that's what Required means!
 88    */
 89  1 public boolean isRequired()
 90    {
 91  1 return true;
 92    }
 93    }