XMLGregorianCalendarConverter implements Converter { public Object convert(Class destClass, Object srcObj) { Class sourceClass = srcObj.getClass(); Calendar result = new GregorianCalendar(); if (java.util.Date.class.isAssignableFrom(sourceClass)) { result.setTime(java.util.Date.class.cast(srcObj)); } else if (Calendar.class.isAssignableFrom(sourceClass)) { Calendar c = Calendar.class.cast(srcObj); result.setTime(c.getTime()); result.setTimeZone(c.getTimeZone()); } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) { result = XMLGregorianCalendar.class.cast(srcObj).toGregorianCalendar(); } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) { if ("".equals(String.class.cast(srcObj))) { return null; } try { long time = dateFormat.parse(String.class.cast(srcObj)).getTime(); result.setTime(new Date(time)); } catch (ParseException e) { throw new ConversionException("Unable to parse source object using specified date format", e); } } else { try { long time = Long.parseLong(srcObj.toString()); result.setTime(new Date(time)); } catch (NumberFormatException e) { throw new ConversionException("Unable to determine time in millis of source object", e); } } if (dateFormat != null && String.class.isAssignableFrom(destClass)) { return dateFormat.format(result.getTime()); } return dataTypeFactory().newXMLGregorianCalendar(GregorianCalendar.class.cast(result)); } XMLGregorianCalendarConverter(DateFormat dateFormat); Object convert(Class destClass, Object srcObj);  }
@Test public void testConvert_Date() { Calendar calendar = Calendar.getInstance(); calendar.set(YEAR, MONTH, DAY); Date date = calendar.getTime(); Object result = converter.convert(XMLGregorianCalendar.class, date); XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar)result; assertEquals(YEAR, xmlCalendar.getYear()); assertEquals(MONTH + 1, xmlCalendar.getMonth()); assertEquals(DAY, xmlCalendar.getDay()); }
@Test public void testConvert_Calendar() { Calendar calendar = Calendar.getInstance(); calendar.set(YEAR, MONTH, DAY); Object result = converter.convert(XMLGregorianCalendar.class, calendar); XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar)result; assertEquals(YEAR, xmlCalendar.getYear()); assertEquals(MONTH + 1, xmlCalendar.getMonth()); assertEquals(DAY, xmlCalendar.getDay()); }
@Test public void testConvert_String() { Object result = converter.convert(XMLGregorianCalendar.class, "04.07.1983"); XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar)result; assertEquals(YEAR, xmlCalendar.getYear()); assertEquals(MONTH - 1, xmlCalendar.getMonth()); assertEquals(DAY, xmlCalendar.getDay()); }
@Test public void testConvert_XmlGregorianCalendar() throws Exception { DatatypeFactory instance = DatatypeFactory.newInstance(); XMLGregorianCalendar calendar = instance.newXMLGregorianCalendar(new GregorianCalendar(YEAR, MONTH, DAY)); Object result = converter.convert(XMLGregorianCalendar.class, calendar); XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar)result; assertEquals(YEAR, xmlCalendar.getYear()); assertEquals(MONTH + 1, xmlCalendar.getMonth()); assertEquals(DAY, xmlCalendar.getDay()); }
@Test public void testConvert_EmptyString() { Object result = converter.convert(XMLGregorianCalendar.class, ""); assertNull(result); }
@Test public void testConvert_Format() throws Exception { DatatypeFactory instance = DatatypeFactory.newInstance(); XMLGregorianCalendar calendar = instance.newXMLGregorianCalendar(new GregorianCalendar(YEAR, MONTH, DAY)); Object result = converter.convert(String.class, calendar); String stringCalendar = (String)result; assertEquals(stringCalendar, "04.09.1983"); }