use of org.w3c.dom.Document in project camel by apache.
the class XPathTest method testXPathWithDocumentTypeDOMSourceNoResultQName.
public void testXPathWithDocumentTypeDOMSourceNoResultQName() throws Exception {
Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
XPathBuilder builder = xpath("/foo");
builder.setDocumentType(DOMSource.class);
builder.setResultQName(null);
Object result = builder.evaluate(createExchange(doc));
assertNotNull(result);
String s = context.getTypeConverter().convertTo(String.class, result);
assertEquals("bar", s);
}
use of org.w3c.dom.Document in project camel by apache.
the class XPathTest method testXPathSplitConcurrent.
public void testXPathSplitConcurrent() throws Exception {
int size = 100;
final Object node = XPathBuilder.xpath("foo/bar").nodeResult().evaluate(createExchange("<foo><bar>cheese</bar><bar>cake</bar><bar>beer</bar></foo>"));
assertNotNull(node);
// convert the node concurrently to test that XML Parser is not thread safe when
// importing nodes to a new Document, so try a test for that
final List<Document> result = new ArrayList<Document>();
ExecutorService executor = Executors.newFixedThreadPool(size);
final CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
executor.submit(new Callable<Document>() {
public Document call() throws Exception {
try {
Document doc = context.getTypeConverter().convertTo(Document.class, node);
result.add(doc);
return doc;
} finally {
latch.countDown();
}
}
});
}
// give time to convert concurrently
assertTrue(latch.await(20, TimeUnit.SECONDS));
Iterator<Document> it = result.iterator();
int count = 0;
while (it.hasNext()) {
count++;
Document doc = it.next();
assertNotNull(doc);
}
assertEquals(size, count);
executor.shutdownNow();
}
use of org.w3c.dom.Document in project camel by apache.
the class XPathTest method testXPathWithDocumentTypeDOMSource.
public void testXPathWithDocumentTypeDOMSource() throws Exception {
Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
XPathBuilder builder = xpath("/foo");
builder.setDocumentType(DOMSource.class);
Object result = builder.evaluate(createExchange(doc));
assertNotNull(result);
String s = context.getTypeConverter().convertTo(String.class, result);
assertEquals("<foo>bar</foo>", s);
}
use of org.w3c.dom.Document in project camel by apache.
the class XPathTest method testXPathNodeListSimpleTest.
public void testXPathNodeListSimpleTest() throws Exception {
String xml = "<foo><person>Claus</person></foo>";
Document doc = context.getTypeConverter().convertTo(Document.class, xml);
Object result = xpath("/foo/person").nodeSetResult().evaluate(createExchange(doc));
assertNotNull(result);
String s = context.getTypeConverter().convertTo(String.class, result);
assertEquals("<person>Claus</person>", s);
}
use of org.w3c.dom.Document in project camel by apache.
the class DumpModelAsXmlSplitBodyRouteTest method testDumpModelAsXml.
public void testDumpModelAsXml() throws Exception {
String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
assertNotNull(xml);
log.info(xml);
Document doc = new XmlConverter().toDOMDocument(xml);
NodeList nodes = doc.getElementsByTagName("simple");
assertEquals(1, nodes.getLength());
Element node = (Element) nodes.item(0);
assertNotNull("Node <simple> expected to be instanceof Element", node);
assertEquals("body", node.getTextContent());
nodes = doc.getElementsByTagName("split");
assertEquals(1, nodes.getLength());
nodes = doc.getElementsByTagName("to");
assertEquals(1, nodes.getLength());
node = (Element) nodes.item(0);
assertNotNull("Node <to> expected to be instanceof Element", node);
assertEquals("mock:sub", node.getAttribute("uri"));
assertEquals("myMock", node.getAttribute("id"));
assertEquals("true", node.getAttribute("customId"));
}
Aggregations