1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.issues;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.commons.jxpath.JXPathContext;
27 import org.apache.commons.jxpath.Pointer;
28 import org.apache.commons.jxpath.Variables;
29 import org.junit.jupiter.api.Test;
30
31 public class JXPath177Test {
32
33 private static final class JXPathVariablesResolver implements Variables {
34
35 private static final long serialVersionUID = -1106360826446119597L;
36 public static final String ROOT_VAR = "__root";
37 private final Object root;
38
39 public JXPathVariablesResolver(final Object root) {
40 this.root = root;
41 }
42
43 @Override
44 public void declareVariable(final String varName, final Object value) {
45 throw new UnsupportedOperationException();
46 }
47
48 @Override
49 public Object getVariable(final String varName) {
50 if (varName == null) {
51 throw new IllegalArgumentException("varName");
52 }
53 if (!varName.equals(ROOT_VAR)) {
54 throw new IllegalArgumentException("Variable is not declared: " + varName);
55 }
56 return root;
57 }
58
59 @Override
60 public boolean isDeclaredVariable(final String varName) {
61 if (varName == null) {
62 throw new IllegalArgumentException("varName");
63 }
64 return varName.equals(ROOT_VAR);
65 }
66
67 @Override
68 public void undeclareVariable(final String varName) {
69 throw new UnsupportedOperationException();
70 }
71 }
72
73 Map model = new HashMap();
74 {
75 model.put("name", "ROOT name");
76 final HashMap x = new HashMap();
77 model.put("x", x);
78 x.put("name", "X name");
79 }
80
81 private void doTest(final String xp, final String expected) {
82 final JXPathContext xpathContext = JXPathContext.newContext(model);
83 xpathContext.setVariables(new JXPathVariablesResolver(model));
84 final Pointer p = xpathContext.getPointer(xp);
85 final Object result = p.getNode();
86 assertNotNull(result);
87 assertEquals(expected, result);
88 }
89
90 @Test
91 public void testJx177() {
92 doTest("name", "ROOT name");
93 doTest("/x/name", "X name");
94 doTest("$__root/x/name", "X name");
95 }
96
97 @Test
98 public void testJx177_Union1() {
99 doTest("$__root/x/name|name", "X name");
100 }
101
102 @Test
103 public void testJx177_Union2() {
104 doTest("$__root/x/unexisting|name", "ROOT name");
105 }
106 }