Search in sources :

Example 51 with Bar

use of com.opensymphony.xwork2.util.Bar in project struts by apache.

the class IntRangeValidatorTest method testRangeValidation.

public void testRangeValidation() {
    HashMap<String, String> params = new HashMap<>();
    params.put("bar", "5");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> errorMessages = errors.get("bar");
        assertEquals(1, errorMessages.size());
        String errorMessage = errorMessages.get(0);
        assertNotNull(errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 52 with Bar

use of com.opensymphony.xwork2.util.Bar in project struts by apache.

the class VisitorFieldValidatorTest method testVisitorChildConversionValidation.

public void testVisitorChildConversionValidation() throws Exception {
    // add conversion error
    Map<String, ConversionData> conversionErrors = new HashMap<>();
    conversionErrors.put("bean.child.count", new ConversionData("bar", Integer.class));
    ActionContext.getContext().withConversionErrors(conversionErrors);
    validate("visitorChildValidation");
    assertTrue(action.hasFieldErrors());
    Map<String, List<String>> fieldErrors = action.getFieldErrors();
    assertEquals(6, fieldErrors.size());
    assertFalse(fieldErrors.containsKey("bean.count"));
    assertTrue(fieldErrors.containsKey("bean.name"));
    assertTrue(fieldErrors.containsKey("bean.birth"));
    assertTrue(fieldErrors.containsKey("bean.child.name"));
    assertTrue(fieldErrors.containsKey("bean.child.birth"));
    // the error from the action should be there too
    assertTrue(fieldErrors.containsKey("context"));
    // nested visitor conversion error
    assertTrue(fieldErrors.containsKey("bean.child.count"));
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 53 with Bar

use of com.opensymphony.xwork2.util.Bar in project struts by apache.

the class SimpleActionValidationTest method testSubPropertiesAreValidated.

public void testSubPropertiesAreValidated() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("baz", "10");
    // valid values
    params.put("foo", "8");
    params.put("bar", "7");
    params.put("date", "12/23/2002");
    params.put("bean.name", "Name should be valid");
    // this should cause a message
    params.put("bean.count", "100");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, null, extraContext);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> beanCountErrors = errors.get("bean.count");
        assertEquals(1, beanCountErrors.size());
        String errorMessage = beanCountErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("bean.count out of range.", errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 54 with Bar

use of com.opensymphony.xwork2.util.Bar in project struts by apache.

the class SimpleActionValidationTest method testAliasValidation.

public void testAliasValidation() {
    Map<String, Object> params = new HashMap<>();
    params.put("baz", "10");
    // valid values
    params.put("bar", "7");
    params.put("date", "12/23/2002");
    params.put("percentage", "1.23456789");
    Map<String, Object> extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
        proxy.execute();
        ValidationAware validationAware = (ValidationAware) proxy.getAction();
        assertFalse(validationAware.hasFieldErrors());
        params.put("bar", "42");
        extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
        proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, null, extraContext);
        proxy.execute();
        validationAware = (ValidationAware) proxy.getAction();
        assertTrue(validationAware.hasFieldErrors());
        Map<String, List<String>> errors = validationAware.getFieldErrors();
        assertTrue(errors.containsKey("baz"));
        List<String> bazErrors = errors.get("baz");
        assertEquals(1, bazErrors.size());
        String message = bazErrors.get(0);
        assertEquals("baz out of range.", message);
        assertTrue(errors.containsKey("bar"));
        List<String> barErrors = errors.get("bar");
        assertEquals(1, barErrors.size());
        message = barErrors.get(0);
        assertEquals("bar must be between 6 and 10, current value is 42.", message);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 55 with Bar

use of com.opensymphony.xwork2.util.Bar in project struts by apache.

the class SimpleActionValidationTest method testLookingUpFieldNameAsTextKey.

public void testLookingUpFieldNameAsTextKey() {
    HashMap<String, Object> params = new HashMap<>();
    // should cause a message
    params.put("baz", "-1");
    // valid values
    params.put("bar", "7");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> bazErrors = errors.get("baz");
        assertEquals(1, bazErrors.size());
        String errorMessage = bazErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("Baz Field must be greater than 0", errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Aggregations

HashMap (java.util.HashMap)17 Bar (com.opensymphony.xwork2.util.Bar)14 Foo (com.opensymphony.xwork2.util.Foo)13 ValueStack (com.opensymphony.xwork2.util.ValueStack)12 ActionProxy (com.opensymphony.xwork2.ActionProxy)9 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)9 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)7 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)7 List (java.util.List)6 Map (java.util.Map)6 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)5 Mock (com.mockobjects.dynamic.Mock)4 ActionContext (com.opensymphony.xwork2.ActionContext)4 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 MockResult (com.opensymphony.xwork2.mock.MockResult)3 ModelDrivenAction2 (com.opensymphony.xwork2.test.ModelDrivenAction2)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 ActionSupport (com.opensymphony.xwork2.ActionSupport)2