Commits

Anonymous committed 433b11d

XW-571
- added test case for testing "nested visitor validators break short-circuit functionality"

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1675e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 926b8ef
  • Branches xwork_1-2

Comments (0)

Files changed (1)

File src/test/com/opensymphony/xwork/validator/ActionValidatorManagerTest.java

+package com.opensymphony.xwork.validator;
+
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.xwork.XWorkTestCase;
+import com.opensymphony.xwork.validator.validators.FieldExpressionValidator;
+import com.opensymphony.xwork.validator.validators.RequiredFieldValidator;
+import com.opensymphony.xwork.validator.validators.RequiredStringValidator;
+import com.opensymphony.xwork.validator.validators.VisitorFieldValidator;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * A test case for ActionValidatorManager. 
+ *
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class ActionValidatorManagerTest extends XWorkTestCase {
+
+
+
+    public void testValidate() throws Exception {
+        /* MockAction.class */
+        // reference number 
+        final RequiredStringValidator referenceNumberRequiredStringValidator = new RequiredStringValidator();
+        referenceNumberRequiredStringValidator.setFieldName("referenceNumber");
+        referenceNumberRequiredStringValidator.setDefaultMessage("Reference number is required");
+
+        // order
+        final RequiredFieldValidator orderRequiredValidator = new RequiredFieldValidator();
+        orderRequiredValidator.setFieldName("order");
+        orderRequiredValidator.setDefaultMessage("Order is required");
+
+        // customer
+        final RequiredFieldValidator customerRequiredValidator = new RequiredFieldValidator();
+        customerRequiredValidator.setFieldName("customer");
+        customerRequiredValidator.setDefaultMessage("Customer is required");
+        final VisitorFieldValidator customerVisitorValidator = new VisitorFieldValidator();
+        customerVisitorValidator.setAppendPrefix(true);
+        customerVisitorValidator.setFieldName("customer");
+
+        /* Customer.class */
+        // customer -> name
+        final RequiredStringValidator customerNameRequiredStringValidator = new RequiredStringValidator();
+        customerNameRequiredStringValidator.setFieldName("name");
+        customerNameRequiredStringValidator.setDefaultMessage("Name is required");
+
+        // customer -> age
+        final RequiredFieldValidator customerAgeRequiredValidator = new RequiredFieldValidator();
+        customerAgeRequiredValidator.setFieldName("age");
+        customerAgeRequiredValidator.setDefaultMessage("Age is required");
+
+        // customer -> Address
+        final RequiredFieldValidator customerAddressRequiredFieldValidator = new RequiredFieldValidator();
+        customerAddressRequiredFieldValidator.setFieldName("address");
+        customerAddressRequiredFieldValidator.setDefaultMessage("Address is required");
+        final VisitorFieldValidator customerAddressVisitorFieldValidator = new VisitorFieldValidator();
+        customerAddressVisitorFieldValidator.setFieldName("address");
+        customerAddressVisitorFieldValidator.setAppendPrefix(true);
+        //customerAddressVisitorFieldValidator.setDefaultMessage("");
+
+
+        /* Address.class */
+        // customer -> Address -> street
+        final RequiredStringValidator customerAddressStreetRequiredFieldValidator = new RequiredStringValidator();
+        customerAddressStreetRequiredFieldValidator.setFieldName("street");
+        customerAddressStreetRequiredFieldValidator.setDefaultMessage("Street is required");
+        customerAddressStreetRequiredFieldValidator.setShortCircuit(true);
+        final RequiredStringValidator customerAddressStreetRequiredFieldValidator2 = new RequiredStringValidator();
+        customerAddressStreetRequiredFieldValidator2.setFieldName("street");
+        customerAddressStreetRequiredFieldValidator2.setDefaultMessage("Street is required 2");
+        customerAddressStreetRequiredFieldValidator2.setShortCircuit(true);
+
+
+        // customer -> Address -> pobox
+        final RequiredStringValidator customerAddressPoboxRequiredFieldValidator = new RequiredStringValidator();
+        customerAddressPoboxRequiredFieldValidator.setFieldName("pobox");
+        customerAddressPoboxRequiredFieldValidator.setDefaultMessage("PO Box is required");
+        customerAddressPoboxRequiredFieldValidator.setShortCircuit(false);
+        final RequiredStringValidator customerAddressPoboxRequiredFieldValidator2 = new RequiredStringValidator();
+        customerAddressPoboxRequiredFieldValidator2.setFieldName("pobox");
+        customerAddressPoboxRequiredFieldValidator2.setDefaultMessage("PO Box is required 2");
+        customerAddressPoboxRequiredFieldValidator2.setShortCircuit(false);
+
+
+
+        final List validatorsForMockAction = new ArrayList() {
+            {
+                add(referenceNumberRequiredStringValidator);
+                add(orderRequiredValidator);
+                add(customerRequiredValidator);
+                add(customerVisitorValidator);
+            }
+        };
+
+        final List validatorsForCustomer = new ArrayList() {
+            {
+                add(customerNameRequiredStringValidator);
+                add(customerAgeRequiredValidator);
+                add(customerAddressRequiredFieldValidator);
+                add(customerAddressVisitorFieldValidator);
+            }
+        };
+
+        final List validatorsForAddress = new ArrayList() {
+            {
+                add(customerAddressStreetRequiredFieldValidator);
+                add(customerAddressStreetRequiredFieldValidator2);
+                add(customerAddressPoboxRequiredFieldValidator);
+                add(customerAddressPoboxRequiredFieldValidator2);
+            }
+        };
+
+
+        ActionValidatorManager validatorManager = new AbstractActionValidatorManager() {
+            public List getValidators(Class clazz, String context) {
+                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.EMPTY_LIST;
+            }
+        };
+        ActionValidatorManagerFactory.setInstance(validatorManager);
+        MockAction action = new MockAction();
+        validatorManager.validate(action, "ctx");
+        ActionValidatorManagerFactory.setInstance(new DefaultActionValidatorManager());
+
+        assertFalse(action.hasActionErrors());
+        assertFalse(action.hasActionMessages());
+        assertTrue(action.hasFieldErrors());
+        assertTrue(action.getFieldErrors().containsKey("referenceNumber"));
+        assertEquals(((List)action.getFieldErrors().get("referenceNumber")).size(), 1);
+        assertTrue(action.getFieldErrors().containsKey("order"));
+        assertEquals(((List)action.getFieldErrors().get("order")).size(), 1);
+        assertTrue(action.getFieldErrors().containsKey("customer.name"));
+        assertEquals(((List)action.getFieldErrors().get("customer.name")).size(), 1);
+        assertTrue(action.getFieldErrors().containsKey("customer.age"));
+        assertEquals(((List)action.getFieldErrors().get("customer.age")).size(), 1);
+        assertTrue(action.getFieldErrors().containsKey("customer.address.street"));
+        assertEquals(((List)action.getFieldErrors().get("customer.address.street")).size(), 1);
+        assertTrue(action.getFieldErrors().containsKey("customer.address.pobox"));
+        //assertEquals(((List)action.getFieldErrors().get("customer.address.pobox")).size(), 2);
+
+        for (Iterator i = action.getFieldErrors().keySet().iterator(); i.hasNext(); ) {
+            System.out.println(i.next());   
+        }
+
+
+        for (Iterator i = ((List)action.getFieldErrors().get("customer.address.street")).iterator(); i.hasNext();) {
+            System.out.println("**"+i.next());
+        }
+        for (Iterator i = ((List)action.getFieldErrors().get("customer.address.pobox")).iterator(); i.hasNext();) {
+            System.out.println("**"+i.next());    
+        }
+
+    }
+
+    private class MockAction extends ActionSupport {
+        
+        private String referenceNumber;
+        private Integer order;
+        private Customer customer = new Customer();
+
+
+        public String getReferenceNumber() { return referenceNumber; }
+        public void setReferenceNumber(String referenceNumber) { this.referenceNumber = referenceNumber; }
+
+        public Integer getOrder() { return order; }
+        public void setOrder(Integer order) { this.order = order; }
+
+        public Customer getCustomer() { return customer; }
+        public void setCustomer(Customer customer) { this.customer = customer; }
+    }
+
+
+    private class Customer {
+        private String name;
+        private Integer age;
+        private Address address = new Address();
+
+        public String getName() { return name; }
+        public void setName(String name) { this.name = name; }
+
+        public Integer getAge() { return age; }
+        public void setAge(Integer age) { this.age = age; }
+
+        public Address getAddress() { return address; }
+        public void setAddress(Address address) { this.address = address; }
+    }
+
+    private class Address {
+        private String street;
+        private String pobox;
+
+        public String getStreet() { return street; }
+        public void setStreet(String street) { this.street = street; }
+
+        public String getPobox() { return pobox; }
+        public void setPobox(String pobox) { this.pobox = pobox; }
+    }
+}