1 22 package org.jboss.test.xml; 23 24 import java.io.ByteArrayInputStream ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 28 import javax.xml.namespace.NamespaceContext ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.xpath.XPath ; 32 import javax.xml.xpath.XPathConstants ; 33 import javax.xml.xpath.XPathExpression ; 34 import javax.xml.xpath.XPathFactory ; 35 36 import junit.framework.TestCase; 37 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.NodeList ; 40 41 47 public class JaxpXPathBaseTestCase extends TestCase 48 { 49 protected static final String XML_STRING_SIMPLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 50 + "<employees>" 51 + " <employee>" 52 + " <name>e1</name>" 53 + " </employee>" 54 + " <employee>" 55 + " <name>e2</name>" 56 + " </employee>" + "</employees>"; 57 58 protected static final String XML_STRING_NS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 59 + "<foo:employees xmlns:foo=\"http://www.jboss.org/foobar\">" 60 + " <foo:employee>" 61 + " <name>e1</name>" 62 + " </foo:employee>" 63 + " <foo:employee>" 64 + " <name>e2</name>" 65 + " </foo:employee>" 66 + "</foo:employees>"; 67 68 protected void setUp() throws Exception 69 { 70 super.setUp(); 71 } 72 73 protected void tearDown() throws Exception 74 { 75 super.tearDown(); 76 } 77 78 public void testXPathDefaultFactoryCreate() 79 { 80 assertNotNull(newXpathFactoryInstance()); 81 } 82 83 public void testSimpleXpathExpression() throws Exception 84 { 85 XPathFactory xpathFactory = newXpathFactoryInstance(); 86 XPath xpath = xpathFactory.newXPath(); 87 88 Document doc = parseXML(XML_STRING_SIMPLE); 89 90 String xpe1 = "/employees/employee"; 91 XPathExpression employeesXPath = xpath.compile(xpe1); 92 93 NodeList nl = (NodeList ) employeesXPath.evaluate(doc, 94 XPathConstants.NODESET); 95 96 assertNotNull(nl); 97 assertEquals(nl.getLength(), 2); 98 assertEquals(nl.item(0).getTextContent().trim(), "e1"); 99 assertEquals(nl.item(1).getTextContent().trim(), "e2"); 100 } 101 102 public void testNamespaceXpathExpression() throws Exception 103 { 104 XPathFactory xpathFactory = newXpathFactoryInstance(); 105 XPath xpath = xpathFactory.newXPath(); 106 xpath.setNamespaceContext(new JBossFooBarNamespaceContext()); 107 108 Document doc = parseXML(XML_STRING_NS); 109 110 String xpe1 = "/employees/employee"; 111 XPathExpression badXPath = xpath.compile(xpe1); 112 NodeList nl = (NodeList ) badXPath.evaluate(doc, XPathConstants.NODESET); 113 assertNotNull(nl); 114 assertEquals(0, nl.getLength()); 115 116 String xpe2 = "//foo:employee"; 117 XPathExpression empXPath = xpath.compile(xpe2); 118 NodeList nl2 = (NodeList ) empXPath.evaluate(doc, XPathConstants.NODESET); 119 120 assertNotNull(nl2); 121 assertEquals(2, nl2.getLength()); 122 assertEquals("e1", nl2.item(0).getTextContent().trim()); 123 assertEquals("e2", nl2.item(1).getTextContent().trim()); 124 } 125 126 protected XPathFactory newXpathFactoryInstance() 127 { 128 return XPathFactory.newInstance(); 129 } 130 131 protected Document parseXML(String xml) throws Exception 132 { 133 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); 134 dbfactory.setNamespaceAware(true); 135 dbfactory.setXIncludeAware(true); 136 137 DocumentBuilder parser = dbfactory.newDocumentBuilder(); 138 139 ByteArrayInputStream is = new ByteArrayInputStream (xml.getBytes()); 140 141 Document doc = parser.parse(is); 142 143 return doc; 144 } 145 146 protected class JBossFooBarNamespaceContext implements NamespaceContext 147 { 148 public String getNamespaceURI(String prefix) 149 { 150 return "http://www.jboss.org/foobar"; 151 } 152 153 public String getPrefix(String namespaceURI) 154 { 155 return "foo"; 156 } 157 158 public Iterator getPrefixes(String namespaceURI) 159 { 160 ArrayList list = new ArrayList (); 161 list.add("foo"); 162 return list.iterator(); 163 } 164 } 165 } | Popular Tags |