1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.model;
19
20 import org.apache.commons.jxpath.AbstractJXPathTest;
21 import org.apache.commons.jxpath.JXPathContext;
22 import org.apache.commons.jxpath.xml.DocumentContainer;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 public class ExternalXMLNamespaceTest extends AbstractJXPathTest {
29
30 protected JXPathContext context;
31
32 protected JXPathContext createContext(final String model) {
33 final JXPathContext context = JXPathContext.newContext(createDocumentContainer(model));
34 context.registerNamespace("A", "foo");
35 context.registerNamespace("B", "bar");
36 return context;
37 }
38
39 protected DocumentContainer createDocumentContainer(final String model) {
40 final DocumentContainer result = new DocumentContainer(AbstractJXPathTest.class.getResource("ExternalNS.xml"), model);
41
42 result.setNamespaceAware(false);
43 return result;
44 }
45
46 protected void doTest(final String xpath, final String model, final String expected) {
47 assertXPathValue(createContext(model), xpath, expected);
48 }
49
50 protected void doTestAttribute(final String model) {
51 doTest("/ElementA/@A:myAttr", model, "Mytype");
52 }
53
54 protected void doTestCreateAndSetAttribute(final String model) {
55 assertXPathCreatePathAndSetValue(createContext(model), "/ElementA/@A:newAttr", "newValue", "/ElementA[1]/@A:newAttr");
56 }
57
58 protected void doTestElement(final String model) {
59 doTest("/ElementA/B:ElementB", model, "MY VALUE");
60 }
61
62 @Test
63 public void testAttributeDOM() {
64 doTestAttribute(DocumentContainer.MODEL_DOM);
65 }
66
67 @Test
68 public void testCreateAndSetAttributeDOM() {
69 doTestCreateAndSetAttribute(DocumentContainer.MODEL_DOM);
70 }
71
72 @Test
73 public void testElementDOM() {
74 doTestElement(DocumentContainer.MODEL_DOM);
75 }
76 }