Search in sources :

Example 26 with Foo

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

the class AnnotationXWorkConverterTest method testStringToCustomTypeUsingCustomConverter.

public void testStringToCustomTypeUsingCustomConverter() {
    // the converter needs to be registered as the Bar.class converter
    // it won't be detected from the Foo-conversion.properties
    // because the Foo-conversion.properties file is only used when converting a property of Foo
    converter.registerConverter(Bar.class.getName(), new FooBarConverter());
    Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
    assertNotNull("conversion failed", bar);
    assertEquals(123, bar.getSomethingElse());
    assertEquals("blah", bar.getTitle());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar)

Example 27 with Foo

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

the class SimpleActionValidationTest method testMessageKey.

public void testMessageKey() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("foo", "200");
    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);
        ValueStack stack = ActionContext.getContext().getValueStack();
        stack.getActionContext().withLocale(Locale.US);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> fooErrors = errors.get("foo");
        assertEquals(1, fooErrors.size());
        String errorMessage = fooErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("Foo Range Message", errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 28 with Foo

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

the class ConversionErrorFieldValidatorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, ConversionData> conversionErrors = new HashMap<>();
    conversionErrors.put("foo", new ConversionData("bar", Integer.class));
    ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
    validator = new ConversionErrorFieldValidator();
    validationAware = new ValidationAwareSupport();
    DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware, container.getInstance(TextProviderFactory.class));
    stack.push(validatorContext);
    validator.setValidatorContext(validatorContext);
    validator.setFieldName("foo");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    assertEquals(0, validationAware.getFieldErrors().size());
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ConversionErrorFieldValidator(com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)

Example 29 with Foo

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

the class ValidatorAnnotationTest method testNotAnnotatedMethodSuccess.

public void testNotAnnotatedMethodSuccess() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    params.put("date", "12/23/2002");
    params.put("foo", "5");
    params.put("bar", "7");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", "notAnnotatedMethod", null, extraContext);
    proxy.execute();
    assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
    Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
    assertEquals(0, errors.size());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) Collection(java.util.Collection) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 30 with Foo

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

the class VisitorFieldValidatorTest method testArrayValidation.

public void testArrayValidation() throws Exception {
    TestBean[] beanArray = action.getTestBeanArray();
    TestBean testBean = beanArray[0];
    testBean.setName("foo");
    validate("validateArray");
    assertTrue(action.hasFieldErrors());
    Map<String, List<String>> fieldErrors = action.getFieldErrors();
    // 4 errors for the array, one for context
    assertEquals(5, fieldErrors.size());
    assertTrue(fieldErrors.containsKey("testBeanArray[1].name"));
    // the error from the action should be there too
    assertTrue(fieldErrors.containsKey("context"));
    List<String> errors = fieldErrors.get("testBeanArray[1].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[2].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[3].name");
    assertEquals(1, errors.size());
    errors = fieldErrors.get("testBeanArray[4].name");
    assertEquals(1, errors.size());
}
Also used : TestBean(com.opensymphony.xwork2.TestBean) List(java.util.List)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)53 HashMap (java.util.HashMap)32 ValueStack (com.opensymphony.xwork2.util.ValueStack)23 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)20 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)19 StrutsException (org.apache.struts2.StrutsException)19 OgnlException (ognl.OgnlException)18 IntrospectionException (java.beans.IntrospectionException)17 InappropriateExpressionException (ognl.InappropriateExpressionException)17 MethodFailedException (ognl.MethodFailedException)17 NoSuchPropertyException (ognl.NoSuchPropertyException)17 ActionProxy (com.opensymphony.xwork2.ActionProxy)16 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)9 ActionContext (com.opensymphony.xwork2.ActionContext)8 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)8 Bar (com.opensymphony.xwork2.util.Bar)8 Map (java.util.Map)8 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)7 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)7 StubValueStack (com.opensymphony.xwork2.StubValueStack)6