本文整理汇总了Java中org.custommonkey.xmlunit.XMLUnit.newXpathEngine方法的典型用法代码示例。如果您正苦于以下问题:Java XMLUnit.newXpathEngine方法的具体用法?Java XMLUnit.newXpathEngine怎么用?Java XMLUnit.newXpathEngine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.custommonkey.xmlunit.XMLUnit
的用法示例。
在下文中一共展示了XMLUnit.newXpathEngine方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeIgnoredBranches
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
public Document removeIgnoredBranches(Document document) {
for (String branch : getIgnoredBranches()) {
XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
NodeList nodeList;
try {
nodeList = simpleXpathEngine.getMatchingNodes(branch, document);
for (int i = 0; i < nodeList.getLength(); i++) {
Node parentNode = nodeList.item(i).getParentNode();
parentNode.removeChild(nodeList.item(i));
}
} catch (XpathException e) {
e.printStackTrace(); // FIXME : remove printStackTrace()
}
}
return document;
}
示例2: forTopics
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void forTopics() throws Exception {
Forum forum = new Forum(); forum.setId(1); forum.setName("forum x"); forum.setDescription("forum description");
when(forumRepository.get(forum.getId())).thenReturn(forum);
when(config.getInt(ConfigKeys.TOPICS_PER_PAGE)).thenReturn(10);
when(rssRepository.getForumTopics(forum, 10)).thenReturn(Arrays.asList(newTopic(1, "topic 1", 1, "post text 1")));
when(i18n.params("forum x")).thenReturn(new Object[] { "forum x" });
when(i18n.getFormattedMessage("RSS.ForumTopics.title", new Object[] { "forum x" })).thenReturn("channel title");
when(config.getValue(ConfigKeys.RSS_DATE_TIME_FORMAT)).thenReturn("EEE, d MMM yyyy HH:mm:ss");
when(config.getString(ConfigKeys.FORUM_LINK)).thenReturn("http://site.link/");
String result = service.forForum(1);
XpathEngine xpath = XMLUnit.newXpathEngine();
Document document = XMLUnit.buildControlDocument(result);
assertEquals("forum description", xpath.evaluate("//channel/description", document));
assertEquals("http://site.link/forums/show/1.page", xpath.evaluate("//channel/link", document));
assertEquals("channel title", xpath.evaluate("//channel/title", document));
assertEquals("post text 1", xpath.evaluate("//channel/item/description", document));
assertEquals("http://site.link/topics/preList/1/1.page", xpath.evaluate("//channel/item/link", document));
assertEquals("topic 1", xpath.evaluate("//channel/item/title", document));
}
示例3: testLoanRequestAccepted
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void testLoanRequestAccepted() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String port = System.getProperty("org.switchyard.component.soap.standalone.port", "8181/cxf");
String response = httpMixIn.postString("http://localhost:" + port + "/loanService/loanService", SOAP_REQUEST_1);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
m.put("tns", "http://example.com/loan-approval/loanService/");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//tns:accept", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().equals("yes")) {
fail("Expecting 'yes'");
}
} finally {
httpMixIn.uninitialize();
}
}
示例4: testLoanRequestUnableToHandle
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void testLoanRequestUnableToHandle() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String response = httpMixIn.postString("http://localhost:" + getSoapClientPort() + "/loanService/loanService", SOAP_REQUEST_2);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
//m.put("tns", "http://example.com/loan-approval/loanService/");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//faultcode", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().endsWith(":unableToHandleRequest")) {
fail("Expecting 'unableToHandleRequest' fault code");
}
} finally {
httpMixIn.uninitialize();
}
}
示例5: testDeployment
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Override
@Test
public void testDeployment() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String response = httpMixIn.postString("http://localhost:" + getSoapClientPort() + "/SayHelloService/SayHelloService", SOAP_REQUEST);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
m.put("tns", "http://www.jboss.org/bpel/examples");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//tns:result", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().equals("Hello Fred")) {
fail("Expecting 'Hello Fred'");
}
} finally {
httpMixIn.uninitialize();
}
}
示例6: testLoanRequestAccepted
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void testLoanRequestAccepted() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String response = httpMixIn.postString("http://localhost:8080/loanService/loanService", SOAP_REQUEST_1);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
m.put("tns", "http://example.com/loan-approval/loanService/");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//tns:accept", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().equals("yes")) {
fail("Expecting 'yes'");
}
} finally {
httpMixIn.uninitialize();
}
}
示例7: testLoanRequestUnableToHandle
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void testLoanRequestUnableToHandle() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String response = httpMixIn.postString("http://localhost:8080/loanService/loanService", SOAP_REQUEST_2);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
//m.put("tns", "http://example.com/loan-approval/loanService/");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//faultcode", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().endsWith(":unableToHandleRequest")) {
fail("Expecting 'unableToHandleRequest' fault code");
}
} finally {
httpMixIn.uninitialize();
}
}
示例8: testSayHello
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void testSayHello() throws Exception {
HTTPMixIn httpMixIn = new HTTPMixIn();
httpMixIn.initialize();
try {
String response = httpMixIn.postString("http://localhost:8080/SayHelloService/SayHelloService", SOAP_REQUEST);
org.w3c.dom.Document d = XMLUnit.buildControlDocument(response);
java.util.HashMap<String,String> m = new java.util.HashMap<String,String>();
m.put("tns", "http://www.jboss.org/bpel/examples");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
NodeList l = engine.getMatchingNodes("//tns:result", d);
assertEquals(1, l.getLength());
assertEquals(org.w3c.dom.Node.ELEMENT_NODE, l.item(0).getNodeType());
if (!l.item(0).getTextContent().equals("Hello Fred")) {
fail("Expecting 'Hello Fred'");
}
} finally {
httpMixIn.uninitialize();
}
}
示例9: assertXpathEvaluatesTo
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
/**
* Assert the values of xpath expression evaluation is exactly the same as expected value.
* <p>The xpath may contain the xml namespace prefixes, since namespaces from flight example
* are being registered.
* @param msg the error message that will be used in case of test failure
* @param expected the expected value
* @param xpath the xpath to evaluate
* @param xmlDoc the xml to use
* @throws Exception if any error occurs during xpath evaluation
*/
private void assertXpathEvaluatesTo(String msg, String expected, String xpath, String xmlDoc) throws Exception {
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("tns", "http://samples.springframework.org/flight");
namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
NamespaceContext ctx = new SimpleNamespaceContext(namespaces);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
Document doc = XMLUnit.buildControlDocument(xmlDoc);
NodeList node = engine.getMatchingNodes(xpath, doc);
assertEquals(msg, expected, node.item(0).getNodeValue());
}
示例10: evaluate
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
/**
* Evaluate an XPATH expression.
*
* @param xpath
* @param xml
* @return
*/
public static String evaluate(final String xpath, final String xml) {
Map<String, Object> m = new HashMap<String, Object>();
m.put("svrl", Constants.HTTP_PURL_OCLC_ORG_DSDL_SVRL);
XpathEngine xpathEngine = XMLUnit.newXpathEngine();
xpathEngine.setNamespaceContext(new SimpleNamespaceContext(m));
try {
return xpathEngine.evaluate(xpath, XMLUnit.buildControlDocument(xml));
} catch (Exception e) {
LOG.error("Failed to apply xpath {} on xml {}", xpath, xml);
throw new RuntimeCamelException(e);
}
}
示例11: initializeXmlUnit
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
private static void initializeXmlUnit() {
HashMap<String, String> m = new HashMap<String, String>();
m.put("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
m.put("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
m.put("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");
m.put("g", "http://www.w3.org/2005/Atom"); // 'g' is a dummy for global namespace
NamespaceContext ctx = new SimpleNamespaceContext(m);
XMLUnit.setXpathNamespaceContext(ctx);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
}
示例12: getXpathEngine
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
public static XpathEngine getXpathEngine() {
final NamespaceContext context = new SimpleNamespaceContext(Collections.singletonMap("itunes", "http://www.itunes.com/dtds/podcast-1.0.dtd"));
final XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(context);
return engine;
}
示例13: test_createFromClass
import org.custommonkey.xmlunit.XMLUnit; //导入方法依赖的package包/类
@Test
public void test_createFromClass() throws Exception {
JAXBUnmarshalTransformer unmarshalTransformer = new JAXBUnmarshalTransformer(
new QName("purchaseOrder"), JavaTypes.toMessageType(POType.class), null, true);
JAXBMarshalTransformer marshalTransformer = new JAXBMarshalTransformer(
JavaTypes.toMessageType(POType.class), new QName("purchaseOrder"), null, true, true);
DefaultMessage message = new DefaultMessage();
message.setContent(new StreamSource(new StringReader(PO_XML)));
message.addAttachment("cid:coverphoto.jpg", new DataSource() {
@Override
public InputStream getInputStream() throws IOException {
return Classes.getResourceAsStream(IMG_FILE_PATH);
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not supported");
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public String getName() {
return "cid:coverphoto.jpg";
}
});
// Transform XML to Java POType
unmarshalTransformer.transform(message);
// Check if the attachment is successfully mapped into JAXB object
Object unmarshalledPOType = message.getContent();
Assert.assertEquals(POType.class, unmarshalledPOType.getClass());
for (Item i : ((POType)unmarshalledPOType).getItems().getItem()) {
if (i.getPartNumber().equals("242-GZ")) {
Assert.assertNotNull("A coverPhoto for 242-GZ must not be null", i.getCoverPhoto());
compareCoverPhoto(Classes.getResourceAsStream(IMG_FILE_PATH), i.getCoverPhoto().getInputStream());
} else {
Assert.assertNull("A coverPhoto for " + i.getPartNumber() + " must be null, but was :" + i.getCoverPhoto(), i.getCoverPhoto());
}
}
// Transform Java POType back to XML
logger.info("Attempting JAVA2XML transformation with attachment enabled");
marshalTransformer.transform(message);
String resultXML = message.getContent(String.class);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.compareXML(PO_XML, resultXML);
//logger.info(String.format("Response:[\n%s]", resultXML));
XpathEngine xpath = XMLUnit.newXpathEngine();
HashMap<String,String> ncm = new HashMap<String,String>();
ncm.put("xop", "http://www.w3.org/2004/08/xop/include");
xpath.setNamespaceContext(new SimpleNamespaceContext(ncm));
String href = xpath.evaluate("//purchaseOrder/items/item[@partNum=\"242-GZ\"]/coverPhoto/xop:Include/@href", XMLUnit.buildControlDocument(resultXML));
DataSource attachment = message.getAttachment(href);
Assert.assertNotNull(String.format("Attachment '%s' must not be null", href), attachment);
logger.info(String.format("Found an attachment '%s'", href));
compareCoverPhoto(Classes.getResourceAsStream(IMG_FILE_PATH), attachment.getInputStream());
// Try POType > XML again with attachment disabled - coverPhoto should be inlined
message.removeAttachment(href);
message.setContent(unmarshalledPOType);
JAXBMarshalTransformer marshalTransformerInline = new JAXBMarshalTransformer(
JavaTypes.toMessageType(POType.class), new QName("purchaseOrder"), null, false, true);
logger.info("Attempting JAVA2XML transformation with attachment disabled");
marshalTransformerInline.transform(message);
resultXML = message.getContent(String.class);
//logger.info(String.format("Response:[\n%s]", resultXML));
Document resultDoc = XMLUnit.buildControlDocument(resultXML);
href = xpath.evaluate("//purchaseOrder/items/item[@partNum=\"242-GZ\"]/coverPhoto/xop:Include", resultDoc);
Assert.assertTrue(String.format("xop:Include should not appear, but got [%s]", href), href.trim().isEmpty());
String coverPhotoB64 = xpath.evaluate("//purchaseOrder/items/item[@partNum=\"242-GZ\"]/coverPhoto", resultDoc);
Assert.assertTrue(String.format("Inlined coverPhoto data is invalid:[%s]", coverPhotoB64), coverPhotoB64 != null && !coverPhotoB64.trim().isEmpty());
logger.info(String.format("Found inlined coverPhoto:[%s]", coverPhotoB64));
compareCoverPhoto(Classes.getResourceAsStream(IMG_FILE_PATH), new ByteArrayInputStream(Base64.decode(coverPhotoB64)));
}