Java 为什么dom4j只在我的测试环境中为有效的XPath抛出InvalidXPathException?
我正在为一些在系统中运行的代码编写一些单元测试,但是在有效的XPath上抛出Java 为什么dom4j只在我的测试环境中为有效的XPath抛出InvalidXPathException?,java,xpath,easymock,dom4j,Java,Xpath,Easymock,Dom4j,我正在为一些在系统中运行的代码编写一些单元测试,但是在有效的XPath上抛出InvalidXPathException时遇到了问题 使用//external |//inline从DOM4J文档中提取某些元素,它可以在生产环境中工作,但不能在我的测试环境中工作。这不应该是一个问题,因为它是一个有效的XPath,我在环境之外测试过 任何帮助都将不胜感激 jUnit/Easymock测试: @Test public void testgetDCR_success(){ RequestCont
InvalidXPathException
时遇到了问题
使用//external |//inline
从DOM4J文档中提取某些元素,它可以在生产环境中工作,但不能在我的测试环境中工作。这不应该是一个问题,因为它是一个有效的XPath,我在环境之外测试过
任何帮助都将不胜感激
jUnit/Easymock测试:
@Test
public void testgetDCR_success(){
RequestContext context = EasyMock.createMock(RequestContext.class);
FileDal dal = EasyMock.createMock(FileDal.class);
String xmlContent = "<xml>your sample stuff</xml>";
Document sampleDoc = Dom4jUtils.newDocument(xmlContent);
InputStream stream = null;
try {
stream = new ByteArrayInputStream(xmlContent.getBytes("UTF-8"));
} catch(IOException e) {
Assert.fail("Could not open file stream for test.");
}
EasyMock.expect(context.getFileDal()).andReturn(dal).anyTimes();
EasyMock.expect(dal.getRoot()).andReturn("").anyTimes();
EasyMock.expect(dal.getSeparator()).andReturn('/').anyTimes();
EasyMock.expect(dal.exists("/some/path")).andReturn(true);
EasyMock.expect(dal.read("/some/path")).andReturn(xmlContent);
EasyMock.expect(dal.getStream("some/path")).andReturn(stream);
EasyMock.replay(context);
EasyMock.replay(dal);
Document doc = new DefaultTransformationService().getDCR(context, "some/path");
Assert.assertEquals(sampleDoc, doc);
}
@测试
public void testgetDCR_success(){
RequestContext=EasyMock.createMock(RequestContext.class);
FileDal=EasyMock.createMock(FileDal.class);
String xmlContent=“您的示例资料”;
Document sampleDoc=Dom4jUtils.newDocument(xmlContent);
InputStream=null;
试一试{
stream=newbytearrayinputstream(xmlContent.getBytes(“UTF-8”);
}捕获(IOE异常){
Assert.fail(“无法打开文件流进行测试”);
}
expect(context.getFileDal()).andReturn(dal.anyTimes();
expect(dal.getRoot()).andReturn(“”.anyTimes();
expect(dal.getSeparator()).andReturn('/').anyTimes();
expect(dal.exists(“/some/path”).andReturn(true);
expect(dal.read(“/some/path”).andReturn(xmlContent);
expect(dal.getStream(“some/path”)).andReturn(stream);
回放(上下文);
EasyMock.replay(dal);
Document doc=new DefaultTransformationService().getDCR(上下文,“some/path”);
Assert.assertEquals(sampleDoc,doc);
}
就这一问题而言:
@Override
public Document getDCR(RequestContext context, String relativePath) {
LOGGER.debug(">> getDCR");
if (StringUtils.isBlank(relativePath)) {
LOGGER.error("No origin file path given");
LOGGER.debug("<< getDCR");
return null;
}
Document dcrDoc = null;
try {
dcrDoc = ExternalUtils.readXmlFile(context, relativePath);
}catch (RuntimeException e){
LOGGER.error("No DCR found at file path: "+relativePath,e);
LOGGER.debug("<< getDCR");
return null;
}
if (dcrDoc == null) {
LOGGER.error("Unable to open xml file: " + relativePath);
LOGGER.debug("<< getDCR");
return null;
}
LOGGER.debug("<< getDCR");
Set<String> parsedPaths = new HashSet<String>();
parsedPaths.add(relativePath);
return parseData(context, dcrDoc, parsedPaths);
}
private Document parseData(RequestContext context, Document dcr, Set<String> parsedPaths) {
LOGGER.debug(">> parseData");
// get all nodes that would be converted.
@SuppressWarnings("unchecked")
List<Element> elements = dcr.selectNodes("//external|//inline");
// For each of the nodes present within the document that are of type external or inline
for (Element element : elements) {
// process each element in the list of selected elements.
processElement(context, element, parsedPaths);
}
LOGGER.debug("<< parseData");
return dcr;
}
@覆盖
公共文档getDCR(RequestContext上下文,字符串relativePath){
LOGGER.debug(“>>getDCR”);
if(StringUtils.isBlank(relativePath)){
LOGGER.error(“未给出原始文件路径”);
debug(“>parseData”);
//获取要转换的所有节点。
@抑制警告(“未选中”)
List elements=dcr.selectNodes(“//external |//inline”);
//对于文档中存在的外部或内联类型的每个节点
for(元素:元素){
//处理选定元素列表中的每个元素。
processElement(上下文、元素、解析路径);
}
debug(“查看DOM4J源代码,可以发现该异常的根本原因不一定是无效的XPath表达式:
protected static XPath parse(String text) {
try {
return new Dom4jXPath(text);
} catch (JaxenException e) {
throw new InvalidXPathException(text, e.getMessage());
} catch (Throwable t) {
throw new InvalidXPathException(text, t);
}
}
考虑到消息的外观,我们可以得出这样的结论:Throwable
被捕获了
public InvalidXPathException(String xpath, Throwable t) {
super("Invalid XPath expression: '" + xpath
+ "'. Caused by: " + t.getMessage());
}
不幸的是,DOM4J在本例中隐藏了原始异常,但是
Caused by: org/jaxen/dom4j/Dom4jXPath
表示原始异常是NoClassDefFoundError
然而,奇怪的是,当JaxenException明显被找到时,Dom4jXPath却找不到(因为它们位于同一个jar(jaxen))中
顺便说一句,前面的“分析”是基于DOM4J 1.6.1的,因此如果您使用另一个版本的YMMV。会/*[self::external或self::inline]
解决dom4j在原始XPath形式上存在的问题?dom4j在XPath本身上没有任何问题。它在使用给定XPath的生产环境中运行良好。在测试环境中,它甚至会遇到一个基本的“//external”“但是,路径。那么,在不知道您的生产环境和测试环境之间有什么不同的情况下,我们如何提供帮助?您必须提供一个复制您在测试环境中遇到的问题的路径。使用上下文的附加代码进行更新。提供了我所能提供的代码,而不会过于冗长。”
Caused by: org/jaxen/dom4j/Dom4jXPath