JAXBElementConverter implements Converter { @Override public Object convert(Class type, Object value) { Object result; Object factory = objectFactory(destObjClass, beanContainer); Class<?> factoryClass = factory.getClass(); Class<?> destClass = value.getClass(); Class<?> valueClass = value.getClass(); String methodName = "create" + destObjClass.substring(destObjClass.lastIndexOf(".") + 1) + StringUtils.capitalize(destFieldName); Method method = null; try { method = ReflectionUtils.findAMethod(factoryClass, methodName, beanContainer); Class<?>[] parameterTypes = method.getParameterTypes(); for (Class<?> parameterClass : parameterTypes) { if (!valueClass.equals(parameterClass)) { destClass = parameterClass; break; } } Class<?>[] paramTypes = {destClass}; method = ReflectionUtils.getMethod(factoryClass, methodName, paramTypes); } catch (NoSuchMethodException e) { MappingUtils.throwMappingException(e); } Object param = value; Converter converter; if (java.util.Date.class.isAssignableFrom(valueClass) && !destClass.equals(XMLGregorianCalendar.class)) { converter = new DateConverter(dateFormat); param = converter.convert(destClass, param); } else if (java.util.Calendar.class.isAssignableFrom(valueClass) && !destClass.equals(XMLGregorianCalendar.class)) { converter = new CalendarConverter(dateFormat); param = converter.convert(destClass, param); } else if (XMLGregorianCalendar.class.isAssignableFrom(valueClass) || XMLGregorianCalendar.class.isAssignableFrom(destClass)) { converter = new XMLGregorianCalendarConverter(dateFormat); param = converter.convert(destClass, param); } Object[] paramValues = {param}; result = ReflectionUtils.invoke(method, factory, paramValues); return result; } JAXBElementConverter(String destObjClass, String destFieldName, DateFormat dateFormat, BeanContainer beanContainer); @Override Object convert(Class type, Object value); String getBeanId();  }
@Test public void stringToJAXBElementConversion() { converter = new JAXBElementConverter(EmployeeWithInnerClass.class.getCanonicalName(), "parentName", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); Object conversion = converter.convert(JAXBElement.class, "dummy"); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("java.lang.String", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); assertEquals("dummy", JAXBElement.class.cast(conversion).getValue()); }
@Test public void xmlgregoriancalendarToJAXBElementConversion() throws Exception { converter = new JAXBElementConverter(EmployeeType.class.getCanonicalName(), "birthDate", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); DatatypeFactory instance = DatatypeFactory.newInstance(); XMLGregorianCalendar calendar = instance.newXMLGregorianCalendar(new GregorianCalendar(2001, 1, 1)); Object conversion = converter.convert(JAXBElement.class, calendar); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("javax.xml.datatype.XMLGregorianCalendar", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); assertEquals(calendar.toString(), JAXBElement.class.cast(conversion).getValue().toString()); }
@Test public void calendarToJAXBElementConversion() throws Exception { converter = new JAXBElementConverter(EmployeeType.class.getCanonicalName(), "birthDate", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); Calendar calendar = new GregorianCalendar(2001, 1, 1); Object conversion = converter.convert(JAXBElement.class, calendar); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("javax.xml.datatype.XMLGregorianCalendar", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); DatatypeFactory instance = DatatypeFactory.newInstance(); XMLGregorianCalendar expected = instance.newXMLGregorianCalendar(new GregorianCalendar(2001, 1, 1)); assertEquals(expected.toString(), JAXBElement.class.cast(conversion).getValue().toString()); }
@Test public void dateToJAXBElementConversion() throws Exception { converter = new JAXBElementConverter(EmployeeType.class.getCanonicalName(), "birthDate", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); Date calendar = new Date(YEAR, MONTH, DAY); Object conversion = converter.convert(JAXBElement.class, calendar); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("javax.xml.datatype.XMLGregorianCalendar", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); DatatypeFactory instance = DatatypeFactory.newInstance(); XMLGregorianCalendar expected = instance.newXMLGregorianCalendar(new GregorianCalendar(2001, 1, 1)); assertEquals(expected.toString(), JAXBElement.class.cast(conversion).getValue().toString()); }
@Test public void dateToJAXBElementStringConversion() { converter = new JAXBElementConverter(EmployeeWithInnerClass.class.getCanonicalName(), "parentName", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); Date calendar = new Date(YEAR, MONTH, DAY); Object conversion = converter.convert(JAXBElement.class, calendar); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("java.lang.String", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); assertEquals("01.02.2001", JAXBElement.class.cast(conversion).getValue()); }
@Test public void calendarToJAXBElementStringConversion() { converter = new JAXBElementConverter(EmployeeWithInnerClass.class.getCanonicalName(), "parentName", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); Calendar calendar = new GregorianCalendar(2001, 1, 1); Object conversion = converter.convert(JAXBElement.class, calendar); assertNotNull(conversion); assertEquals("javax.xml.bind.JAXBElement", conversion.getClass().getCanonicalName()); assertEquals("java.lang.String", JAXBElement.class.cast(conversion).getDeclaredType().getCanonicalName()); assertEquals("01.02.2001", JAXBElement.class.cast(conversion).getValue()); }
@Test(expected = MappingException.class) public void createFieldNotFoundException() { converter = new JAXBElementConverter(EmployeeWithInnerClass.class.getCanonicalName(), "vds", new SimpleDateFormat("dd.MM.yyyy"), beanContainer); converter.convert(JAXBElement.class, "dummy"); }