Search in sources :

Example 46 with Foo

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

the class OgnlUtilTest method testNullProperties.

public void testNullProperties() {
    Foo foo = new Foo();
    foo.setALong(88);
    Map<String, Object> context = ognlUtil.createDefaultContext(foo);
    ognlUtil.setProperties(null, foo, context);
    assertEquals(88, foo.getALong());
    Map<String, Object> props = new HashMap<>();
    props.put("ALong", "99");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(99, foo.getALong());
}
Also used : HashMap(java.util.HashMap) Foo(com.opensymphony.xwork2.util.Foo)

Example 47 with Foo

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

the class OgnlUtilTest method testAvoidCallingMethodsOnObjectClassAsMapWithQuotes.

public void testAvoidCallingMethodsOnObjectClassAsMapWithQuotes() {
    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 48 with Foo

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

the class OgnlUtilTest method testSetPropertiesDate.

public void testSetPropertiesDate() {
    Foo foo = new Foo();
    Map<String, Object> context = ognlUtil.createDefaultContext(foo);
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(new HashMap<>()));
    Map<String, Object> props = new HashMap<>();
    props.put("birthday", "02/12/1982");
    // US style test
    context = ActionContext.of(context).withLocale(Locale.US).withValueStack(stack).getContextMap();
    ognlUtil.setProperties(props, foo, context);
    Calendar cal = Calendar.getInstance(Locale.US);
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);
    assertEquals(cal.getTime(), foo.getBirthday());
    // UK style test
    cal = Calendar.getInstance(Locale.UK);
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.OCTOBER);
    cal.set(Calendar.DAY_OF_MONTH, 18);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 23);
    cal.set(Calendar.SECOND, 45);
    Date eventTime = cal.getTime();
    String formatted = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK).format(eventTime);
    props.put("event", formatted);
    cal = Calendar.getInstance(Locale.UK);
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 9);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 30);
    Date meetingTime = cal.getTime();
    formatted = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK).format(meetingTime);
    props.put("meeting", formatted);
    context = ActionContext.of(context).withLocale(Locale.UK).getContextMap();
    ognlUtil.setProperties(props, foo, context);
    assertEquals(eventTime, foo.getEvent());
    assertEquals(meetingTime, foo.getMeeting());
    // test RFC 3339 date format for JSON
    props.put("event", "1996-12-19T16:39:57Z");
    context = ActionContext.of(context).withLocale(Locale.US).getContextMap();
    ognlUtil.setProperties(props, foo, context);
    cal = Calendar.getInstance(Locale.US);
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 19);
    cal.set(Calendar.YEAR, 1996);
    cal.set(Calendar.HOUR_OF_DAY, 16);
    cal.set(Calendar.MINUTE, 39);
    cal.set(Calendar.SECOND, 57);
    assertEquals(cal.getTime(), foo.getEvent());
    // test setting a calendar property
    props.put("calendar", "1996-12-19T16:39:57Z");
    context = ActionContext.of(context).withLocale(Locale.US).getContextMap();
    ognlUtil.setProperties(props, foo, context);
    assertEquals(cal, foo.getCalendar());
}
Also used : StubValueStack(com.opensymphony.xwork2.StubValueStack) StubTextProvider(com.opensymphony.xwork2.StubTextProvider) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) Foo(com.opensymphony.xwork2.util.Foo) Calendar(java.util.Calendar) Date(java.util.Date)

Example 49 with Foo

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

the class OgnlUtilTest method testCallMethod.

public void testCallMethod() {
    Foo foo = new Foo();
    Exception expected = null;
    try {
        ognlUtil.callMethod("#booScope=@myclass@DEFAULT_SCOPE,#bootScope.init()", ognlUtil.createDefaultContext(foo), foo);
        fail();
    } catch (OgnlException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertSame(OgnlException.class, expected.getClass());
    assertEquals(expected.getMessage(), "It isn't a simple method which can be called!");
}
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 50 with Foo

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

the class OgnlUtilTest method testAllowCallingMethodsOnObjectClassInDevModeFalse.

public void testAllowCallingMethodsOnObjectClassInDevModeFalse() {
    Exception expected = null;
    try {
        ognlUtil.setExcludedClasses(Foo.class.getName());
        ognlUtil.setDevModeExcludedClasses("");
        ognlUtil.setDevMode(Boolean.FALSE.toString());
        Foo foo = new Foo();
        String result = (String) ognlUtil.getValue("toString", ognlUtil.createDefaultContext(foo), foo, String.class);
        assertEquals("Foo", result);
    } catch (OgnlException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertSame(NoSuchPropertyException.class, expected.getClass());
    assertEquals("com.opensymphony.xwork2.util.Foo.toString", 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)

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