我正在爲某些代碼編寫一些單元測試,這些代碼是在系統中工作的,但我在InvalidXPathException
上遇到了有效XPath的問題。爲什麼dom4j僅在我的測試環境中爲有效的XPath拋出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);
}
攻向問題:
@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;
}
堆棧跟蹤:
org.dom4j.InvalidXPathException: Invalid XPath expression: '//external|//inline'. Caused by: org/jaxen/dom4j/Dom4jXPath
at org.dom4j.xpath.DefaultXPath.parse(DefaultXPath.java:362)
at org.dom4j.xpath.DefaultXPath.<init>(DefaultXPath.java:59)
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at com.sample.project.service.impl.DefaultTransformationService.parseData(DefaultTransformationService.java:163)
at com.sample.project.service.impl.DefaultTransformationService.getDCR(DefaultTransformationService.java:144)
at com.sample.project.service.impl.DefaultTransformationServiceTest.testgetDCR_success(DefaultTransformationServiceTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
'// * [self :: external or self :: inline]'是否解決了dom4j與XPath的原始形式有關的問題? – kjhughes
Dom4j與xpath本身沒有任何問題。它在給定xpath的生產環境中工作得很好。然而,在測試環境中,即使是基本的「//外部」路徑也會崩潰。 –
那麼,如果我們不知道您的生產環境和測試環境之間有什麼不同,我們該如何提供幫助?你必須提供一個[mcve]來重現你在測試環境中遇到的問題。 – kjhughes