MetadataXpathQuery extends MetadataXpathQueryImpl implements XpathQuery { @Override public MetadataElement resolveXpath(Document doc, XPath xpath, String expr) throws Exception { return new MetadataElement(getMetadataKey(), XpathQueryHelper.resolveSingleTextItem(doc, xpath, expr, allowEmptyResults())); } MetadataXpathQuery();  MetadataXpathQuery(String metadataKey, String xpathMetadataKey); @Override MetadataElement resolveXpath(Document doc, XPath xpath, String expr);  }
@Test public void testResolveXpath_EmptyResults_NotAllowed() throws Exception { MetadataXpathQuery query = init(create()); Document doc = XmlHelper.createDocument(XML); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML); msg.addMetadata("xpathMetadataKey", " try { MetadataElement result = query.resolveXpath(doc, new XPath(), query.createXpathQuery(msg)); fail(); } catch (CoreException expected) { } }
@Test public void testResolveXpath_EmptyResults_Allowed() throws Exception { MetadataXpathQuery query = init(create()); query.setAllowEmptyResults(Boolean.TRUE); Document doc = XmlHelper.createDocument(XML); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML); msg.addMetadata("xpathMetadataKey", " MetadataElement result = query.resolveXpath(doc, new XPath(), query.createXpathQuery(msg)); assertEquals("", result.getValue()); }
@Test public void testResolveXpath_Attribute() throws Exception { MetadataXpathQuery query = init(create()); Document doc = XmlHelper.createDocument(XML); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML); msg.addMetadata("xpathMetadataKey", " MetadataElement result = query.resolveXpath(doc, new XPath(), query.createXpathQuery(msg)); }
@Test public void testResolveXpath_function() throws Exception { MetadataXpathQuery query = init(create()); Document doc = XmlHelper.createDocument(XML); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML); msg.addMetadata("xpathMetadataKey", "count(/message)"); MetadataElement result = query.resolveXpath(doc, new XPath(), query.createXpathQuery(msg)); assertEquals("1", result.getValue()); }
@Test public void testResolveXpath_NamespaceWithNamespaceContext() throws Exception { MetadataXpathQuery query = init(create()); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_WITH_NAMESPACE); msg.addMetadata("xpathMetadataKey", "count(/svrl:schematron-output/svrl:failed-assert)"); StaticNamespaceContext ctx = new StaticNamespaceContext(); Document doc = XmlHelper.createDocument(XML_WITH_NAMESPACE, ctx); MetadataElement result = query.resolveXpath(doc, new XPath(ctx), query.createXpathQuery(msg)); assertEquals("2", result.getValue()); }