JAXP - XSLT 偶发测试失败
下面所附的代码会产生零星的测试失败 (JUnit),在 80% 的时间内有效。 我正在使用静态模板对象。如果出现故障,则会将与预期不同的 JAXB 对象放入 Transformer.transform(jaxbSource, result) 方法调用的“结果”中。
我尝试过锁定和同步部分,但没有成功。此外,根据规范,模板对象应该是线程安全的。转变过程中发生了一些奇怪的事情。
错误症状:JUnit 测试失败 - 突然从转换返回错误的对象。
有什么想法吗?
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您有多个类映射到相同的根元素名称,因此您需要将希望解组的类型作为参数传递给解组操作。这意味着转换为中间表示形式,例如:DOM、byte[]、String 等:
有关详细信息:
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: