The code attached below produces sporadic test failures (JUnit), works 80% of the time.
I’m using a static Templates object. In the case of failure a different than expected JAXB object is placed into ‘result’ from the transformer.transform(jaxbSource, result) method call.
I’ve tried locks and synchronizes sections in vain. Also the Templates object is supposed to be thread safe according to spec. Something weird is happening in the transform.
Error symptom: JUnit test failure – suddenly the wrong object is returned from the transform.
Any ideas?
private <S, T> S transform(final Templates template, final Class resultClass, final T data) throws JAXBException, TransformerException {
Transformer transformer = template.newTransformer();
final JAXBSource jaxbSource = new JAXBSource(getCachedJAXBContext(data.getClass()), data);
final Result result = new JAXBResult(getCachedJAXBContext(resultClass));
transformer.transform(jaxbSource, result);
return (S) ((JAXBResult) result).getResult();
}
Since you have multiple classes mapped to the same root element name, you need to pass the type you wish to unmarshal as a parameter to the unmarshal operation. This will mean transforming to an intermediate representation such as: DOM, byte[], String, etc:
For more information: