I'm working on authoring some unit testing for some code that I've got working in a system, but I'm having problems with InvalidXPathException
's being thrown on valid XPaths.
Using //external|//inline
to extract out certain elements out of a DOM4J document, and it works in production but not in my test environment. There shouldn't be an issue as it's a valid XPath that I've tested outside of the environment.
Any help would be appreciated!
jUnit/Easymock test:
@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);
}
Lead up to the issue:
@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;
}
Stack trace:
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)
Looking at the DOM4J source code, one can see that the root cause for that exception is not necessarily an invalid XPath expression:
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);
}
}
And given how the message looks, one can conclude that Throwable
was catched. From InvalidXPathException
:
public InvalidXPathException(String xpath, Throwable t) {
super("Invalid XPath expression: '" + xpath
+ "'. Caused by: " + t.getMessage());
}
Unfortunately, DOM4J hides the original exception in this case, but
Caused by: org/jaxen/dom4j/Dom4jXPath
implies that the original exception is a NoClassDefFoundError.
However, it is strange that that Dom4jXPath cannot be found while JaxenException is obviously found (since they live in the same jar (jaxen)). Anyway, it looks like your classpath is not set up properly.
BTW, the preceding "analysis" is based on DOM4J 1.6.1., so if you use another version YMMV.