Search in sources :

Example 1 with StubTextProvider

use of com.opensymphony.xwork2.StubTextProvider in project struts by apache.

the class DateConverterTest method testDateType.

public void testDateType() {
    DateConverter converter = new DateConverter();
    Map<String, String> map = new HashMap<>();
    map.put(org.apache.struts2.components.Date.DATETAG_PROPERTY, "yyyy-MM-dd");
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(map));
    ActionContext context = ActionContext.of(new HashMap<>()).withLocale(new Locale("es_MX", "MX")).withValueStack(stack);
    Object value = converter.convertValue(context.getContextMap(), null, null, null, DATE_STR, Date.class);
    assertTrue(value.toString().startsWith(DATE_CONVERTED));
}
Also used : Locale(java.util.Locale) 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) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 2 with StubTextProvider

use of com.opensymphony.xwork2.StubTextProvider in project struts by apache.

the class DateConverterTest method testTypeConversionExceptionWhenParseError.

public void testTypeConversionExceptionWhenParseError() {
    DateConverter converter = new DateConverter();
    Map<String, String> map = new HashMap<>();
    map.put(org.apache.struts2.components.Date.DATETAG_PROPERTY, "yyyy-MM-dd");
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(map));
    ActionContext context = ActionContext.of(new HashMap<>()).withLocale(new Locale("es_MX", "MX")).withValueStack(stack);
    try {
        converter.convertValue(context.getContextMap(), null, null, null, INVALID_DATE, Date.class);
        fail("TypeConversionException expected - Conversion error occurred");
    } catch (Exception ex) {
        assertEquals(TypeConversionException.class, ex.getClass());
        assertEquals(MESSAGE_PARSE_ERROR, ex.getMessage());
    }
}
Also used : Locale(java.util.Locale) 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) TypeConversionException(org.apache.struts2.conversion.TypeConversionException) ActionContext(com.opensymphony.xwork2.ActionContext) TypeConversionException(org.apache.struts2.conversion.TypeConversionException)

Example 3 with StubTextProvider

use of com.opensymphony.xwork2.StubTextProvider in project struts by apache.

the class XWorkBasicConverterTest method testDateWithLocaleFrance.

public void testDateWithLocaleFrance() {
    Map<String, String> map = new HashMap<>();
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(map));
    Locale locale = new Locale("fr", "FR");
    ActionContext context = ActionContext.of(new HashMap<>()).withLocale(locale).withValueStack(stack);
    String reference = "09/01/2009";
    Object convertedObject = basicConverter.convertValue(context.getContextMap(), null, null, null, reference, Date.class);
    assertNotNull(convertedObject);
    compareDates(locale, convertedObject);
}
Also used : StubValueStack(com.opensymphony.xwork2.StubValueStack) StubTextProvider(com.opensymphony.xwork2.StubTextProvider) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 4 with StubTextProvider

use of com.opensymphony.xwork2.StubTextProvider in project struts by apache.

the class XWorkBasicConverterTest method testDateConversionWithInvalidValue.

public void testDateConversionWithInvalidValue() {
    Map<String, String> map = new HashMap<>();
    map.put(org.apache.struts2.components.Date.DATETAG_PROPERTY, "yyyy-MM-dd");
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(map));
    ActionContext context = ActionContext.of(new HashMap<>()).withLocale(new Locale("es_MX", "MX")).withValueStack(stack);
    try {
        basicConverter.convertValue(context.getContextMap(), null, null, null, "asdsd", Date.class);
        fail("StrutsException expected - conversion error occurred");
    } catch (StrutsException e) {
        assertEquals("Could not parse date", e.getMessage());
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) StubValueStack(com.opensymphony.xwork2.StubValueStack) StubTextProvider(com.opensymphony.xwork2.StubTextProvider) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 5 with StubTextProvider

use of com.opensymphony.xwork2.StubTextProvider 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)

Aggregations

StubTextProvider (com.opensymphony.xwork2.StubTextProvider)7 StubValueStack (com.opensymphony.xwork2.StubValueStack)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)7 ActionContext (com.opensymphony.xwork2.ActionContext)6 HashMap (java.util.HashMap)3 Locale (java.util.Locale)2 Foo (com.opensymphony.xwork2.util.Foo)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 StrutsException (org.apache.struts2.StrutsException)1 TypeConversionException (org.apache.struts2.conversion.TypeConversionException)1