You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2005/06/18 20:13:19 UTC

cvs commit: jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator TestMinLength.java TestMinLengthValidator.java

hlship      2005/06/18 11:13:19

  Added:       framework/src/java/org/apache/tapestry/form/validator
                        MinLength.java
               framework/src/test/org/apache/tapestry/form/validator
                        TestMinLength.java
  Removed:     framework/src/java/org/apache/tapestry/form/validator
                        MinLengthValidator.java
               framework/src/test/org/apache/tapestry/form/validator
                        TestMinLengthValidator.java
  Log:
  Rename MinLengthValidator to MinLength.
  
  Revision  Changes    Path
  1.1                  jakarta-tapestry/framework/src/java/org/apache/tapestry/form/validator/MinLength.java
  
  Index: MinLength.java
  ===================================================================
  // Copyright 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.form.validator;
  
  import org.apache.hivemind.util.PropertyUtils;
  import org.apache.tapestry.IMarkupWriter;
  import org.apache.tapestry.IRequestCycle;
  import org.apache.tapestry.form.FormComponentContributorContext;
  import org.apache.tapestry.form.IFormComponent;
  import org.apache.tapestry.form.ValidationMessages;
  import org.apache.tapestry.valid.ValidationConstraint;
  import org.apache.tapestry.valid.ValidationStrings;
  import org.apache.tapestry.valid.ValidatorException;
  
  /**
   * Validates that the value, a string, is of a minimum length.
   * 
   * @author Howard Lewis Ship
   * @since 4.0
   */
  public class MinLength implements Validator
  {
      private int _minLength;
  
      private String _message;
  
      public MinLength()
      {
      }
  
      public MinLength(String initializer)
      {
          PropertyUtils.configureProperties(this, initializer);
      }
  
      public void setMessage(String message)
      {
          _message = message;
      }
  
      public void setMinLength(int minLength)
      {
          _minLength = minLength;
      }
  
      public boolean getAcceptsNull()
      {
          return false;
      }
  
      public void validate(IFormComponent field, ValidationMessages messages, Object object)
              throws ValidatorException
      {
          String string = (String) object;
  
          if (string.length() < _minLength)
              throw new ValidatorException(buildMessage(messages, field),
                      ValidationConstraint.MINIMUM_WIDTH);
      }
  
      protected String buildMessage(ValidationMessages messages, IFormComponent field)
      {
          return messages.formatValidationMessage(
                  _message,
                  ValidationStrings.VALUE_TOO_SHORT,
                  new Object[]
                  { new Integer(_minLength), field.getDisplayName() });
      }
  
      public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
              FormComponentContributorContext context, IFormComponent field)
      {
          StringBuffer buffer = new StringBuffer("function(event) { validate_min_length(event, ");
          buffer.append(context.getFieldDOM());
          buffer.append(", ");
          buffer.append(_minLength);
          buffer.append(", '");
          buffer.append(buildMessage(context, field));
          buffer.append("'); }");
  
          context.addSubmitListener(buffer.toString());
      }
  }
  
  
  1.1                  jakarta-tapestry/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java
  
  Index: TestMinLength.java
  ===================================================================
  // Copyright 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.form.validator;
  
  import org.apache.tapestry.IMarkupWriter;
  import org.apache.tapestry.IRequestCycle;
  import org.apache.tapestry.form.FormComponentContributorContext;
  import org.apache.tapestry.form.IFormComponent;
  import org.apache.tapestry.form.ValidationMessages;
  import org.apache.tapestry.valid.ValidationConstraint;
  import org.apache.tapestry.valid.ValidationStrings;
  import org.apache.tapestry.valid.ValidatorException;
  import org.easymock.MockControl;
  
  /**
   * Tests for {@link org.apache.tapestry.form.validator.MinLength}.
   * 
   * @author Howard Lewis Ship
   * @since 4.0
   */
  public class TestMinLength extends BaseValidatorTestCase
  {
      public void testOK() throws Exception
      {
          IFormComponent field = newField();
          ValidationMessages messages = newMessages();
  
          String object = "a nice long string";
  
          replayControls();
  
          new MinLength("minLength=5").validate(field, messages, object);
  
          verifyControls();
      }
  
      public void testFail()
      {
          IFormComponent field = newField("My Field");
          ValidationMessages messages = newMessages(
                  null,
                  ValidationStrings.VALUE_TOO_SHORT,
                  new Object[]
                  { new Integer(10), "My Field" },
                  "Exception!");
  
          replayControls();
  
          try
          {
              new MinLength("minLength=10").validate(field, messages, "short");
          }
          catch (ValidatorException ex)
          {
              assertEquals("Exception!", ex.getMessage());
              assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
          }
      }
  
      public void testFailCustomMessage()
      {
          IFormComponent field = newField("My Field");
          ValidationMessages messages = newMessages(
                  "Too Short",
                  ValidationStrings.VALUE_TOO_SHORT,
                  new Object[]
                  { new Integer(10), "My Field" },
                  "Exception!");
  
          replayControls();
  
          try
          {
              new MinLength("minLength=10,message=Too Short").validate(
                      field,
                      messages,
                      "short");
          }
          catch (ValidatorException ex)
          {
              assertEquals("Exception!", ex.getMessage());
              assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
          }
      }
  
      public void testRenderContribution()
      {
          IMarkupWriter writer = newWriter();
          IRequestCycle cycle = newCycle();
          IFormComponent field = newField("My Field");
          MockControl contextc = newControl(FormComponentContributorContext.class);
          FormComponentContributorContext context = (FormComponentContributorContext) contextc
                  .getMock();
  
          context.getFieldDOM();
          contextc.setReturnValue("document.myform.myfield");
  
          trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_SHORT, new Object[]
          { new Integer(20), "My Field" }, "default message");
  
          context
                  .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 20, 'default message'); }");
  
          replayControls();
  
          new MinLength("minLength=20").renderContribution(writer, cycle, context, field);
  
          verifyControls();
      }
  
      public void testRenderContributionCustomMessage()
      {
          IMarkupWriter writer = newWriter();
          IRequestCycle cycle = newCycle();
          IFormComponent field = newField("My Field");
          MockControl contextc = newControl(FormComponentContributorContext.class);
          FormComponentContributorContext context = (FormComponentContributorContext) contextc
                  .getMock();
  
          context.getFieldDOM();
          contextc.setReturnValue("document.myform.myfield");
  
          trainFormatMessage(
                  contextc,
                  context,
                  "custom",
                  ValidationStrings.VALUE_TOO_SHORT,
                  new Object[]
                  { new Integer(25), "My Field" },
                  "custom message");
  
          context
                  .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 25, 'custom message'); }");
  
          replayControls();
  
          new MinLength("minLength=25,message=custom").renderContribution(
                  writer,
                  cycle,
                  context,
                  field);
  
          verifyControls();
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org