Search in sources :

Example 1 with RequiredFieldValidator

use of com.opensymphony.xwork2.validator.validators.RequiredFieldValidator in project struts by apache.

the class RequiredFieldValidatorTest method testNullObject.

@Test
public void testNullObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("stringValue");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
    rfv.setValidatorContext(context);
    // when
    rfv.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertNotNull(context.getFieldErrors().get("stringValue"));
    assertEquals("stringValue field is required!", context.getFieldErrors().get("stringValue").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

Example 2 with RequiredFieldValidator

use of com.opensymphony.xwork2.validator.validators.RequiredFieldValidator in project struts by apache.

the class RequiredFieldValidatorTest method testCollectionObject.

@Test
public void testCollectionObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("shorts");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    action.setShorts(new ArrayList<Short>());
    DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
    rfv.setValidatorContext(context);
    // when
    rfv.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertNotNull(context.getFieldErrors().get("shorts"));
    assertEquals("shorts field is required!", context.getFieldErrors().get("shorts").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

Example 3 with RequiredFieldValidator

use of com.opensymphony.xwork2.validator.validators.RequiredFieldValidator in project struts by apache.

the class RequiredFieldValidatorTest method testArrayObject.

@Test
public void testArrayObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("ints");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    action.setInts(new Integer[] {});
    DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
    rfv.setValidatorContext(context);
    // when
    rfv.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertNotNull(context.getFieldErrors().get("ints"));
    assertEquals("ints field is required!", context.getFieldErrors().get("ints").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

Example 4 with RequiredFieldValidator

use of com.opensymphony.xwork2.validator.validators.RequiredFieldValidator in project struts by apache.

the class ActionValidatorManagerTest method testValidate.

public void testValidate() throws Exception {
    /* MockAction.class */
    // reference number
    ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
    final RequiredStringValidator referenceNumberRequiredStringValidator = container.inject(RequiredStringValidator.class);
    referenceNumberRequiredStringValidator.setFieldName("referenceNumber");
    referenceNumberRequiredStringValidator.setDefaultMessage("Reference number is required");
    referenceNumberRequiredStringValidator.setValueStack(stack);
    // order
    final RequiredFieldValidator orderRequiredValidator = container.inject(RequiredFieldValidator.class);
    orderRequiredValidator.setFieldName("order");
    orderRequiredValidator.setDefaultMessage("Order is required");
    orderRequiredValidator.setValueStack(stack);
    // customer
    final RequiredFieldValidator customerRequiredValidator = container.inject(RequiredFieldValidator.class);
    customerRequiredValidator.setFieldName("customer");
    customerRequiredValidator.setDefaultMessage("Customer is required");
    customerRequiredValidator.setValueStack(stack);
    final VisitorFieldValidator customerVisitorValidator = container.inject(VisitorFieldValidator.class);
    customerVisitorValidator.setAppendPrefix(true);
    customerVisitorValidator.setFieldName("customer");
    customerVisitorValidator.setValueStack(stack);
    /* Customer.class */
    // customer -> name
    final RequiredStringValidator customerNameRequiredStringValidator = container.inject(RequiredStringValidator.class);
    customerNameRequiredStringValidator.setFieldName("name");
    customerNameRequiredStringValidator.setDefaultMessage("Name is required");
    customerNameRequiredStringValidator.setValueStack(stack);
    // customer -> age
    final RequiredFieldValidator customerAgeRequiredValidator = container.inject(RequiredFieldValidator.class);
    customerAgeRequiredValidator.setFieldName("age");
    customerAgeRequiredValidator.setDefaultMessage("Age is required");
    customerAgeRequiredValidator.setValueStack(stack);
    // customer -> Address
    final RequiredFieldValidator customerAddressRequiredFieldValidator = container.inject(RequiredFieldValidator.class);
    customerAddressRequiredFieldValidator.setFieldName("address");
    customerAddressRequiredFieldValidator.setDefaultMessage("Address is required");
    customerAddressRequiredFieldValidator.setValueStack(stack);
    final VisitorFieldValidator customerAddressVisitorFieldValidator = container.inject(VisitorFieldValidator.class);
    customerAddressVisitorFieldValidator.setFieldName("address");
    customerAddressVisitorFieldValidator.setAppendPrefix(true);
    // customerAddressVisitorFieldValidator.setDefaultMessage("");
    customerAddressVisitorFieldValidator.setValueStack(stack);
    /* Address.class */
    // customer -> Address -> street
    final RequiredStringValidator customerAddressStreetRequiredFieldValidator = container.inject(RequiredStringValidator.class);
    customerAddressStreetRequiredFieldValidator.setFieldName("street");
    customerAddressStreetRequiredFieldValidator.setDefaultMessage("Street is required");
    customerAddressStreetRequiredFieldValidator.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator.setValueStack(stack);
    final RequiredStringValidator customerAddressStreetRequiredFieldValidator2 = container.inject(RequiredStringValidator.class);
    customerAddressStreetRequiredFieldValidator2.setFieldName("street");
    customerAddressStreetRequiredFieldValidator2.setDefaultMessage("Street is required 2");
    customerAddressStreetRequiredFieldValidator2.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator2.setValueStack(stack);
    // customer -> Address -> pobox
    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator = container.inject(RequiredStringValidator.class);
    customerAddressPoboxRequiredFieldValidator.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator.setDefaultMessage("PO Box is required");
    customerAddressPoboxRequiredFieldValidator.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator.setValueStack(stack);
    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator2 = container.inject(RequiredStringValidator.class);
    customerAddressPoboxRequiredFieldValidator2.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator2.setDefaultMessage("PO Box is required 2");
    customerAddressPoboxRequiredFieldValidator2.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator2.setValueStack(stack);
    final List<Validator> validatorsForMockAction = new ArrayList<Validator>() {

        {
            add(referenceNumberRequiredStringValidator);
            add(orderRequiredValidator);
            add(customerRequiredValidator);
            add(customerVisitorValidator);
        }
    };
    final List<Validator> validatorsForCustomer = new ArrayList<Validator>() {

        {
            add(customerNameRequiredStringValidator);
            add(customerAgeRequiredValidator);
            add(customerAddressRequiredFieldValidator);
            add(customerAddressVisitorFieldValidator);
        }
    };
    final List<Validator> validatorsForAddress = new ArrayList<Validator>() {

        {
            add(customerAddressStreetRequiredFieldValidator);
            add(customerAddressStreetRequiredFieldValidator2);
            add(customerAddressPoboxRequiredFieldValidator);
            add(customerAddressPoboxRequiredFieldValidator2);
        }
    };
    DefaultActionValidatorManager validatorManager = new DefaultActionValidatorManager() {

        @Override
        public List<Validator> getValidators(Class clazz, String context, String method) {
            if (clazz.isAssignableFrom(MockAction.class)) {
                return validatorsForMockAction;
            } else if (clazz.isAssignableFrom(Customer.class)) {
                return validatorsForCustomer;
            } else if (clazz.isAssignableFrom(Address.class)) {
                return validatorsForAddress;
            }
            return Collections.emptyList();
        }
    };
    container.inject(validatorManager);
    customerVisitorValidator.setActionValidatorManager(validatorManager);
    customerAddressVisitorFieldValidator.setActionValidatorManager(validatorManager);
    MockAction action = container.inject(MockAction.class);
    stack.push(action);
    validatorManager.validate(action, "ctx");
    assertFalse(action.hasActionErrors());
    assertFalse(action.hasActionMessages());
    assertTrue(action.hasFieldErrors());
    assertTrue(action.getFieldErrors().containsKey("referenceNumber"));
    assertEquals((action.getFieldErrors().get("referenceNumber")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("order"));
    assertEquals((action.getFieldErrors().get("order")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.name"));
    assertEquals((action.getFieldErrors().get("customer.name")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.age"));
    assertEquals((action.getFieldErrors().get("customer.age")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.street"));
    assertEquals((action.getFieldErrors().get("customer.address.street")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.pobox"));
    assertEquals((action.getFieldErrors().get("customer.address.pobox")).size(), 2);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory) ArrayList(java.util.ArrayList) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)

Example 5 with RequiredFieldValidator

use of com.opensymphony.xwork2.validator.validators.RequiredFieldValidator in project struts by apache.

the class AnnotationActionValidatorManagerTest method no_testGetValidatorsFromInterface.

public void no_testGetValidatorsFromInterface() {
    List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction3.class, alias);
    // 17 in the class hierarchy + 1 in the interface + 1 in interface alias
    assertEquals(19, validatorList.size());
    final FieldValidator v = (FieldValidator) validatorList.get(0);
    assertEquals("bar", v.getFieldName());
    assertTrue(v instanceof RequiredFieldValidator);
    final FieldValidator v1 = (FieldValidator) validatorList.get(1);
    assertEquals("bar", v1.getFieldName());
    assertTrue(v1 instanceof IntRangeFieldValidator);
    final FieldValidator vdouble = (FieldValidator) validatorList.get(2);
    assertEquals("percentage", vdouble.getFieldName());
    assertTrue(vdouble instanceof DoubleRangeFieldValidator);
    final FieldValidator v2 = (FieldValidator) validatorList.get(3);
    assertEquals("baz", v2.getFieldName());
    assertTrue(v2 instanceof IntRangeFieldValidator);
    final FieldValidator v3 = (FieldValidator) validatorList.get(4);
    assertEquals("date", v3.getFieldName());
    assertTrue(v3 instanceof DateRangeFieldValidator);
    // action-level validator comes first
    final Validator v4 = (Validator) validatorList.get(5);
    assertTrue(v4 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v5 = (Validator) validatorList.get(6);
    assertTrue(v5 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v6 = (Validator) validatorList.get(7);
    assertTrue(v6 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v7 = (Validator) validatorList.get(8);
    assertTrue(v7 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v8 = (Validator) validatorList.get(9);
    assertTrue(v8 instanceof ExpressionValidator);
    final FieldValidator v9 = (FieldValidator) validatorList.get(10);
    assertEquals("datefield", v9.getFieldName());
    assertTrue(v9 instanceof DateRangeFieldValidator);
    final FieldValidator v10 = (FieldValidator) validatorList.get(11);
    assertEquals("emailaddress", v10.getFieldName());
    assertTrue(v10 instanceof EmailValidator);
    final FieldValidator v11 = (FieldValidator) validatorList.get(12);
    assertEquals("intfield", v11.getFieldName());
    assertTrue(v11 instanceof IntRangeFieldValidator);
    final FieldValidator v12 = (FieldValidator) validatorList.get(13);
    assertEquals("customfield", v12.getFieldName());
    assertTrue(v12 instanceof RequiredFieldValidator);
    final FieldValidator v13 = (FieldValidator) validatorList.get(14);
    assertEquals("stringisrequired", v13.getFieldName());
    assertTrue(v13 instanceof RequiredStringValidator);
    final FieldValidator v14 = (FieldValidator) validatorList.get(15);
    assertEquals("needstringlength", v14.getFieldName());
    assertTrue(v14 instanceof StringLengthFieldValidator);
    final FieldValidator v15 = (FieldValidator) validatorList.get(16);
    assertEquals("hreflocation", v15.getFieldName());
    assertTrue(v15 instanceof URLValidator);
    final FieldValidator v16 = (FieldValidator) validatorList.get(17);
    assertEquals("data", v16.getFieldName());
    assertTrue(v16 instanceof RequiredFieldValidator);
    final FieldValidator v17 = (FieldValidator) validatorList.get(18);
    assertEquals("data", v17.getFieldName());
    assertTrue(v17 instanceof RequiredStringValidator);
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) List(java.util.List) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)

Aggregations

TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)3 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)3 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)3 Test (org.junit.Test)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)2 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)2 IntRangeFieldValidator (com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator)2 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)2 List (java.util.List)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)1 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)1 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)1 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)1 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)1 ArrayList (java.util.ArrayList)1