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.TimeOfDay;
09: import org.vraptor.AbstractTest;
10: import org.vraptor.LogicRequest;
11: import org.vraptor.converter.ConversionException;
12:
13: public class TimeOfDayConverterTest extends AbstractTest {
14: public void testCanConvert() {
15: assertTrue(Arrays.deepEquals(new TimeOfDayConverter()
16: .getSupportedTypes(), new Class[] { TimeOfDay.class }));
17: }
18:
19: public void testConvertNull() throws ConversionException {
20: LogicRequest context = getSetContext();
21: Object convertedNull = new TimeOfDayConverter().convert(null,
22: TimeOfDay.class, context);
23: Object convertedEmpty = new TimeOfDayConverter().convert("",
24: TimeOfDay.class, context);
25: assertNull("Null", convertedNull);
26: assertNull("Empty", convertedEmpty);
27: }
28:
29: public void testConvert() throws ConversionException {
30: LogicRequest context = getSetContext();
31: TimeOfDay expectedTime = new TimeOfDay(11, 22);
32: Object converted = new TimeOfDayConverter().convert("11:22 am",
33: TimeOfDay.class, context);
34: assertEquals(expectedTime, converted);
35: }
36:
37: public void testConvertFromCalendar() throws ConversionException {
38: LogicRequest context = getSetContext();
39: GregorianCalendar calendar = new GregorianCalendar(1970, 0, 1,
40: 11, 22);
41: TimeOfDay expectedTimeFromCalendar = TimeOfDay
42: .fromCalendarFields(calendar);
43: Object converted = new TimeOfDayConverter().convert("11:22 am",
44: TimeOfDay.class, context);
45: assertEquals(expectedTimeFromCalendar, converted);
46: }
47:
48: public void testConvertFromDate() throws ConversionException {
49: LogicRequest context = getSetContext();
50: Date date = new GregorianCalendar(1970, 0, 1, 11, 22).getTime();
51: TimeOfDay expectedTimeFromDate = TimeOfDay.fromDateFields(date);
52: Object converted = new TimeOfDayConverter().convert("11:22 am",
53: TimeOfDay.class, context);
54: assertEquals(expectedTimeFromDate, 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 TimeOfDayConverter().convert("XX:YY", TimeOfDay.class,
69: context);
70: fail();
71: } catch (ConversionException e) {
72: // ok
73: }
74: }
75: }
|