Search in sources :

Example 11 with Foo

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

the class ConversionErrorInterceptorTest method testFieldErrorWithMapKeyAdded.

public void testFieldErrorWithMapKeyAdded() throws Exception {
    String fieldName = "foo['1'].intValue";
    conversionErrors.put(fieldName, new ConversionData("bar", int.class));
    ActionSupport action = new ActionSupport();
    mockInvocation.expectAndReturn("getAction", action);
    stack.push(action);
    mockInvocation.matchAndReturn("getAction", action);
    assertNull(action.getFieldErrors().get(fieldName));
    interceptor.doIntercept(invocation);
    // This fails!
    assertTrue(action.hasFieldErrors());
    assertNotNull(action.getFieldErrors().get(fieldName));
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 12 with Foo

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

the class OgnlUtilTest method testAvoidCallingMethodsOnObjectClass.

public void testAvoidCallingMethodsOnObjectClass() {
    Foo foo = new Foo();
    Exception expected = null;
    try {
        ognlUtil.setExcludedClasses(Object.class.getName());
        ognlUtil.setValue("class.classLoader.defaultAssertionStatus", ognlUtil.createDefaultContext(foo), foo, true);
        fail();
    } catch (OgnlException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertSame(NoSuchPropertyException.class, expected.getClass());
    assertEquals("com.opensymphony.xwork2.util.Foo.class", expected.getMessage());
}
Also used : OgnlException(ognl.OgnlException) Foo(com.opensymphony.xwork2.util.Foo) NoSuchPropertyException(ognl.NoSuchPropertyException) MethodFailedException(ognl.MethodFailedException) InappropriateExpressionException(ognl.InappropriateExpressionException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) IntrospectionException(java.beans.IntrospectionException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException)

Example 13 with Foo

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

the class OgnlUtilTest method testCopyEditable.

public void testCopyEditable() {
    Foo foo1 = new Foo();
    Foo foo2 = new Foo();
    Map<String, Object> context = ognlUtil.createDefaultContext(foo1);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.MAY);
    cal.set(Calendar.DAY_OF_MONTH, 29);
    cal.set(Calendar.YEAR, 2017);
    foo1.setTitle("blah");
    foo1.setNumber(1);
    foo1.setPoints(new long[] { 1, 2, 3 });
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);
    ognlUtil.copy(foo1, foo2, context, null, null, Bar.class);
    assertEquals(foo1.getTitle(), foo2.getTitle());
    assertEquals(0, foo2.getNumber());
    assertNull(foo2.getPoints());
    assertNull(foo2.getBirthday());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo) Calendar(java.util.Calendar)

Example 14 with Foo

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

the class OgnlUtilTest method testExpressionIsCachedIrrespectiveOfItsExecutionStatus.

public void testExpressionIsCachedIrrespectiveOfItsExecutionStatus() throws OgnlException {
    Foo foo = new Foo();
    OgnlContext context = (OgnlContext) ognlUtil.createDefaultContext(foo);
    // Expression which executes with success
    try {
        ognlUtil.getValue("@com.opensymphony.xwork2.ognl.OgnlUtilTest@STATIC_FINAL_PUBLIC_ATTRIBUTE", context, foo);
        assertEquals("Successfully executed expression must have been cached", ognlUtil.expressionCacheSize(), 1);
    } catch (Exception ex) {
        fail("Expression execution should have succeeded here. Exception: " + ex);
    }
    // Expression which executes with failure
    try {
        ognlUtil.getValue("@com.opensymphony.xwork2.ognl.OgnlUtilTest@STATIC_PRIVATE_ATTRIBUTE", context, foo);
        fail("Expression execution should have failed here");
    } catch (Exception ex) {
        assertEquals("Expression with failed execution must have been cached nevertheless", ognlUtil.expressionCacheSize(), 2);
    }
}
Also used : Foo(com.opensymphony.xwork2.util.Foo) NoSuchPropertyException(ognl.NoSuchPropertyException) MethodFailedException(ognl.MethodFailedException) InappropriateExpressionException(ognl.InappropriateExpressionException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) IntrospectionException(java.beans.IntrospectionException) StrutsException(org.apache.struts2.StrutsException) OgnlException(ognl.OgnlException) OgnlContext(ognl.OgnlContext)

Example 15 with Foo

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

the class OgnlUtilTest method testSetPropertiesString.

public void testSetPropertiesString() {
    Foo foo = new Foo();
    Map<String, Object> context = ognlUtil.createDefaultContext(foo);
    Map<String, Object> props = new HashMap<>();
    props.put("title", "this is a title");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(foo.getTitle(), "this is a title");
}
Also used : HashMap(java.util.HashMap) Foo(com.opensymphony.xwork2.util.Foo)

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