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));
}
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());
}
}
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);
}
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());
}
}
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());
}
Aggregations