Source Code Cross Referenced for XPathTest.java in  » XML » xom » nu » xom » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xom » nu.xom.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* Copyright 2005 Elliotte Rusty Harold
0002:           
0003:           This library is free software; you can redistribute it and/or modify
0004:           it under the terms of version 2.1 of the GNU Lesser General Public 
0005:           License as published by the Free Software Foundation.
0006:           
0007:           This library is distributed in the hope that it will be useful,
0008:           but WITHOUT ANY WARRANTY; without even the implied warranty of
0009:           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
0010:           GNU Lesser General Public License for more details.
0011:           
0012:           You should have received a copy of the GNU Lesser General Public
0013:           License along with this library; if not, write to the 
0014:           Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
0015:           Boston, MA 02111-1307  USA
0016:           
0017:           You can contact Elliotte Rusty Harold by sending e-mail to
0018:           elharo@metalab.unc.edu. Please include the word "XOM" in the
0019:           subject line. The XOM home page is located at http://www.xom.nu/
0020:         */
0021:
0022:        package nu.xom.tests;
0023:
0024:        import java.io.ByteArrayOutputStream;
0025:        import java.io.File;
0026:        import java.io.IOException;
0027:        import java.io.PrintStream;
0028:        import java.net.MalformedURLException;
0029:
0030:        import nu.xom.Attribute;
0031:        import nu.xom.Builder;
0032:        import nu.xom.Comment;
0033:        import nu.xom.DocType;
0034:        import nu.xom.Document;
0035:        import nu.xom.Element;
0036:        import nu.xom.Elements;
0037:        import nu.xom.Namespace;
0038:        import nu.xom.NamespaceConflictException;
0039:        import nu.xom.Node;
0040:        import nu.xom.Nodes;
0041:        import nu.xom.ParsingException;
0042:        import nu.xom.ProcessingInstruction;
0043:        import nu.xom.Text;
0044:        import nu.xom.XPathContext;
0045:        import nu.xom.XPathException;
0046:        import nu.xom.XPathTypeException;
0047:
0048:        /**
0049:         * <p>
0050:         * Unit tests for XPath functionality
0051:         * </p>
0052:         * 
0053:         * @author Elliotte Rusty Harold
0054:         * @version 1.1b4
0055:         *
0056:         */
0057:        public class XPathTest extends XOMTestCase {
0058:
0059:            public XPathTest(String name) {
0060:                super (name);
0061:            }
0062:
0063:            // This class tests error conditions, which Xerces
0064:            // annoyingly logs to System.err. This hides System.err 
0065:            // before each test and restores it after each test.
0066:            private PrintStream systemErr = System.err;
0067:
0068:            protected void setUp() {
0069:                System.setErr(new PrintStream(new ByteArrayOutputStream()));
0070:            }
0071:
0072:            protected void tearDown() {
0073:                System.setErr(systemErr);
0074:            }
0075:
0076:            public void testAllNodesQuery() {
0077:
0078:                Document doc = new Document(new Element("doc"));
0079:                Nodes subset = doc.query("//. | /");
0080:                assertEquals(2, subset.size());
0081:
0082:            }
0083:
0084:            /* <a>
0085:            <x>1</x>
0086:            <b>
0087:            <x>2</x>
0088:            <x>3</x>
0089:            </b>
0090:            <x>4</x>
0091:            </a> */
0092:            public void testDescendantAxisOrder() {
0093:
0094:                Element a = new Element("a");
0095:                Document doc = new Document(a);
0096:                Element x1 = new Element("x");
0097:                x1.appendChild("a1");
0098:                Element x2 = new Element("x");
0099:                x2.appendChild("b2");
0100:                Element x3 = new Element("x");
0101:                x3.appendChild("c3");
0102:                Element x4 = new Element("x");
0103:                x4.appendChild("d4");
0104:                a.appendChild(x1);
0105:                Element b = new Element("b");
0106:                b.appendChild(x2);
0107:                b.appendChild(x3);
0108:                a.appendChild(b);
0109:                a.appendChild(x4);
0110:                Nodes result = doc.query("//x");
0111:                assertEquals(4, result.size());
0112:                assertTrue(result.get(0) instanceof  Element);
0113:                assertTrue(result.get(1) instanceof  Element);
0114:                assertTrue(result.get(2) instanceof  Element);
0115:                assertTrue(result.get(3) instanceof  Element);
0116:                assertEquals(x1, result.get(0));
0117:                assertEquals(x2, result.get(1));
0118:                assertEquals(x3, result.get(2));
0119:                assertEquals(x4, result.get(3));
0120:
0121:            }
0122:
0123:            public void testSimpleQuery() {
0124:
0125:                Element parent = new Element("Test");
0126:                Element child = new Element("child");
0127:                parent.appendChild(child);
0128:
0129:                Nodes result = parent.query("*");
0130:                assertEquals(1, result.size());
0131:                assertEquals(child, result.get(0));
0132:
0133:            }
0134:
0135:            public void testChildWildCard() {
0136:
0137:                Element a = new Element("a");
0138:                Element b = new Element("b");
0139:                Element c = new Element("c");
0140:                Element d = new Element("d");
0141:
0142:                a.appendChild(b);
0143:                b.appendChild(c);
0144:                c.appendChild(d);
0145:
0146:                Nodes result = a.query("/a/b/*/d");
0147:                assertEquals(1, result.size());
0148:                assertEquals(d, result.get(0));
0149:
0150:            }
0151:
0152:            public void testSimpleChild() {
0153:
0154:                Element a = new Element("a");
0155:                Nodes result = a.query("/a");
0156:                assertEquals(1, result.size());
0157:
0158:            }
0159:
0160:            public void testSimpleWildCard() {
0161:
0162:                Element a = new Element("a");
0163:                Nodes result = a.query("/*");
0164:                assertEquals(1, result.size());
0165:
0166:            }
0167:
0168:            public void testLongs() {
0169:
0170:                Document doc = new Document(new Element("root"));
0171:
0172:                Nodes result = doc.query("/*[5000000000=5000000000]");
0173:                assertEquals(1, result.size());
0174:
0175:                result = doc.query("/*[5000000000 < 5000000001]");
0176:                assertEquals(1, result.size());
0177:                result = doc.query("/*[5000000000 > 5000000001]");
0178:                assertEquals(0, result.size());
0179:                result = doc.query("/*[5000000001 >= 5000000001]");
0180:                assertEquals(1, result.size());
0181:                result = doc.query("/*[5000000001 > 5000000001]");
0182:                assertEquals(0, result.size());
0183:
0184:            }
0185:
0186:            public void testNamespaceNodeParent() {
0187:
0188:                Element root = new Element("root");
0189:                Document doc = new Document(root);
0190:                Element child = new Element("pre:child", "http://www.ietf.org");
0191:                root.appendChild(child);
0192:
0193:                Nodes result = doc.query("/root/*/namespace::*/parent::*");
0194:                assertEquals(1, result.size());
0195:                assertEquals(child, result.get(0));
0196:
0197:            }
0198:
0199:            public void testNamespaceNodeChild() {
0200:
0201:                Element root = new Element("root");
0202:                Document doc = new Document(root);
0203:                Element child = new Element("pre:child", "http://www.ietf.org");
0204:                root.appendChild(child);
0205:
0206:                Nodes result = doc.query("/root/*/namespace::*/child::*");
0207:                assertEquals(0, result.size());
0208:
0209:            }
0210:
0211:            // JAXEN-46: http://jira.codehaus.org/browse/JAXEN-46
0212:            public void testHashInAttributeValue() {
0213:
0214:                Element root = new Element("test");
0215:                Document doc = new Document(root);
0216:                Attribute test = new Attribute("test", "SUBSCRIBER");
0217:                Attribute test1 = new Attribute("test1", "SUBSCRIBER#");
0218:                root.addAttribute(test);
0219:                root.addAttribute(test1);
0220:
0221:                Nodes result = doc.query("test[@test='SUBSCRIBER']");
0222:                assertEquals(1, result.size());
0223:                assertEquals(root, result.get(0));
0224:
0225:                result = doc.query("test[@test1='SUBSCRIBER#']");
0226:                assertEquals(1, result.size());
0227:                assertEquals(root, result.get(0));
0228:
0229:            }
0230:
0231:            public void testUseRootNodeWhenQueryingDocumentLessElements() {
0232:
0233:                Element test = new Element("Test");
0234:
0235:                Nodes result = test.query("/*");
0236:                assertEquals(1, result.size());
0237:                assertEquals(test, result.get(0));
0238:
0239:                try {
0240:                    test.query("/");
0241:                    fail("Did not throw exception when querying rootless document for root");
0242:                } catch (XPathException success) {
0243:                    assertNotNull(success.getMessage());
0244:                }
0245:
0246:            }
0247:
0248:            public void testUseRootNodeWhenQueryingDocumentLessElements2() {
0249:
0250:                Element test = new Element("Test");
0251:                Nodes result = test.query("/None");
0252:                assertEquals(0, result.size());
0253:
0254:            }
0255:
0256:            public void testUseRootNodeWhenQueryingDocumentLessElements3() {
0257:
0258:                Element test = new Element("Test");
0259:
0260:                Nodes result = test.query("//*");
0261:                assertEquals(1, result.size());
0262:
0263:            }
0264:
0265:            public void testUseRootNodeWhenQueryingDocumentLessElements4() {
0266:
0267:                Element test = new Element("Test");
0268:
0269:                Nodes result = test.query("//* | /");
0270:                assertEquals(1, result.size());
0271:
0272:            }
0273:
0274:            public void testUnionOfNodesWithInts() {
0275:
0276:                Element parent = new Element("Test");
0277:                Element child = new Element("child");
0278:                parent.appendChild(child);
0279:
0280:                try {
0281:                    parent.query("* | count(/*)");
0282:                    fail("Allowed query returning non-node-set");
0283:                } catch (XPathException success) {
0284:                    assertNotNull(success.getMessage());
0285:                }
0286:
0287:            }
0288:
0289:            public void testFranceschet1() throws ParsingException, IOException {
0290:
0291:                Builder builder = new Builder();
0292:                Document doc = builder
0293:                        .build("http://staff.science.uva.nl/~francesc/xpathmark/benchmark_canon.xml");
0294:                Element root = doc.getRootElement();
0295:                Elements inputs = root.getChildElements("document");
0296:                Element input = inputs.get(0).getFirstChildElement("site");
0297:                input.detach();
0298:
0299:                Nodes doc1Queries = root
0300:                        .query("child::query[starts-with(@id, 'Q')]");
0301:
0302:                for (int i = 0; i < doc1Queries.size(); i++) {
0303:                    Element query = (Element) doc1Queries.get(i);
0304:                    String xpath = query.getFirstChildElement("syntax")
0305:                            .getValue();
0306:                    String id = query.getAttributeValue("id");
0307:                    // this query needs special comparison code due to 
0308:                    // adjacent text nodes
0309:                    if ("Q21".equals(id))
0310:                        continue;
0311:                    // test suite bug relating to id() function
0312:                    else if (xpath.indexOf("id(") >= 0)
0313:                        continue;
0314:                    Nodes result = input.query(xpath);
0315:                    Element answer = query.getFirstChildElement("answer");
0316:                    Nodes expected = new Nodes();
0317:                    for (int j = 0; j < answer.getChildCount(); j++) {
0318:                        Node node = answer.getChild(j);
0319:                        if (node instanceof  Text) {
0320:                            if (!("".equals(node.getValue().trim()))) {
0321:                                expected.append(node);
0322:                            }
0323:                        } else {
0324:                            expected.append(node);
0325:                        }
0326:                    }
0327:                    assertEquals("Failed query " + id, expected.size(), result
0328:                            .size());
0329:                    for (int j = 0; j < result.size(); j++) {
0330:                        Node expectedNode = expected.get(j);
0331:                        Node actualNode = result.get(j);
0332:                        assertEquals(id + " " + expectedNode.toXML() + " "
0333:                                + actualNode.toXML(), expectedNode, actualNode);
0334:                    }
0335:                }
0336:
0337:            }
0338:
0339:            public void testFranceschet2() throws ParsingException, IOException {
0340:
0341:                Builder builder = new Builder();
0342:                Document doc = builder
0343:                        .build("http://staff.science.uva.nl/~francesc/xpathmark/benchmark_canon.xml");
0344:                Element root = doc.getRootElement();
0345:                Elements inputs = root.getChildElements("document");
0346:                Element input = inputs.get(1);
0347:                Document html = new Document(new Element("fake"));
0348:                int p = 0;
0349:                while (true) {
0350:                    Node node = input.getChild(0);
0351:                    if (node instanceof  Element)
0352:                        break;
0353:                    else {
0354:                        node.detach();
0355:                        if (node instanceof  Text)
0356:                            continue;
0357:                        html.insertChild(node, p++);
0358:                    }
0359:                }
0360:                Node newroot = input.getChild(0);
0361:                newroot.detach();
0362:                html.setRootElement((Element) newroot);
0363:                while (input.getChildCount() > 0) {
0364:                    Node node = input.getChild(0);
0365:                    node.detach();
0366:                    if (node instanceof  Text)
0367:                        continue;
0368:                    html.appendChild(node);
0369:                }
0370:
0371:                Nodes doc2Queries = root
0372:                        .query("child::query[starts-with(@id, 'A')]");
0373:
0374:                XPathContext context = new XPathContext();
0375:                context.addNamespace("svg", "http://www.w3.org/2000/svg");
0376:                context.addNamespace("xlink", "http://www.w3.org/1999/xlink");
0377:
0378:                for (int i = 0; i < doc2Queries.size(); i++) {
0379:                    Element query = (Element) doc2Queries.get(i);
0380:                    String xpath = query.getFirstChildElement("syntax")
0381:                            .getValue();
0382:                    String id = query.getAttributeValue("id");
0383:
0384:                    Nodes result = html.query(xpath, context);
0385:                    Element answer = query.getFirstChildElement("answer");
0386:                    Nodes expected = new Nodes();
0387:                    for (int j = 0; j < answer.getChildCount(); j++) {
0388:                        Node node = answer.getChild(j);
0389:                        if (node instanceof  Text) {
0390:                            if (!("".equals(node.getValue().trim()))) {
0391:                                expected.append(node);
0392:                            }
0393:                        } else {
0394:                            expected.append(node);
0395:                        }
0396:                    }
0397:                    assertEquals("Failed query " + id, expected.size(), result
0398:                            .size());
0399:                    for (int j = 0; j < result.size(); j++) {
0400:                        Node expectedNode = expected.get(j);
0401:                        Node actualNode = result.get(j);
0402:                        assertEquals(id + " " + expectedNode.toXML() + " "
0403:                                + actualNode.toXML(), expectedNode, actualNode);
0404:                    }
0405:                }
0406:
0407:            }
0408:
0409:            public void testQueryThatReturnsNumber() {
0410:
0411:                Element parent = new Element("Test");
0412:                Element child = new Element("child");
0413:                parent.appendChild(child);
0414:
0415:                try {
0416:                    parent.query("count(*)");
0417:                    fail("Allowed query to return number");
0418:                } catch (XPathTypeException success) {
0419:                    assertNotNull(success.getMessage());
0420:                    assertEquals(new Double(1), success.getReturnValue());
0421:                    assertEquals("count(*)", success.getXPath());
0422:                }
0423:
0424:            }
0425:
0426:            public void testStartsWith() {
0427:
0428:                Element parent = new Element("Test");
0429:                Element child = new Element("child");
0430:                child.addAttribute(new Attribute("foo", "bar"));
0431:                parent.appendChild(child);
0432:                Element child2 = new Element("child");
0433:                child2.addAttribute(new Attribute("foo", "big"));
0434:                parent.appendChild(child2);
0435:
0436:                Nodes result = parent.query(".//*[starts-with(@foo, 'ba')]");
0437:                assertEquals(1, result.size());
0438:                assertEquals(child, result.get(0));
0439:
0440:            }
0441:
0442:            // a jaxen extension function
0443:            public void testEndsWith() {
0444:
0445:                Element parent = new Element("Test");
0446:                Element child = new Element("child");
0447:                child.addAttribute(new Attribute("foo", "bar"));
0448:                parent.appendChild(child);
0449:
0450:                try {
0451:                    parent.query(".//*[ends-with(@foo, 'ar')]");
0452:                    fail("Allowed Jaxen extension function");
0453:                } catch (XPathException success) {
0454:                    assertTrue(success.getMessage().indexOf("ends-with") >= 0);
0455:                }
0456:
0457:            }
0458:
0459:            public void testEmptyTextNodesDontCount() {
0460:
0461:                Element parent = new Element("Test");
0462:                Element child1 = new Element("child1");
0463:                parent.appendChild(child1);
0464:                parent.appendChild(new Text(""));
0465:                parent.appendChild(new Text(""));
0466:                Element child2 = new Element("child2");
0467:                parent.appendChild(child2);
0468:
0469:                Nodes result = parent.query("*");
0470:                assertEquals(2, result.size());
0471:                assertEquals(child1, result.get(0));
0472:
0473:                result = parent.query("*[2]");
0474:                assertEquals(1, result.size());
0475:                assertEquals(child2, result.get(0));
0476:
0477:            }
0478:
0479:            public void testEmptyTextNodesDontCount2() {
0480:
0481:                Element parent = new Element("Test");
0482:                parent.appendChild(new Text(""));
0483:                Element child1 = new Element("child1");
0484:                parent.appendChild(child1);
0485:                Element child2 = new Element("child2");
0486:                parent.appendChild(child2);
0487:
0488:                Nodes result = parent.query("node()");
0489:                assertEquals(2, result.size());
0490:                assertEquals(child1, result.get(0));
0491:                assertEquals(child2, result.get(1));
0492:
0493:                result = parent.query("node()[1]");
0494:                assertEquals(1, result.size());
0495:                assertEquals(child1, result.get(0));
0496:
0497:            }
0498:
0499:            public void testEmptyTextNodesAtEndOfParent() {
0500:
0501:                Element parent = new Element("Test");
0502:                Element child1 = new Element("child1");
0503:                parent.appendChild(child1);
0504:                Element child2 = new Element("child2");
0505:                parent.appendChild(child2);
0506:                parent.appendChild(new Text(""));
0507:                parent.appendChild(new Text(""));
0508:                parent.appendChild(new Text(""));
0509:
0510:                Nodes result = parent.query("node()");
0511:                assertEquals(2, result.size());
0512:                assertEquals(child1, result.get(0));
0513:                assertEquals(child2, result.get(1));
0514:
0515:                result = parent.query("node()[1]");
0516:                assertEquals(1, result.size());
0517:                assertEquals(child1, result.get(0));
0518:
0519:            }
0520:
0521:            public void testEmptyTextNodeNextToNonEmptyTextNode() {
0522:
0523:                Element parent = new Element("Test");
0524:                Text empty = new Text("");
0525:                parent.appendChild(empty);
0526:                Text nonempty = new Text("value");
0527:                parent.appendChild(nonempty);
0528:                Element child1 = new Element("child1");
0529:                parent.appendChild(child1);
0530:                Element child2 = new Element("child2");
0531:                parent.appendChild(child2);
0532:
0533:                Nodes result = parent.query("node()");
0534:                assertEquals(4, result.size());
0535:                assertEquals(empty, result.get(0));
0536:                assertEquals(nonempty, result.get(1));
0537:
0538:                result = parent.query("node()[1]");
0539:                assertEquals(2, result.size());
0540:                assertEquals(empty, result.get(0));
0541:
0542:            }
0543:
0544:            public void testBasicPredicate() {
0545:
0546:                Element parent = new Element("Test");
0547:                Element child1 = new Element("child");
0548:                child1.appendChild("1");
0549:                parent.appendChild(child1);
0550:                Element child2 = new Element("child");
0551:                child2.appendChild("2");
0552:                parent.appendChild(child2);
0553:                Element child3 = new Element("child");
0554:                child3.appendChild("3");
0555:                parent.appendChild(child3);
0556:
0557:                Nodes result = parent.query("*[.='2']");
0558:                assertEquals(1, result.size());
0559:                assertEquals(child2, result.get(0));
0560:
0561:            }
0562:
0563:            public void testXMLLang() {
0564:
0565:                Element parent = new Element("Test");
0566:                Element child1 = new Element("child");
0567:                child1.addAttribute(new Attribute("xml:lang",
0568:                        "http://www.w3.org/XML/1998/namespace", "en"));
0569:                parent.appendChild(child1);
0570:                Element child2 = new Element("child");
0571:                child2.appendChild("2");
0572:                child2.addAttribute(new Attribute("xml:lang",
0573:                        "http://www.w3.org/XML/1998/namespace", "fr"));
0574:                parent.appendChild(child2);
0575:                Element child3 = new Element("child");
0576:                child3.appendChild("3");
0577:                parent.appendChild(child3);
0578:                Element child4 = new Element("child");
0579:                child4.appendChild("4");
0580:                child4.addAttribute(new Attribute("xml:lang",
0581:                        "http://www.w3.org/XML/1998/namespace", "en-US"));
0582:                parent.appendChild(child4);
0583:
0584:                Nodes result = parent.query("child::*[lang('en')]");
0585:                assertEquals(2, result.size());
0586:                assertEquals(child1, result.get(0));
0587:                assertEquals(child4, result.get(1));
0588:
0589:            }
0590:
0591:            public void testXMLPrefixIsAlwaysBound() {
0592:
0593:                Element parent = new Element("Test");
0594:                Element child1 = new Element("child");
0595:                child1.addAttribute(new Attribute("xml:lang",
0596:                        "http://www.w3.org/XML/1998/namespace", "en"));
0597:                parent.appendChild(child1);
0598:                Element child2 = new Element("child");
0599:                child2.appendChild("2");
0600:                child2.addAttribute(new Attribute("xml:lang",
0601:                        "http://www.w3.org/XML/1998/namespace", "fr"));
0602:                parent.appendChild(child2);
0603:                Element child3 = new Element("child");
0604:                child3.appendChild("3");
0605:                parent.appendChild(child3);
0606:                Element child4 = new Element("child");
0607:                child4.appendChild("4");
0608:                child4.addAttribute(new Attribute("xml:lang",
0609:                        "http://www.w3.org/XML/1998/namespace", "en-US"));
0610:                parent.appendChild(child4);
0611:
0612:                Nodes result = parent.query("child::*/@xml:lang");
0613:                assertEquals(3, result.size());
0614:
0615:            }
0616:
0617:            public void testCantRebindXMLPrefix() {
0618:
0619:                XPathContext context = new XPathContext();
0620:                try {
0621:                    context.addNamespace("xml", "http://www.example.org");
0622:                    fail("Rebound xml prefix");
0623:                } catch (NamespaceConflictException success) {
0624:                    assertNotNull(success.getMessage());
0625:                }
0626:
0627:            }
0628:
0629:            public void testParentAxis() {
0630:
0631:                Element parent = new Element("Test");
0632:                Element child = new Element("child");
0633:                parent.appendChild(child);
0634:
0635:                Nodes result = child.query("parent::*");
0636:                assertEquals(1, result.size());
0637:                assertEquals(parent, result.get(0));
0638:
0639:            }
0640:
0641:            public void testAncestorAxis() {
0642:
0643:                Element grandparent = new Element("Test");
0644:                new Document(grandparent);
0645:                Element parent = new Element("Test");
0646:                Element child = new Element("child");
0647:                parent.appendChild(child);
0648:                grandparent.appendChild(parent);
0649:
0650:                Nodes result = child.query("ancestor::*");
0651:                assertEquals(2, result.size());
0652:                assertEquals(grandparent, result.get(0));
0653:                assertEquals(parent, result.get(1));
0654:
0655:            }
0656:
0657:            public void testParentAxisWithDocument() {
0658:
0659:                Element root = new Element("Test");
0660:                new Document(root);
0661:
0662:                Nodes result = root.query("parent::*");
0663:                assertEquals(0, result.size());
0664:
0665:            }
0666:
0667:            public void testParentAxisWithNodeMatchingDocument() {
0668:
0669:                Element root = new Element("Test");
0670:                Document doc = new Document(root);
0671:
0672:                Nodes result = root.query("parent::node()");
0673:                assertEquals(1, result.size());
0674:                assertEquals(doc, result.get(0));
0675:
0676:            }
0677:
0678:            public void testSubstringFunction() {
0679:
0680:                Element root = new Element("Test");
0681:                new Document(root);
0682:
0683:                Nodes result = root.query("/*[substring('12345', 0, 3)='12']");
0684:                assertEquals(1, result.size());
0685:                assertEquals(root, result.get(0));
0686:
0687:            }
0688:
0689:            public void testPrecedingAxisWithElementName() {
0690:
0691:                Element root = new Element("Test");
0692:                Document doc = new Document(root);
0693:
0694:                Nodes result = doc.query("/descendant::*/preceding::x");
0695:                assertEquals(0, result.size());
0696:
0697:            }
0698:
0699:            public void testDocTypeIsNotAnXPathNode() {
0700:
0701:                Element root = new Element("root");
0702:                Document doc = new Document(root);
0703:                DocType doctype = new DocType("root");
0704:                doc.setDocType(doctype);
0705:
0706:                Nodes result = doc.query("child::node()[1]");
0707:                assertEquals(1, result.size());
0708:                assertEquals(root, result.get(0));
0709:
0710:            }
0711:
0712:            public void testGetNodeBeforeDocType() {
0713:
0714:                Element root = new Element("root");
0715:                Document doc = new Document(root);
0716:                DocType doctype = new DocType("root");
0717:                doc.setDocType(doctype);
0718:                Comment c = new Comment("test");
0719:                doc.insertChild(c, 0);
0720:
0721:                Nodes result = doc.query("child::node()[1]");
0722:                assertEquals(1, result.size());
0723:                assertEquals(c, result.get(0));
0724:
0725:            }
0726:
0727:            public void testCantUseDocTypeAsXPathContextNode() {
0728:
0729:                Element root = new Element("root");
0730:                Document doc = new Document(root);
0731:                DocType doctype = new DocType("root");
0732:                doc.setDocType(doctype);
0733:
0734:                try {
0735:                    doctype.query("/");
0736:                    fail("Allowed DocType as context node");
0737:                } catch (XPathException success) {
0738:                    assertNotNull(success.getMessage());
0739:                }
0740:
0741:            }
0742:
0743:            public void testDescendantAxis() {
0744:
0745:                Element grandparent = new Element("Test");
0746:                Document doc = new Document(grandparent);
0747:                Element parent = new Element("Test");
0748:                Element child = new Element("child");
0749:                parent.appendChild(child);
0750:                grandparent.appendChild(parent);
0751:
0752:                Nodes result = doc.query("descendant::*");
0753:                assertEquals(3, result.size());
0754:                assertEquals(grandparent, result.get(0));
0755:                assertEquals(parent, result.get(1));
0756:                assertEquals(child, result.get(2));
0757:
0758:            }
0759:
0760:            public void testGetElementQName() {
0761:
0762:                Element grandparent = new Element("Test");
0763:                Document doc = new Document(grandparent);
0764:                Element parent = new Element("Test");
0765:                Element child1 = new Element("pre:child",
0766:                        "http://www.example.org/");
0767:                Element child2 = new Element("pre:child",
0768:                        "http://www.example.com/");
0769:                parent.appendChild(child1);
0770:                parent.appendChild(child2);
0771:                grandparent.appendChild(parent);
0772:
0773:                Nodes result = doc.query("descendant::*[name(.)='pre:child']");
0774:                assertEquals(2, result.size());
0775:                assertEquals(child1, result.get(0));
0776:                assertEquals(child2, result.get(1));
0777:
0778:            }
0779:
0780:            // This does not actually hit the method I was aiming at. 
0781:            // Apparently, Jaxen never actually invokes
0782:            // getElementStringValue()
0783:            public void testGetElementStringValue() {
0784:
0785:                Element grandparent = new Element("Test");
0786:                Document doc = new Document(grandparent);
0787:                Element parent = new Element("Test");
0788:                Element child1 = new Element("child");
0789:                child1.appendChild("foo");
0790:                Element child2 = new Element("child");
0791:                child2.appendChild("bar");
0792:                parent.appendChild(child1);
0793:                parent.appendChild(child2);
0794:                grandparent.appendChild(parent);
0795:
0796:                Nodes result = doc.query("descendant::*[.='foo']");
0797:                assertEquals(1, result.size());
0798:                assertEquals(child1, result.get(0));
0799:
0800:            }
0801:
0802:            public void testGetNonExistentNode() {
0803:
0804:                Element grandparent = new Element("Test");
0805:                Document doc = new Document(grandparent);
0806:                Element parent = new Element("Test");
0807:                Element child1 = new Element("child");
0808:                child1.appendChild("foo");
0809:                Element child2 = new Element("child");
0810:                child2.appendChild("bar");
0811:                parent.appendChild(child1);
0812:                parent.appendChild(child2);
0813:                grandparent.appendChild(parent);
0814:
0815:                Nodes result = doc.query("/Test/Test/*[12]");
0816:                assertEquals(0, result.size());
0817:
0818:            }
0819:
0820:            public void testGetAttributeQName() {
0821:
0822:                Element grandparent = new Element("Test");
0823:                Document doc = new Document(grandparent);
0824:                Element parent = new Element("Test");
0825:                Attribute a1 = new Attribute("pre:attribute",
0826:                        "http://www.example.org/", "test");
0827:                Attribute a2 = new Attribute("pre:attribute",
0828:                        "http://www.example.com/", "test");
0829:                parent.addAttribute(a2);
0830:                grandparent.addAttribute(a1);
0831:                grandparent.appendChild(parent);
0832:
0833:                Nodes result = doc
0834:                        .query("descendant::*/attribute::*[name(.)='pre:attribute']");
0835:                assertEquals(2, result.size());
0836:                assertTrue(result.contains(a1));
0837:                assertTrue(result.contains(a2));
0838:
0839:            }
0840:
0841:            public void testGetNamespaceStringValue() {
0842:
0843:                Element test = new Element("Test", "http://www.example.com/");
0844:
0845:                Nodes result = test
0846:                        .query("self::*[contains(namespace::*, 'http://')]");
0847:                assertEquals(1, result.size());
0848:                assertEquals(test, result.get(0));
0849:
0850:            }
0851:
0852:            private File inputDir = new File("data");
0853:
0854:            public void testGetDocument() throws MalformedURLException {
0855:
0856:                Element element = new Element("test");
0857:                File f = new File(inputDir, "prettyxml.xml");
0858:                String url = f.toURL().toExternalForm();
0859:                try {
0860:                    element.query("document('" + url + "')/*");
0861:                    fail("allowed document() function");
0862:                } catch (XPathException success) {
0863:                    assertTrue(success.getMessage().indexOf("document") >= 0);
0864:                }
0865:
0866:            }
0867:
0868:            public void testGetMultipleNodesViaDocumentFunction()
0869:                    throws MalformedURLException {
0870:
0871:                Element element = new Element("test");
0872:                File f = new File(inputDir, "prettyxml.xml");
0873:                String url = f.toURL().toExternalForm();
0874:                try {
0875:                    element.query("document('" + url + "')//*");
0876:                    fail("allowed document() function");
0877:                } catch (XPathException success) {
0878:                    assertTrue(success.getMessage().indexOf("document") >= 0);
0879:                }
0880:
0881:            }
0882:
0883:            public void testDoubleDocument() throws MalformedURLException {
0884:
0885:                Element element = new Element("test");
0886:                File f1 = new File(inputDir, "prettyxml.xml");
0887:                String url1 = f1.toURL().toExternalForm();
0888:                File f2 = new File(inputDir, "test.xml");
0889:                String url2 = f2.toURL().toExternalForm();
0890:                try {
0891:                    element.query("document('" + url1 + "')/* | "
0892:                            + "document('" + url2 + "')/*");
0893:                    fail("allowed document() function");
0894:                } catch (XPathException success) {
0895:                    assertTrue(success.getMessage().indexOf("document") >= 0);
0896:                }
0897:
0898:            }
0899:
0900:            public void testGetNonExistentDocument() {
0901:
0902:                Element element = new Element("test");
0903:                try {
0904:                    element
0905:                            .query("document('http://www.ibiblio.org/aksdjhk/')/*");
0906:                    fail("That file doesn't exist!");
0907:                } catch (XPathException success) {
0908:                    assertNotNull(success.getMessage());
0909:                }
0910:
0911:            }
0912:
0913:            public void testMalformedDocument() {
0914:
0915:                Element element = new Element("test");
0916:                try {
0917:                    element
0918:                            .query("document('http://www.cafeaulait.org/formatter/Formatter.java')/*");
0919:                    fail("Queried malformed document!");
0920:                } catch (XPathException success) {
0921:                    assertNotNull(success.getMessage());
0922:                }
0923:
0924:            }
0925:
0926:            public void testGetDocumentNode() {
0927:
0928:                Element element = new Element("test");
0929:                Document doc = new Document(element);
0930:                Nodes result = element.query("/");
0931:                assertEquals(1, result.size());
0932:                assertEquals(doc, result.get(0));
0933:
0934:            }
0935:
0936:            public void testCommentNodeTest() {
0937:
0938:                Element grandparent = new Element("Test");
0939:                Document doc = new Document(grandparent);
0940:                Element parent = new Element("Test");
0941:                Element child = new Element("child");
0942:                parent.appendChild(child);
0943:                grandparent.appendChild(parent);
0944:
0945:                Comment c1 = new Comment("c1");
0946:                Comment c2 = new Comment("c2");
0947:                Comment c3 = new Comment("c3");
0948:                Comment c4 = new Comment("c4");
0949:
0950:                doc.insertChild(c1, 0);
0951:                grandparent.insertChild(c2, 0);
0952:                parent.insertChild(c3, 0);
0953:                child.insertChild(c4, 0);
0954:
0955:                Nodes result = doc.query("descendant::comment()");
0956:                assertEquals(4, result.size());
0957:                assertEquals(c1, result.get(0));
0958:                assertEquals(c2, result.get(1));
0959:                assertEquals(c3, result.get(2));
0960:                assertEquals(c4, result.get(3));
0961:
0962:            }
0963:
0964:            public void testCommentStringValue() {
0965:
0966:                Element grandparent = new Element("Test");
0967:                Document doc = new Document(grandparent);
0968:                Element parent = new Element("Test");
0969:                Element child = new Element("child");
0970:                parent.appendChild(child);
0971:                grandparent.appendChild(parent);
0972:
0973:                Comment c1 = new Comment("c1");
0974:                Comment c2 = new Comment("c2");
0975:                Comment c3 = new Comment("c3");
0976:                Comment c4 = new Comment("c4");
0977:
0978:                doc.insertChild(c1, 0);
0979:                grandparent.insertChild(c2, 0);
0980:                parent.insertChild(c3, 0);
0981:                child.insertChild(c4, 0);
0982:
0983:                Nodes result = doc.query("descendant::comment()[.='c3']");
0984:                assertEquals(1, result.size());
0985:                assertEquals(c3, result.get(0));
0986:
0987:            }
0988:
0989:            public void testGetProcessingInstructionData() {
0990:
0991:                Element grandparent = new Element("Test");
0992:                Document doc = new Document(grandparent);
0993:                Element parent = new Element("Test");
0994:                Element child = new Element("child");
0995:                parent.appendChild(child);
0996:                grandparent.appendChild(parent);
0997:
0998:                ProcessingInstruction p1 = new ProcessingInstruction("c1", "1");
0999:                ProcessingInstruction p2 = new ProcessingInstruction("c1", "2");
1000:                ProcessingInstruction p3 = new ProcessingInstruction("c1", "3");
1001:                ProcessingInstruction p4 = new ProcessingInstruction("c1", "4");
1002:
1003:                doc.insertChild(p1, 0);
1004:                grandparent.insertChild(p2, 0);
1005:                parent.insertChild(p3, 0);
1006:                child.insertChild(p4, 0);
1007:
1008:                Nodes result = doc
1009:                        .query("descendant::processing-instruction()[.='3']");
1010:                assertEquals(1, result.size());
1011:                assertEquals(p3, result.get(0));
1012:
1013:            }
1014:
1015:            public void testProcessingInstructionNodeTest() {
1016:
1017:                Element grandparent = new Element("Test");
1018:                Document doc = new Document(grandparent);
1019:                Element parent = new Element("Test");
1020:                Element child = new Element("child");
1021:                parent.appendChild(child);
1022:                grandparent.appendChild(parent);
1023:
1024:                Comment c1 = new Comment("c1");
1025:                Comment c2 = new Comment("c2");
1026:                Comment c3 = new Comment("c3");
1027:                Comment c4 = new Comment("c4");
1028:
1029:                doc.insertChild(c1, 0);
1030:                grandparent.insertChild(c2, 0);
1031:                parent.insertChild(c3, 0);
1032:                child.insertChild(c4, 0);
1033:                ProcessingInstruction pi = new ProcessingInstruction(
1034:                        "appendix", "text");
1035:                doc.appendChild(pi);
1036:                ProcessingInstruction pi2 = new ProcessingInstruction("test",
1037:                        "text");
1038:                parent.appendChild(pi2);
1039:
1040:                Nodes result = doc
1041:                        .query("descendant::processing-instruction('test')");
1042:                assertEquals(1, result.size());
1043:                assertEquals(pi2, result.get(0));
1044:
1045:            }
1046:
1047:            public void testDescendantOrSelfAxis() {
1048:
1049:                Element grandparent = new Element("Test");
1050:                Element parent = new Element("Test");
1051:                Element child = new Element("child");
1052:                parent.appendChild(child);
1053:                grandparent.appendChild(parent);
1054:
1055:                Nodes result = grandparent.query("descendant-or-self::*");
1056:                assertEquals(3, result.size());
1057:                assertEquals(grandparent, result.get(0));
1058:                assertEquals(parent, result.get(1));
1059:                assertEquals(child, result.get(2));
1060:
1061:            }
1062:
1063:            public void testAncestorOrSelfAxis() {
1064:
1065:                Element grandparent = new Element("Test");
1066:                new Document(grandparent);
1067:                Element parent = new Element("Test");
1068:                Element child = new Element("child");
1069:                parent.appendChild(child);
1070:                grandparent.appendChild(parent);
1071:
1072:                Nodes result = child.query("ancestor-or-self::*");
1073:                assertEquals(3, result.size());
1074:                assertEquals(child, result.get(2));
1075:                assertEquals(parent, result.get(1));
1076:                assertEquals(grandparent, result.get(0));
1077:
1078:            }
1079:
1080:            public void testSelfAxis() {
1081:
1082:                Element parent = new Element("Test");
1083:                Element child = new Element("child");
1084:                parent.appendChild(child);
1085:
1086:                Nodes result = child.query("self::*");
1087:                assertEquals(1, result.size());
1088:                assertEquals(child, result.get(0));
1089:                result = parent.query("self::*");
1090:                assertEquals(1, result.size());
1091:                assertEquals(parent, result.get(0));
1092:
1093:            }
1094:
1095:            public void testSelfAxisWithUnparentedText() {
1096:
1097:                Text text = new Text("test");
1098:                Nodes result = text.query("self::text()");
1099:                assertEquals(1, result.size());
1100:                assertEquals(text, result.get(0));
1101:
1102:            }
1103:
1104:            public void testSelfAxisWithAttribute() {
1105:
1106:                Element e = new Element("child");
1107:                e.addAttribute(new Attribute("test", "value"));
1108:                Nodes result = e.query("@*[self::test]");
1109:                assertEquals(0, result.size());
1110:
1111:            }
1112:
1113:            public void testSelfAxisWithTextChild() {
1114:
1115:                Element parent = new Element("parent");
1116:                Node child = new Text("child");
1117:                parent.appendChild(child);
1118:                Nodes result = child.query("self::text()");
1119:                assertEquals(1, result.size());
1120:                assertEquals(child, result.get(0));
1121:
1122:            }
1123:
1124:            public void testSelfAxisWithTextChildren() {
1125:
1126:                Element parent = new Element("parent");
1127:                Node child1 = new Text("1");
1128:                Node child2 = new Text("2");
1129:                Node child3 = new Text("3");
1130:                Node child4 = new Text("4");
1131:                parent.appendChild(child1);
1132:                parent.appendChild(child2);
1133:                parent.appendChild(child3);
1134:                parent.appendChild(child4);
1135:                Nodes result = child1.query("self::text()");
1136:                assertEquals(4, result.size());
1137:                assertEquals(child1, result.get(0));
1138:                assertEquals(child2, result.get(1));
1139:                assertEquals(child3, result.get(2));
1140:                assertEquals(child4, result.get(3));
1141:
1142:            }
1143:
1144:            public void testSelfAxisWithTextChildren2() {
1145:
1146:                Element parent = new Element("parent");
1147:                Node child1 = new Text("1");
1148:                Node child2 = new Text("2");
1149:                Node child3 = new Text("3");
1150:                Node child4 = new Text("4");
1151:                parent.appendChild(child1);
1152:                parent.appendChild(child2);
1153:                parent.appendChild(child3);
1154:                parent.appendChild(child4);
1155:                Nodes result = child3.query("self::text()");
1156:                assertEquals(4, result.size());
1157:                assertEquals(child1, result.get(0));
1158:                assertEquals(child2, result.get(1));
1159:                assertEquals(child3, result.get(2));
1160:                assertEquals(child4, result.get(3));
1161:
1162:            }
1163:
1164:            public void testSelfAxisWithTextChildAndNoParent() {
1165:
1166:                Node child = new Text("child");
1167:                Nodes result = child.query("self::text()");
1168:                assertEquals(1, result.size());
1169:                assertEquals(child, result.get(0));
1170:
1171:            }
1172:
1173:            public void testAttributeAxis() {
1174:
1175:                Element parent = new Element("Test");
1176:                Element child = new Element("child");
1177:                parent.appendChild(child);
1178:                parent.addAttribute(new Attribute("name", "value"));
1179:                parent.addAttribute(new Attribute("name2", "value"));
1180:
1181:                Nodes result = child.query("attribute::*");
1182:                assertEquals(0, result.size());
1183:                result = parent.query("attribute::*");
1184:                assertEquals(2, result.size());
1185:                result = parent.query("attribute::name");
1186:                assertEquals(1, result.size());
1187:
1188:            }
1189:
1190:            public void testAttributeAxisOnNonElement() {
1191:
1192:                Text text = new Text("Test");
1193:                Nodes result = text.query("attribute::*");
1194:                assertEquals(0, result.size());
1195:
1196:            }
1197:
1198:            public void testEmptyParentAxis() {
1199:
1200:                Element parent = new Element("Test");
1201:                Element child = new Element("child");
1202:                parent.appendChild(child);
1203:
1204:                Nodes result = parent.query("parent::*");
1205:                assertEquals(0, result.size());
1206:
1207:            }
1208:
1209:            public void testPrecedingSiblingAxis() {
1210:
1211:                Element parent = new Element("Test");
1212:                Element child1 = new Element("child1");
1213:                Element child2 = new Element("child2");
1214:                Element child3 = new Element("child3");
1215:                parent.appendChild(child1);
1216:                parent.appendChild(child2);
1217:                parent.appendChild(child3);
1218:
1219:                Nodes result = child1.query("preceding-sibling::*");
1220:                assertEquals(0, result.size());
1221:                result = child2.query("preceding-sibling::*");
1222:                assertEquals(1, result.size());
1223:                assertEquals(child1, result.get(0));
1224:                result = child3.query("preceding-sibling::*");
1225:                assertEquals(2, result.size());
1226:
1227:            }
1228:
1229:            public void testAttributeHasNoSiblings() {
1230:
1231:                Element parent = new Element("Test");
1232:                Element child1 = new Element("child1");
1233:                Element child2 = new Element("child2");
1234:                Element child3 = new Element("child3");
1235:                parent.appendChild(child1);
1236:                parent.appendChild(child2);
1237:                parent.appendChild(child3);
1238:                Attribute a1 = new Attribute("a1", "value");
1239:                Attribute a2 = new Attribute("a2", "value");
1240:                Attribute a3 = new Attribute("a3", "value");
1241:                child2.addAttribute(a1);
1242:                child2.addAttribute(a2);
1243:                child2.addAttribute(a3);
1244:
1245:                Nodes result = a2.query("preceding-sibling::node()");
1246:                assertEquals(0, result.size());
1247:                result = a2.query("following-sibling::node()");
1248:                assertEquals(0, result.size());
1249:
1250:            }
1251:
1252:            public void testIDFunction() {
1253:
1254:                Element parent = new Element("Test");
1255:                Element child1 = new Element("child1");
1256:                Element child2 = new Element("child2");
1257:                Element child3 = new Element("child3");
1258:                Attribute id = new Attribute("a", "anchor");
1259:                id.setType(Attribute.Type.ID);
1260:                child2.addAttribute(id);
1261:
1262:                parent.appendChild(child1);
1263:                parent.appendChild(child2);
1264:                parent.appendChild(child3);
1265:
1266:                Nodes result = parent.query("id('anchor')");
1267:                assertEquals(1, result.size());
1268:                assertEquals(child2, result.get(0));
1269:
1270:            }
1271:
1272:            public void testIDQueryOnDocumentNode() {
1273:
1274:                Element parent = new Element("Test");
1275:                Element child1 = new Element("child1");
1276:                Element child2 = new Element("child2");
1277:                Element child3 = new Element("child3");
1278:                Attribute id = new Attribute("a", "anchor");
1279:                id.setType(Attribute.Type.ID);
1280:                child2.addAttribute(id);
1281:
1282:                parent.appendChild(child1);
1283:                parent.appendChild(child2);
1284:                parent.appendChild(child3);
1285:                Document doc = new Document(parent);
1286:
1287:                Nodes result = doc.query("id('anchor')");
1288:                assertEquals(1, result.size());
1289:                assertEquals(child2, result.get(0));
1290:
1291:            }
1292:
1293:            public void testIDFunctionWithoutType() {
1294:
1295:                Element parent = new Element("Test");
1296:                Element child1 = new Element("child1");
1297:                Element child2 = new Element("child2");
1298:                Element child3 = new Element("child3");
1299:                Attribute id = new Attribute("id", "anchor");
1300:                child2.addAttribute(id);
1301:
1302:                parent.appendChild(child1);
1303:                parent.appendChild(child2);
1304:                parent.appendChild(child3);
1305:
1306:                Nodes result = parent.query("id('anchor')");
1307:                assertEquals(0, result.size());
1308:
1309:            }
1310:
1311:            public void testIDFunctionFromTextNode() {
1312:
1313:                Element parent = new Element("Test");
1314:                Element child1 = new Element("child1");
1315:                Element child2 = new Element("child2");
1316:                Element child3 = new Element("child3");
1317:                Text text = new Text("test");
1318:                child3.appendChild(text);
1319:                Attribute id = new Attribute("a", "anchor");
1320:                id.setType(Attribute.Type.ID);
1321:                child2.addAttribute(id);
1322:
1323:                parent.appendChild(child1);
1324:                parent.appendChild(child2);
1325:                parent.appendChild(child3);
1326:
1327:                Nodes result = text.query("id('anchor')");
1328:                assertEquals(1, result.size());
1329:                assertEquals(child2, result.get(0));
1330:
1331:            }
1332:
1333:            public void testIDFunctionFromUnparentedTextNode() {
1334:
1335:                Text text = new Text("test");
1336:                Nodes result = text.query("id('anchor')");
1337:                assertEquals(0, result.size());
1338:
1339:            }
1340:
1341:            public void testIDFunctionFromDisconnectedTextNode() {
1342:
1343:                Text text = new Text("test");
1344:                Nodes result = text.query("id('anchor')");
1345:                assertEquals(0, result.size());
1346:
1347:            }
1348:
1349:            public void testFollowingSiblingAxis() {
1350:
1351:                Element parent = new Element("Test");
1352:                Element child1 = new Element("child1");
1353:                Element child2 = new Element("child2");
1354:                Element child3 = new Element("child3");
1355:                parent.appendChild(child1);
1356:                parent.appendChild(child2);
1357:                parent.appendChild(child3);
1358:
1359:                Nodes result = child3.query("following-sibling::*");
1360:                assertEquals(0, result.size());
1361:                result = child2.query("following-sibling::*");
1362:                assertEquals(1, result.size());
1363:                assertEquals(child3, result.get(0));
1364:                result = child1.query("following-sibling::*");
1365:                assertEquals(2, result.size());
1366:
1367:            }
1368:
1369:            public void testNamespaceQuery() {
1370:
1371:                Element parent = new Element("Test", "http://www.example.org");
1372:                Element child = new Element("child", "http://www.example.org");
1373:                parent.appendChild(child);
1374:
1375:                XPathContext context = new XPathContext("pre",
1376:                        "http://www.example.org");
1377:                Nodes result = parent.query("child::pre:child", context);
1378:                assertEquals(1, result.size());
1379:                assertEquals(child, result.get(0));
1380:
1381:            }
1382:
1383:            public void testNamespaceAxis() {
1384:
1385:                Element parent = new Element("Test", "http://www.example.org");
1386:
1387:                Nodes result = parent.query("namespace::*");
1388:                assertEquals(2, result.size());
1389:                Namespace n1 = (Namespace) result.get(0);
1390:                Namespace n2 = (Namespace) result.get(1);
1391:                assertTrue(n1.getPrefix().equals("")
1392:                        || n2.getPrefix().equals(""));
1393:                assertTrue(n1.getPrefix().equals("xml")
1394:                        || n2.getPrefix().equals("xml"));
1395:                assertTrue(n1.getValue().equals("http://www.example.org")
1396:                        || n2.getValue().equals("http://www.example.org"));
1397:                assertTrue(n1.getValue().equals(Namespace.XML_NAMESPACE)
1398:                        || n2.getValue().equals(Namespace.XML_NAMESPACE));
1399:
1400:            }
1401:
1402:            public void testNamespaceAxisFromNonElement() {
1403:
1404:                Text text = new Text("test");
1405:
1406:                Nodes result = text.query("namespace::*");
1407:                assertEquals(0, result.size());
1408:
1409:            }
1410:
1411:            public void testPredicateWithNamespaceAxis() {
1412:
1413:                Element parent = new Element("Test");
1414:                Element child = new Element("child", "http://www.example.com");
1415:                Element grandchild = new Element("child",
1416:                        "http://www.example.com");
1417:                grandchild.addNamespaceDeclaration("pre", "http://www.w3.org/");
1418:                parent.appendChild(child);
1419:                child.appendChild(grandchild);
1420:
1421:                // Every node has at least a mapping for xml prefix.
1422:                Nodes result = parent.query("self::*[count(namespace::*)=0]");
1423:                assertEquals(0, result.size());
1424:
1425:                result = parent.query("self::*[count(namespace::*)=1]");
1426:                assertEquals(1, result.size());
1427:                assertEquals(parent, result.get(0));
1428:
1429:                result = child.query("self::*[count(namespace::*)=2]");
1430:                assertEquals(1, result.size());
1431:                assertEquals(child, result.get(0));
1432:
1433:                result = grandchild.query("self::*[count(namespace::*)=3]");
1434:                assertEquals(1, result.size());
1435:                assertEquals(grandchild, result.get(0));
1436:
1437:            }
1438:
1439:            public void testPredicateWithNamespaceAxis2() {
1440:
1441:                Element parent = new Element("Test");
1442:                Element child = new Element("child", "http://www.example.com");
1443:                Element grandchild = new Element("child",
1444:                        "http://www.example.com");
1445:                grandchild.addNamespaceDeclaration("pre", "http://www.w3.org/");
1446:                parent.appendChild(child);
1447:                child.appendChild(grandchild);
1448:
1449:                // Every node has at least a mapping for xml prefix.
1450:                Nodes result = parent.query("*[count(namespace::*)=0]");
1451:                assertEquals(0, result.size());
1452:
1453:                result = parent.query(".//self::*[count(namespace::*)=1]");
1454:                assertEquals(1, result.size());
1455:                assertEquals(parent, result.get(0));
1456:
1457:                result = parent.query(".//*[count(namespace::*)=2]");
1458:                assertEquals(1, result.size());
1459:                assertEquals(child, result.get(0));
1460:
1461:                result = parent.query(".//*[count(namespace::*)=3]");
1462:                assertEquals(1, result.size());
1463:                assertEquals(grandchild, result.get(0));
1464:
1465:            }
1466:
1467:            public void testNamespaceQueryWithNullPrefix() {
1468:
1469:                try {
1470:                    XPathContext context = new XPathContext("pre",
1471:                            "http://www.example.org");
1472:                    context.addNamespace(null, "http://www.w3.org");
1473:                    fail("Allowed null prefix");
1474:                } catch (NullPointerException success) {
1475:                    assertNotNull(success.getMessage());
1476:                }
1477:
1478:            }
1479:
1480:            public void testNamespaceQueryWithNullPrefix2() {
1481:
1482:                try {
1483:                    new XPathContext(null, "http://www.example.org");
1484:                    fail("Allowed null prefix");
1485:                } catch (NullPointerException success) {
1486:                    assertNotNull(success.getMessage());
1487:                }
1488:
1489:            }
1490:
1491:            public void testNamespaceQueryWithEmptyPrefix() {
1492:
1493:                try {
1494:                    XPathContext context = new XPathContext("pre",
1495:                            "http://www.example.org");
1496:                    context.addNamespace("", "http://www.w3.org");
1497:                } catch (NamespaceConflictException success) {
1498:                    assertTrue(success.getMessage().length() > 1);
1499:                }
1500:
1501:            }
1502:
1503:            public void testNamespaceQueryWithEmptyPrefix2() {
1504:
1505:                try {
1506:                    new XPathContext("", "http://www.example.org");
1507:                } catch (NamespaceConflictException success) {
1508:                    assertTrue(success.getMessage().length() > 1);
1509:                }
1510:
1511:            }
1512:
1513:            public void testNamespaceQueryWithNullURI() {
1514:
1515:                Element parent = new Element("Test", "http://www.example.org");
1516:                Element child = new Element("child", "http://www.example.org");
1517:                parent.appendChild(child);
1518:
1519:                XPathContext context = new XPathContext("pre", null);
1520:                try {
1521:                    parent.query("child::pre:child", context);
1522:                    fail("Allowed null URI");
1523:                } catch (XPathException success) {
1524:                    assertNotNull(success.getCause());
1525:                    assertNotNull(success.getMessage());
1526:                }
1527:
1528:            }
1529:
1530:            public void testNamespaceQueryWithEmptyURI() {
1531:
1532:                Element parent = new Element("Test", "http://www.example.org");
1533:                Element child = new Element("child", "http://www.example.org");
1534:                parent.appendChild(child);
1535:
1536:                XPathContext context = new XPathContext("pre", "");
1537:                try {
1538:                    parent.query("child::pre:child", context);
1539:                    fail("Allowed empty string as namespace URI");
1540:                } catch (XPathException success) {
1541:                    assertNotNull(success.getCause());
1542:                    assertNotNull(success.getMessage());
1543:                }
1544:
1545:            }
1546:
1547:            public void testNamespaceQueryWithReboundPrefix() {
1548:
1549:                Element parent = new Element("Test", "http://www.example.org");
1550:                Element child = new Element("child", "http://www.example.org");
1551:                parent.appendChild(child);
1552:
1553:                XPathContext context = new XPathContext("pre",
1554:                        "http://www.example.com");
1555:                Nodes result = parent.query("child::pre:child", context);
1556:                assertEquals(0, result.size());
1557:
1558:                context.addNamespace("pre", "http://www.example.org");
1559:                result = parent.query("child::pre:child", context);
1560:                assertEquals(1, result.size());
1561:                assertEquals(child, result.get(0));
1562:
1563:            }
1564:
1565:            public void testNamespaceQueryWithUnboundPrefix() {
1566:
1567:                Element parent = new Element("Test", "http://www.example.org");
1568:                Element child = new Element("child", "http://www.example.org");
1569:                parent.appendChild(child);
1570:
1571:                XPathContext context = new XPathContext("not",
1572:                        "http://www.example.com");
1573:                try {
1574:                    parent.query("child::pre:child", context);
1575:                    fail("Queried with unbound prefix");
1576:                } catch (XPathException success) {
1577:                    assertNotNull(success.getMessage());
1578:                    assertNotNull(success.getCause());
1579:                }
1580:
1581:                try {
1582:                    parent.query("child::pre:child");
1583:                    fail("Queried with unbound prefix");
1584:                } catch (XPathException success) {
1585:                    assertNotNull(success.getMessage());
1586:                    assertNotNull(success.getCause());
1587:                }
1588:
1589:            }
1590:
1591:            public void testElementBasedNamespaceContext() {
1592:
1593:                Element parent = new Element("Test", "http://www.example.org");
1594:                Element child = new Element("child", "http://www.example.org");
1595:                parent.appendChild(child);
1596:
1597:                Element test = new Element("pre:test", "http://www.example.org");
1598:                XPathContext context = XPathContext.makeNamespaceContext(test);
1599:                Nodes result = parent.query("child::pre:child", context);
1600:                assertEquals(1, result.size());
1601:                assertEquals(child, result.get(0));
1602:
1603:            }
1604:
1605:            public void testAttributeBasedNamespaceContext() {
1606:
1607:                Element parent = new Element("Test", "http://www.example.org");
1608:                Element child = new Element("child", "http://www.example.org");
1609:                parent.appendChild(child);
1610:
1611:                Element test = new Element("test");
1612:                test.addAttribute(new Attribute("pre:test",
1613:                        "http://www.example.org", "value"));
1614:                XPathContext context = XPathContext.makeNamespaceContext(test);
1615:                Nodes result = parent.query("child::pre:child", context);
1616:                assertEquals(1, result.size());
1617:                assertEquals(child, result.get(0));
1618:
1619:            }
1620:
1621:            public void testAdditionalNamespaceBasedNamespaceContext() {
1622:
1623:                Element parent = new Element("Test", "http://www.example.org");
1624:                Element child = new Element("child", "http://www.example.org");
1625:                parent.appendChild(child);
1626:
1627:                Element test = new Element("test");
1628:                test.addNamespaceDeclaration("pre", "http://www.example.org");
1629:                XPathContext context = XPathContext.makeNamespaceContext(test);
1630:                Nodes result = parent.query("child::pre:child", context);
1631:                assertEquals(1, result.size());
1632:                assertEquals(child, result.get(0));
1633:
1634:            }
1635:
1636:            public void testAncestorElementBasedNamespaceContext() {
1637:
1638:                Element parent = new Element("Test", "http://www.example.org");
1639:                Element child = new Element("child", "http://www.example.org");
1640:                parent.appendChild(child);
1641:
1642:                Element test = new Element("pre:test", "http://www.example.org");
1643:                Element testChild = new Element("testchild");
1644:                test.appendChild(testChild);
1645:                XPathContext context = XPathContext
1646:                        .makeNamespaceContext(testChild);
1647:                Nodes result = parent.query("child::pre:child", context);
1648:                assertEquals(1, result.size());
1649:                assertEquals(child, result.get(0));
1650:
1651:            }
1652:
1653:            public void testAncestorAttributeBasedNamespaceContext() {
1654:
1655:                Element parent = new Element("Test", "http://www.example.org");
1656:                Element child = new Element("child", "http://www.example.org");
1657:                parent.appendChild(child);
1658:
1659:                Element test = new Element("test");
1660:                test.addAttribute(new Attribute("pre:test",
1661:                        "http://www.example.org", "value"));
1662:                Element testChild = new Element("testchild");
1663:                test.appendChild(testChild);
1664:                XPathContext context = XPathContext
1665:                        .makeNamespaceContext(testChild);
1666:                Nodes result = parent.query("child::pre:child", context);
1667:                assertEquals(1, result.size());
1668:                assertEquals(child, result.get(0));
1669:
1670:            }
1671:
1672:            public void testAncestorAdditionalNamespaceBasedNamespaceContext() {
1673:
1674:                Element parent = new Element("Test", "http://www.example.org");
1675:                Element child = new Element("child", "http://www.example.org");
1676:                parent.appendChild(child);
1677:
1678:                Element test = new Element("test");
1679:                test.addNamespaceDeclaration("pre", "http://www.example.org");
1680:                Element testChild = new Element("testchild");
1681:                test.appendChild(testChild);
1682:                XPathContext context = XPathContext
1683:                        .makeNamespaceContext(testChild);
1684:                Nodes result = parent.query("child::pre:child", context);
1685:                assertEquals(1, result.size());
1686:                assertEquals(child, result.get(0));
1687:
1688:            }
1689:
1690:            public void testPrefixedNamespaceQuery() {
1691:
1692:                Element parent = new Element("a:Test", "http://www.example.org");
1693:                Element child = new Element("b:child", "http://www.example.org");
1694:                Attribute att = new Attribute("c:dog",
1695:                        "http://www.cafeconleche.org/", "test");
1696:                parent.appendChild(child);
1697:                child.addAttribute(att);
1698:
1699:                XPathContext context = new XPathContext("pre",
1700:                        "http://www.example.org");
1701:                context.addNamespace("c", "http://www.cafeconleche.org/");
1702:                Nodes result = parent.query("child::pre:child", context);
1703:                assertEquals(1, result.size());
1704:                assertEquals(child, result.get(0));
1705:
1706:                result = child.query("@c:*", context);
1707:                assertEquals(1, result.size());
1708:                assertEquals(att, result.get(0));
1709:
1710:            }
1711:
1712:            public void testBradley() {
1713:
1714:                Element element = new Element("root");
1715:                Text t1 = new Text("makes ");
1716:                Text t2 = new Text("a");
1717:                Text t3 = new Text(" good");
1718:                Text t4 = new Text(" point.");
1719:                Element child = new Element("someElement");
1720:                Text t5 = new Text("  Yes");
1721:                Text t6 = new Text(" he");
1722:                Text t7 = new Text(" does!");
1723:                element.appendChild(t1);
1724:                element.appendChild(t2);
1725:                element.appendChild(t3);
1726:                element.appendChild(t4);
1727:                element.appendChild(child);
1728:                element.appendChild(t5);
1729:                element.appendChild(t6);
1730:                element.appendChild(t7);
1731:
1732:                Nodes result = element.query("./text()[contains(., 'o')]");
1733:                assertEquals(7, result.size());
1734:                assertEquals(t1, result.get(0));
1735:                assertEquals(t2, result.get(1));
1736:                assertEquals(t3, result.get(2));
1737:                assertEquals(t4, result.get(3));
1738:                assertEquals(t5, result.get(4));
1739:                assertEquals(t6, result.get(5));
1740:                assertEquals(t7, result.get(6));
1741:
1742:            }
1743:
1744:            public void testNamespaceQueryWithAdjacentTextNodes() {
1745:
1746:                Element parent = new Element("Test", "http://www.example.org");
1747:                Element child = new Element("child", "http://www.example.org");
1748:                parent.appendChild(child);
1749:                child.appendChild("1");
1750:                child.appendChild("2");
1751:
1752:                XPathContext context = new XPathContext("pre",
1753:                        "http://www.example.org");
1754:                Nodes result = parent.query("descendant::text()", context);
1755:                assertEquals(2, result.size());
1756:                assertEquals("1", result.get(0).getValue());
1757:                assertEquals("2", result.get(1).getValue());
1758:
1759:            }
1760:
1761:            public void testNamespaceQueryWithoutPrefixMapping() {
1762:
1763:                Element parent = new Element("Test", "http://www.example.org");
1764:                Element child = new Element("child", "http://www.example.org");
1765:                parent.appendChild(child);
1766:
1767:                Nodes result = parent.query("child");
1768:                assertEquals(0, result.size());
1769:
1770:            }
1771:
1772:            public void testAdjacentTextObjects() {
1773:
1774:                Element parent = new Element("Test");
1775:                parent.appendChild("test");
1776:                parent.appendChild("again");
1777:
1778:                Nodes result = parent.query("text()");
1779:                assertEquals(2, result.size());
1780:                assertEquals("test", result.get(0).getValue());
1781:                assertEquals("again", result.get(1).getValue());
1782:
1783:            }
1784:
1785:            public void testQueryCrossesAdjacentTextObjects() {
1786:
1787:                Element parent = new Element("Test");
1788:                parent.appendChild("test");
1789:                parent.appendChild("again");
1790:
1791:                Nodes result = parent.query("node()[contains(., 'tag')]");
1792:                assertEquals(2, result.size());
1793:                assertEquals("test", result.get(0).getValue());
1794:                assertEquals("again", result.get(1).getValue());
1795:
1796:            }
1797:
1798:            // According to section 5.7 of the XPath 1.0 spec,
1799:            // "As much character data as possible is grouped into each text 
1800:            // node: a text node never has an immediately following or 
1801:            // preceding sibling that is a text node."
1802:            public void testAdjacentTextNodes2() {
1803:
1804:                Element parent = new Element("Test");
1805:                parent.appendChild("test");
1806:                parent.appendChild("again");
1807:
1808:                Nodes result = parent.query("child::text()[1]");
1809:                assertEquals(2, result.size());
1810:                assertEquals("test", result.get(0).getValue());
1811:                assertEquals("again", result.get(1).getValue());
1812:
1813:            }
1814:
1815:            // According to section 5.7 of the XPath 1.0 spec,
1816:            // "A text node always has at least one character of data."
1817:            public void testEmptyTextNodes() {
1818:
1819:                Element parent = new Element("Test");
1820:                parent.appendChild("");
1821:
1822:                Nodes result = parent.query("child::text()");
1823:                assertEquals(0, result.size());
1824:
1825:            }
1826:
1827:            public void testEmptyTextFollowsNonEmptyText() {
1828:
1829:                Element parent = new Element("parent");
1830:                Text empty = new Text("");
1831:                Text nonempty = new Text("value");
1832:                parent.appendChild(nonempty);
1833:                parent.appendChild(empty);
1834:                Nodes result = parent.query("node()");
1835:                assertEquals(2, result.size());
1836:
1837:            }
1838:
1839:            public void testCountAdjacentEmptyAndNonEmptyTextNodes() {
1840:
1841:                Element parent = new Element("parent");
1842:                Text empty = new Text("");
1843:                Text nonempty = new Text("value");
1844:                parent.appendChild(nonempty);
1845:                parent.appendChild(empty);
1846:
1847:                Nodes result2 = parent.query("/*[count(node())=1]");
1848:                assertEquals(1, result2.size());
1849:                assertEquals(parent, result2.get(0));
1850:
1851:                Nodes nodes1 = parent.query("node()[1]");
1852:                assertEquals(2, nodes1.size());
1853:                Nodes nodes2 = parent.query("node()[2]");
1854:                assertEquals(0, nodes2.size());
1855:
1856:            }
1857:
1858:            public void testAdjacentEmptyAndNonEmptyTextNodes() {
1859:
1860:                Element parent = new Element("parent");
1861:                Text empty = new Text("");
1862:                Text nonempty = new Text("value");
1863:                Text nonempty2 = new Text("value2");
1864:                parent.appendChild(empty);
1865:                parent.appendChild(nonempty);
1866:                parent.appendChild(nonempty2);
1867:
1868:                Nodes result = parent.query("node()");
1869:                assertEquals(3, result.size());
1870:
1871:                Nodes result2 = parent.query("/*[count(node())=1]");
1872:                assertEquals(1, result2.size());
1873:                assertEquals(parent, result2.get(0));
1874:
1875:                Nodes nodes1 = parent.query("node()[1]");
1876:                assertEquals(3, nodes1.size());
1877:
1878:                Nodes nodes2 = parent.query("node()[2]");
1879:                assertEquals(0, nodes2.size());
1880:
1881:            }
1882:
1883:            public void testBadXPathExpression() {
1884:
1885:                Element parent = new Element("Test");
1886:
1887:                try {
1888:                    parent.query("This is not an XPath expression");
1889:                    fail("Allowed malformed query");
1890:                } catch (XPathException success) {
1891:                    assertNotNull(success.getMessage());
1892:                }
1893:
1894:            }
1895:
1896:            public void testNaNEvaluatesToFalse() {
1897:
1898:                Element root = new Element("root");
1899:                Document doc = new Document(root);
1900:
1901:                Nodes result = doc.query("/*[boolean(0 div 0)]");
1902:                assertEquals(0, result.size());
1903:
1904:            }
1905:
1906:            /* <body>
1907:            <p>
1908:            <span></span>
1909:            </p>
1910:            <div></div>
1911:            </body> */
1912:            public void testPrecedingAxis() {
1913:
1914:                Element body = new Element("body");
1915:                Element p = new Element("p");
1916:                body.appendChild(p);
1917:                Element span = new Element("span");
1918:                p.appendChild(span);
1919:                Element div = new Element("div");
1920:                body.appendChild(div);
1921:
1922:                Nodes result = div.query("preceding::*[1]");
1923:                assertEquals(1, result.size());
1924:                assertEquals(span, result.get(0));
1925:
1926:            }
1927:
1928:            public void testRootNodeValueIsNonEmpty() {
1929:
1930:                Element root = new Element("html");
1931:                Document doc = new Document(root);
1932:                root.appendChild("test");
1933:
1934:                Nodes result = doc.query("/*[string(/) != '']");
1935:                assertEquals(1, result.size());
1936:                assertEquals(root, result.get(0));
1937:
1938:            }
1939:
1940:            public void testContextPositionForParaentlessNodeIs1() {
1941:
1942:                Element root = new Element("html");
1943:
1944:                Nodes result = root.query("self::*[1]");
1945:                assertEquals(1, result.size());
1946:                assertEquals(root, result.get(0));
1947:
1948:            }
1949:
1950:            public void testContextSizeForParaentlessNodeIs1() {
1951:
1952:                Element root = new Element("html");
1953:
1954:                Nodes result = root.query("self::*[last()=1]");
1955:                assertEquals(1, result.size());
1956:                assertEquals(root, result.get(0));
1957:
1958:            }
1959:
1960:            public void testLastFunction() {
1961:
1962:                Element root = new Element("html");
1963:                Element child1 = new Element("child1");
1964:                Element child2 = new Element("child2");
1965:                root.appendChild(child1);
1966:                root.appendChild(child2);
1967:                new Document(root);
1968:
1969:                Nodes result = child2.query("self::*[position()=last()]");
1970:                assertEquals(1, result.size());
1971:                assertEquals(child2, result.get(0));
1972:
1973:            }
1974:
1975:            public void testJaxen51() throws ParsingException, IOException {
1976:
1977:                String data = "<root>\n <servlet-mapping>\n"
1978:                        + "  <servlet-name>DeviceInfoServlet</servlet-name>\n"
1979:                        + "  <url-pattern>/tools/*</url-pattern>\n"
1980:                        + "</servlet-mapping>\n</root>";
1981:
1982:                Builder builder = new Builder();
1983:                Document doc = builder.build(data, null);
1984:
1985:                Nodes result = doc
1986:                        .query("//*[./../servlet-name = 'DeviceInfoServlet']");
1987:                assertEquals(2, result.size());
1988:
1989:            }
1990:
1991:            public void testXPathNamespaceParentage() throws ParsingException,
1992:                    IOException {
1993:
1994:                String input = "<!DOCTYPE doc [\n"
1995:                        + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n"
1996:                        + "<!ATTLIST e3 id ID #IMPLIED>\n"
1997:                        + "]>\n"
1998:                        + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
1999:                        + "   <e1>\n" + "      <e2 xmlns=\"\">\n"
2000:                        + "         <e3 id=\"E3\"/>\n" + "      </e2>\n"
2001:                        + "   </e1>\n" + "</doc>";
2002:
2003:                Document doc = (new Builder()).build(input, null);
2004:                String xpath = "(/*/* | /*/*/namespace::*)\n";
2005:                Nodes result = doc.query(xpath);
2006:                assertEquals(4, result.size());
2007:                Element parent = (Element) result.get(0);
2008:                for (int i = 1; i < 4; i++) {
2009:                    Namespace namespace = (Namespace) result.get(i);
2010:                    assertEquals(parent, namespace.getParent());
2011:                }
2012:
2013:            }
2014:
2015:            public void testAttributesFollowElementsInDocumentOrder()
2016:                    throws ParsingException, IOException {
2017:
2018:                String input = "<document>\n" + "   <e1 a='b' c='d' e='f'>\n"
2019:                        + "      <e2>\n" + "         <e3 id=\"E3\"/>\n"
2020:                        + "      </e2>\n" + "   </e1>\n" + "</document>";
2021:
2022:                Document doc = (new Builder()).build(input, null);
2023:                String xpath = "(/*/* | /*/*/attribute::*)\n";
2024:                Nodes result = doc.query(xpath);
2025:                assertEquals(4, result.size());
2026:                Element parent = (Element) result.get(0);
2027:                for (int i = 1; i < 4; i++) {
2028:                    Attribute attribute = (Attribute) result.get(i);
2029:                    assertEquals(parent, attribute.getParent());
2030:                }
2031:
2032:            }
2033:
2034:            public void testDoubleSlashIsIncorrect() {
2035:
2036:                Element root = new Element("root", "http://www.example.org");
2037:                Document doc = new Document(root);
2038:                root.appendChild(new Element("child"));
2039:                root.appendChild("test");
2040:                root.addAttribute(new Attribute("test", "test"));
2041:
2042:                try {
2043:                    doc.query("//");
2044:                    fail("Queried //");
2045:                } catch (XPathException success) {
2046:                    assertNotNull(success.getMessage());
2047:                }
2048:
2049:            }
2050:
2051:            public void testDoubleSlashIsIncorrect2() {
2052:
2053:                Element root = new Element("root", "http://www.example.org");
2054:                Document doc = new Document(root);
2055:                root.appendChild(new Element("child"));
2056:                root.appendChild("test");
2057:                root.addAttribute(new Attribute("test", "test"));
2058:
2059:                try {
2060:                    doc.query("// ");
2061:                    fail("Queried // ");
2062:                } catch (XPathException success) {
2063:                    assertNotNull(success.getMessage());
2064:                }
2065:
2066:            }
2067:
2068:            public void testWhiteSpaceIsAllowedBetweenTokens() {
2069:
2070:                Element root = new Element("root", "http://www.example.org");
2071:                Document doc = new Document(root);
2072:                root.appendChild(new Element("child"));
2073:                root.appendChild("test");
2074:                root.addAttribute(new Attribute("test", "test"));
2075:
2076:                Nodes nodes = doc.query("// a");
2077:                assertEquals(0, nodes.size());
2078:
2079:            }
2080:
2081:            /*    public void testMassimo() throws ParsingException, IOException {
2082:            
2083:             Builder builder = new Builder();
2084:             Document doc = builder.build("http://staff.science.uva.nl/~francesc/xpathmark/benchmark_canon.xml");
2085:             Element root = doc.getRootElement();
2086:             Element input_1 = root.getFirstChildElement("document_1");
2087:             Element input_2 = root.getFirstChildElement("document_2");
2088:            
2089:             Nodes queries = root.query("child::query[starts-with('@id', 'Q')]");
2090:             for (int i = 0; i < queries.size(); i++) {
2091:             Element query = (Element) queries.get(i);
2092:             String xpath = query.getFirstChildElement("syntax").getValue();
2093:             Nodes actual = input_1.query(xpath);
2094:             Elements expected = query.getChildElements();
2095:             }
2096:            
2097:             } */
2098:
2099:            public void testJaxenIntegrationTest() throws ParsingException,
2100:                    IOException {
2101:
2102:                Builder builder = new Builder();
2103:                Document testDoc = builder
2104:                        .build("http://cvs.jaxen.codehaus.org/viewrep/~raw,r=HEAD/jaxen/jaxen/xml/test/tests.xml");
2105:                Elements documents = testDoc.getRootElement().getChildElements(
2106:                        "document");
2107:                for (int i = 0; i < documents.size(); i++) {
2108:                    Element documentElement = documents.get(i);
2109:                    String url = documentElement.getAttributeValue("url");
2110:                    Document source = builder
2111:                            .build("http://cvs.jaxen.codehaus.org/viewrep/~raw,r=HEAD/jaxen/jaxen/"
2112:                                    + url);
2113:                    Elements contextElements = documentElement
2114:                            .getChildElements("context");
2115:                    for (int j = 0; j < contextElements.size(); j++) {
2116:                        Element contextElement = contextElements.get(j);
2117:
2118:                        // skip tests that use variables
2119:                        // because XOM doesn't support variables in 
2120:                        // XPath expressions
2121:                        if (queryUsesVars(contextElement))
2122:                            continue;
2123:
2124:                        String xpath = contextElement
2125:                                .getAttributeValue("select");
2126:                        XPathContext namespaces = getXPathContext(contextElement);
2127:                        Node context = source.query(xpath).get(0);
2128:
2129:                        // process counts
2130:                        Elements tests = contextElement
2131:                                .getChildElements("test");
2132:                        for (int k = 0; k < tests.size(); k++) {
2133:                            Element test = tests.get(k);
2134:
2135:                            String select = test.getAttributeValue("select");
2136:                            Attribute countAttribute = test
2137:                                    .getAttribute("count");
2138:                            int count = -1;
2139:                            if (countAttribute != null) {
2140:                                count = Integer.parseInt(countAttribute
2141:                                        .getValue());
2142:                            }
2143:
2144:                            boolean exceptional = false;
2145:                            String exception = test
2146:                                    .getAttributeValue("exception");
2147:                            if ("true".equals(exception)) {
2148:                                exceptional = true;
2149:                            }
2150:
2151:                            if (exceptional) {
2152:                                try {
2153:                                    context.query(select, namespaces);
2154:                                    fail("Evaluated " + select);
2155:                                } catch (XPathException success) {
2156:                                    assertNotNull(success.getMessage());
2157:                                }
2158:                            } else {
2159:                                try {
2160:                                    Nodes results = context.query(select,
2161:                                            namespaces);
2162:                                    if (count != -1) {
2163:                                        assertEquals(select, count, results
2164:                                                .size());
2165:                                    }
2166:                                    Elements valueOfs = test
2167:                                            .getChildElements("valueOf");
2168:                                    for (int v = 0; v < valueOfs.size(); v++) {
2169:                                        Element vo = valueOfs.get(v);
2170:                                        checkValueOf(results.get(0), vo,
2171:                                                namespaces);
2172:                                    }
2173:                                } catch (XPathException ex) {
2174:                                    if (ex
2175:                                            .getMessage()
2176:                                            .equalsIgnoreCase(
2177:                                                    "XPath error: No such function document")
2178:                                            || ex
2179:                                                    .getMessage()
2180:                                                    .equalsIgnoreCase(
2181:                                                            "XPath error: No such function evaluate")) {
2182:                                        continue;
2183:                                    }
2184:                                    throw ex;
2185:                                }
2186:
2187:                            }
2188:                        }
2189:
2190:                        // process valueOfs
2191:                        Elements valueOfs = contextElement
2192:                                .getChildElements("valueOf");
2193:                        for (int k = 0; k < valueOfs.size(); k++) {
2194:                            checkValueOf(context, valueOfs.get(k), namespaces);
2195:                        }
2196:
2197:                    }
2198:                }
2199:            }
2200:
2201:            private void checkValueOf(Node context, Element valueOf,
2202:                    XPathContext namespaces) {
2203:
2204:                String select = valueOf.getAttributeValue("select");
2205:                try {
2206:                    Nodes nodes = context.query(select, namespaces);
2207:                    String result = nodes.get(0).getValue();
2208:                    assertEquals(valueOf.getValue(), result);
2209:                } catch (XPathTypeException ex) {
2210:                    assertNotNull(ex.getMessage());
2211:                } catch (XPathException ex) {
2212:                    if (ex.getMessage().equalsIgnoreCase(
2213:                            "XPath error: No such function document")
2214:                            || ex.getMessage().equalsIgnoreCase(
2215:                                    "XPath error: No such function upper-case")
2216:                            || ex.getMessage().equalsIgnoreCase(
2217:                                    "XPath error: No such function lower-case")
2218:                            || ex.getMessage().equalsIgnoreCase(
2219:                                    "XPath error: No such function ends-with")) {
2220:                        return;
2221:                    }
2222:                    throw ex;
2223:                }
2224:
2225:            }
2226:
2227:            private XPathContext getXPathContext(Element contextElement) {
2228:
2229:                XPathContext context = new XPathContext();
2230:                for (int i = 0; i < contextElement
2231:                        .getNamespaceDeclarationCount(); i++) {
2232:                    String prefix = contextElement.getNamespacePrefix(i);
2233:                    if (!"".equals(prefix)) {
2234:                        context.addNamespace(prefix, contextElement
2235:                                .getNamespaceURI(prefix));
2236:                    }
2237:                }
2238:                return context;
2239:            }
2240:
2241:            private boolean queryUsesVars(Element testElement) {
2242:
2243:                for (int i = 0; i < testElement.getAttributeCount(); i++) {
2244:                    Attribute a = testElement.getAttribute(i);
2245:                    if ("http://jaxen.org/test-harness/var".equals(a
2246:                            .getNamespaceURI())) {
2247:                        return true;
2248:                    }
2249:                }
2250:                return false;
2251:            }
2252:
2253:            public void testTextChildInPredicate() {
2254:
2255:                Element item = new Element("item");
2256:                Document doc = new Document(item);
2257:                Element name = new Element("name");
2258:                name.appendChild("a");
2259:                item.appendChild(name);
2260:                Element value = new Element("value");
2261:                value.appendChild("b");
2262:                item.appendChild(value);
2263:                Nodes result = doc.query("/item[name/text()='a']/value");
2264:                assertEquals(1, result.size());
2265:                assertEquals("b", result.get(0).getValue());
2266:
2267:            }
2268:
2269:            public void testSimpleTextChildInPredicate() {
2270:
2271:                Element item = new Element("item");
2272:                Document doc = new Document(item);
2273:                item.appendChild("a");
2274:                Nodes result = doc.query("/item[text()='a']");
2275:                assertEquals(1, result.size());
2276:
2277:            }
2278:
2279:            public void testPrecedingAxisInDocumentOrder() {
2280:
2281:                Element root = new Element("root");
2282:                new Document(root);
2283:
2284:                Element a = new Element("a");
2285:                root.appendChild(a);
2286:                Element b = new Element("b");
2287:                root.appendChild(b);
2288:                Element c = new Element("c");
2289:                a.appendChild(c);
2290:
2291:                Nodes result = b.query("preceding::*");
2292:                assertEquals(2, result.size());
2293:                assertEquals(a, result.get(0));
2294:                assertEquals(c, result.get(1));
2295:
2296:            }
2297:
2298:            public void testAttributeWithUnderscore() {
2299:
2300:                Element a = new Element("a");
2301:                Attribute foo = new Attribute("_foo", "bar");
2302:                a.addAttribute(foo);
2303:                Nodes results = a.query("//@_foo");
2304:                assertEquals(1, results.size());
2305:                assertEquals(foo, results.get(0));
2306:
2307:            }
2308:
2309:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.