XmlBeanMapper extends AbstractComponent implements ValidationEventHandler { public T loadXml(InputStream inputStream, Object source) { try { Object unmarshalledObject = getOrCreateUnmarshaller().unmarshal(inputStream); T jaxbBean = this.xmlBeanClass.cast(unmarshalledObject); validate(jaxbBean); return jaxbBean; } catch (JAXBException e) { throw new XmlInvalidException(e, source); } } XmlBeanMapper(Class<T> xmlBeanClass); @Inject void setStaxUtil(StaxUtil staxUtil); @Inject void setResourceFactory(BrowsableResourceFactory browsableResourceFactory); boolean isXIncludeAware(); void setXIncludeAware(boolean isXIncludeAware); T loadXml(InputStream inputStream, Object source); T loadXml(String locationUrl); T loadXml(File file); T loadXml(DataResource resource); void saveXml(T jaxbBean, DataResource resource); void saveXml(T jaxbBean, OutputStream outputStream); void saveXml(T jaxbBean, String locationUrl); @Override boolean handleEvent(ValidationEvent event);  }
@Test public void testLoadXmlWithInvalidIds() { String xml = "<?xml version='1.0'?><bean><node id='duplicate-id'/>" + "<node id='duplicate-id' parent='undefined-parent'/>" + "<node id='root' parent='unknown-parent'/></bean>"; XmlBeanMapper<XmlBean> mapper = new XmlBeanMapper<XmlBeanMapperTest.XmlBean>(XmlBean.class); mapper.initialize(); ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes()); try { XmlBean loadedBean = mapper.loadXml(inputStream, "test-case"); ExceptionHelper.failExceptionExpected(); } catch (Exception e) { String message = e.getMessage(); Assert.assertTrue(message.contains("duplicate-id")); Assert.assertTrue(message.contains("undefined-parent")); Assert.assertTrue(message.contains("unknown-parent")); } }