01: package org.vraptor.converter.joda;
02:
03: import java.util.Arrays;
04: import java.util.Date;
05: import java.util.GregorianCalendar;
06: import java.util.Locale;
07:
08: import org.joda.time.YearMonthDay;
09: import org.vraptor.AbstractTest;
10: import org.vraptor.LogicRequest;
11: import org.vraptor.converter.ConversionException;
12:
13: public class YearMonthDayConverterTest extends AbstractTest {
14: public void testCanConvert() {
15: assertTrue(Arrays.deepEquals(new YearMonthDayConverter()
16: .getSupportedTypes(),
17: new Class[] { YearMonthDay.class }));
18: }
19:
20: public void testConvertNull() throws ConversionException {
21: LogicRequest context = getSetContext();
22: Object convertedNull = new YearMonthDayConverter().convert(
23: null, YearMonthDay.class, context);
24: Object convertedEmpty = new YearMonthDayConverter().convert("",
25: YearMonthDay.class, context);
26: assertNull("Null", convertedNull);
27: assertNull("Empty", convertedEmpty);
28: }
29:
30: public void testConvert() throws ConversionException {
31: LogicRequest context = getSetContext();
32: YearMonthDay expected = new YearMonthDay(1970, 1, 1);
33: assertEquals(expected, new YearMonthDayConverter().convert(
34: "1/1/1970", YearMonthDay.class, context));
35: }
36:
37: public void testConvertFromCalendar() throws ConversionException {
38: LogicRequest context = getSetContext();
39: GregorianCalendar calendar = new GregorianCalendar(1970, 0, 1);
40: YearMonthDay expectedFromCalendar = YearMonthDay
41: .fromCalendarFields(calendar);
42: Object converted = new YearMonthDayConverter().convert(
43: "1/1/1970", YearMonthDay.class, context);
44: assertEquals(expectedFromCalendar, converted);
45: }
46:
47: public void testConvertFromDate() throws ConversionException {
48: LogicRequest context = getSetContext();
49: Date date = new GregorianCalendar(1970, 0, 1).getTime();
50: YearMonthDay expectedFromDate = YearMonthDay
51: .fromDateFields(date);
52: Object converted = new YearMonthDayConverter().convert(
53: "1/1/1970", YearMonthDay.class, context);
54: assertEquals(expectedFromDate, converted);
55: }
56:
57: private LogicRequest getSetContext() {
58: LogicRequest context = createLogicRequest();
59: context.getSessionContext().setAttribute(
60: "javax.servlet.jsp.jstl.fmt.locale.session",
61: Locale.ENGLISH);
62: return context;
63: }
64:
65: public void testConvertionError() {
66: LogicRequest context = getSetContext();
67: try {
68: new YearMonthDayConverter().convert("XX:YY",
69: YearMonthDay.class, context);
70: fail();
71: } catch (ConversionException e) {
72: // ok
73: }
74: }
75:
76: }
|