001: /*
002: * Created on May 24, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package de.schlund.pfixxml.util;
008:
009: import java.io.StringReader;
010: import java.util.List;
011:
012: import javax.xml.parsers.DocumentBuilder;
013: import javax.xml.parsers.DocumentBuilderFactory;
014: import javax.xml.transform.TransformerException;
015:
016: import junit.framework.TestCase;
017:
018: import org.w3c.dom.Attr;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.Node;
022: import org.xml.sax.InputSource;
023: import org.xml.sax.SAXException;
024:
025: public class XPathTest extends TestCase {
026:
027: private List<Node> lst;
028: private Node node;
029:
030: public void testElements() throws Exception {
031: lst = select("<x><a/><a/></x>", "/x/a");
032: assertEquals(2, lst.size());
033: assertEquals("a", ((Node) lst.get(0)).getLocalName());
034: assertEquals("a", ((Node) lst.get(1)).getLocalName());
035: node = selectNode("<x><a/></x>", "/x/b");
036: assertNull(node);
037: node = selectNode("<x><a/></x>", "/x/a");
038: assertEquals("a", node.getLocalName());
039: node = selectNode("<x><a id=\"1\"/><a id=\"2\"/></x>", "/x/a");
040: assertEquals("1", ((Element) node).getAttribute("id"));
041: }
042:
043: public void testAttributes() throws Exception {
044: lst = select("<x><a attr='foo'/><b attr='bar'/></x>", "//@attr");
045: assertEquals(2, lst.size());
046: assertEquals("foo", ((Attr) lst.get(0)).getValue());
047: assertEquals("bar", ((Attr) lst.get(1)).getValue());
048: }
049:
050: public void testContext() throws Exception {
051: lst = select("<x><a/><a/></x>", "/x/a");
052: node = (Node) lst.get(0);
053: lst = XPath.select(node, ".");
054: assertEquals(1, lst.size());
055: checkNodeEquality(node, (Node) lst.get(0));
056: }
057:
058: protected void checkNodeEquality(Node node1, Node node2) {
059: assertSame(node1, node2);
060: }
061:
062: public void testBoolean() throws Exception {
063: Document doc = parse("<x/>");
064:
065: assertEquals(false, XPath.test(doc, "false"));
066: assertEquals(false, XPath.test(doc, "/y"));
067: assertEquals(false, XPath.test(doc, "0"));
068: assertEquals(false, XPath.test(doc, "''"));
069:
070: assertEquals(true, XPath.test(doc, "' '"));
071: assertEquals(true, XPath.test(doc, "7"));
072: assertEquals(true, XPath.test(doc, "/x"));
073: }
074:
075: public void testVersion2() throws Exception {
076: // things that xpath 2.0 reports as an error (xpath 1.0 didn't object ...)
077: try {
078: select("<x/>", "0='0'");
079: fail();
080: } catch (TransformerException e) {
081: // ok
082: }
083: }
084:
085: private List<Node> select(String doc, String xpath)
086: throws Exception {
087: return XPath.select(parse(doc), xpath);
088: }
089:
090: private Node selectNode(String doc, String xpath) throws Exception {
091: return XPath.selectNode(parse(doc), xpath);
092: }
093:
094: private Document parse(String doc) throws Exception {
095: //return createDOM(doc);
096: try {
097: return Xml.parseStringMutable(doc);
098: } catch (SAXException e) {
099: fail("wrong document: " + doc + ":" + e.getMessage());
100: return null; // dummy
101: }
102: }
103:
104: protected Document createDOM(String xml) throws Exception {
105: DocumentBuilderFactory dbf = DocumentBuilderFactory
106: .newInstance();
107: return createDOM(dbf, xml);
108: }
109:
110: protected Document createDOM(DocumentBuilderFactory dbf, String xml)
111: throws Exception {
112: dbf.setNamespaceAware(true);
113: dbf.setValidating(false);
114: DocumentBuilder db = dbf.newDocumentBuilder();
115: InputSource src = new InputSource(new StringReader(xml));
116: Document doc = db.parse(src);
117: return doc;
118: }
119:
120: }
|