Clover coverage report - Code Coverage for tapestry release 4.0-beta-3
Coverage timestamp: Sun Jul 24 2005 08:26:33 EDT
file stats: LOC: 83   Methods: 6
NCLOC: 53   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Required.java 100% 92.3% 83.3% 90.5%
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 org.apache.hivemind.util.PropertyUtils;
 18    import org.apache.tapestry.IMarkupWriter;
 19    import org.apache.tapestry.IRequestCycle;
 20    import org.apache.tapestry.form.FormComponentContributorContext;
 21    import org.apache.tapestry.form.IFormComponent;
 22    import org.apache.tapestry.form.ValidationMessages;
 23    import org.apache.tapestry.valid.ValidationConstants;
 24    import org.apache.tapestry.valid.ValidationConstraint;
 25    import org.apache.tapestry.valid.ValidationStrings;
 26    import org.apache.tapestry.valid.ValidatorException;
 27   
 28    /**
 29    * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied.
 30    *
 31    * @author Howard Lewis Ship
 32    * @since 4.0
 33    */
 34    public class Required extends BaseValidator
 35    {
 36  4 public Required()
 37    {
 38    }
 39   
 40  1 public Required(String initializer)
 41    {
 42  1 super(initializer);
 43    }
 44   
 45  0 public boolean getAcceptsNull()
 46    {
 47  0 return true;
 48    }
 49   
 50  3 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 51    throws ValidatorException
 52    {
 53  3 if (object == null)
 54    {
 55  2 String message = buildMessage(messages, field);
 56  2 throw new ValidatorException(message, ValidationConstraint.REQUIRED);
 57    }
 58    }
 59   
 60  3 private String buildMessage(ValidationMessages messages, IFormComponent field)
 61    {
 62  3 return messages.formatValidationMessage(
 63    getMessage(),
 64    ValidationStrings.REQUIRED_TEXT_FIELD,
 65    new Object[]
 66    { field.getDisplayName() });
 67    }
 68   
 69  1 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 70    FormComponentContributorContext context, IFormComponent field)
 71    {
 72  1 context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
 73   
 74  1 StringBuffer buffer = new StringBuffer("function(event) { require(event, ");
 75  1 buffer.append(context.getFieldDOM());
 76  1 buffer.append(", '");
 77  1 buffer.append(buildMessage(context, field));
 78  1 buffer.append("'); }");
 79   
 80  1 context.addSubmitListener(buffer.toString());
 81    }
 82   
 83    }