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