Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 87   Methods: 6
NCLOC: 54   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MaxLength.java 100% 100% 83.3% 95.7%
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.tapestry.IMarkupWriter;
 18    import org.apache.tapestry.IRequestCycle;
 19    import org.apache.tapestry.TapestryUtils;
 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.ValidationConstraint;
 24    import org.apache.tapestry.valid.ValidationStrings;
 25    import org.apache.tapestry.valid.ValidatorException;
 26   
 27    /**
 28    * Validator that ensures a string value does not exceed a maximum length.
 29    *
 30    * @author Howard Lewis Ship
 31    * @since 4.0
 32    */
 33    public class MaxLength extends BaseValidator
 34    {
 35    private int _maxLength;
 36   
 37  0 public MaxLength()
 38    {
 39   
 40    }
 41   
 42  4 public MaxLength(String initializer)
 43    {
 44  4 super(initializer);
 45    }
 46   
 47  4 public void setMaxLength(int maxLength)
 48    {
 49  4 _maxLength = maxLength;
 50    }
 51   
 52  3 public void validate(IFormComponent field, ValidationMessages messages, Object object)
 53    throws ValidatorException
 54    {
 55  3 String string = (String) object;
 56   
 57  3 if (string.length() > _maxLength)
 58  2 throw new ValidatorException(buildMessage(messages, field),
 59    ValidationConstraint.MAXIMUM_WIDTH);
 60    }
 61   
 62  3 protected String buildMessage(ValidationMessages messages, IFormComponent field)
 63    {
 64  3 return messages.formatValidationMessage(
 65    getMessage(),
 66    ValidationStrings.VALUE_TOO_LONG,
 67    new Object[]
 68    { new Integer(_maxLength), field.getDisplayName() });
 69    }
 70   
 71  1 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 72    FormComponentContributorContext context, IFormComponent field)
 73    {
 74  1 context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
 75   
 76  1 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_max_length(event, '");
 77  1 buffer.append(field.getClientId());
 78  1 buffer.append("', ");
 79  1 buffer.append(_maxLength);
 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    }