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 import static org.junit.jupiter.api.Assertions.assertNull;
23
24 import java.util.HashMap;
25
26 import org.apache.commons.jxpath.AbstractJXPathTest;
27 import org.apache.commons.jxpath.JXPathContext;
28 import org.apache.commons.jxpath.Pointer;
29 import org.apache.commons.jxpath.ri.model.dynamic.DynamicPropertyPointer;
30 import org.junit.jupiter.api.Test;
31
32 public class JXPath172DynamicTest extends AbstractJXPathTest {
33
34
35
36
37
38
39
40 private JXPathContext getContext(final String val, final boolean lenient) {
41 final HashMap map = new HashMap();
42
43 map.put("value", val);
44 final Object target = map;
45 final JXPathContext context = JXPathContext.newContext(null, target);
46 context.setLenient(lenient);
47 return context;
48 }
49
50 @Test
51 public void testIssue172_nestedpropertyDoesNotExist_Lenient() {
52 final JXPathContext context = getContext(null, true);
53 final Object bRet = context.selectSingleNode("value.unexisting");
54 assertNull(bRet);
55 final Pointer pointer = context.getPointer("value.unexisting");
56 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
57 assertNull(pointer.getValue());
58 }
59
60 @Test
61 public void testIssue172_nestedpropertyDoesNotExist_NotLenient() {
62 final JXPathContext context = getContext(null, false);
63 final Object bRet = context.selectSingleNode("value.unexisting");
64 assertNull(bRet);
65 final Pointer pointer = context.getPointer("value.unexisting");
66 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
67 assertNull(pointer.getValue());
68 }
69
70 @Test
71 public void testIssue172_propertyDoesNotExist() {
72 final JXPathContext context = getContext(null, false);
73 final Object bRet = context.selectSingleNode("unexisting");
74 assertNull(bRet);
75 final Pointer pointer = context.getPointer("unexisting");
76 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
77 assertNull(pointer.getValue());
78 }
79
80 @Test
81 public void testIssue172_propertyDoesNotExist_Lenient() {
82 final JXPathContext context = getContext(null, true);
83 final Object bRet = context.selectSingleNode("unexisting");
84 assertNull(bRet);
85 final Pointer pointer = context.getPointer("unexisting");
86 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
87 assertNull(pointer.getValue());
88 }
89
90 @Test
91 public void testIssue172_propertyExistAndIsNotNull() {
92 final JXPathContext context = getContext("ciao", false);
93 final Object bRet = context.selectSingleNode("value");
94 assertNotNull(bRet, "null!!");
95 assertEquals("ciao", bRet, "Is " + bRet.getClass());
96 final Pointer pointer = context.getPointer("value");
97 assertNotNull(pointer);
98 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
99 assertEquals("ciao", pointer.getValue());
100 }
101
102 @Test
103 public void testIssue172_propertyExistAndIsNull() {
104 final JXPathContext context = getContext(null, false);
105 final Object bRet = context.selectSingleNode("value");
106 assertNull(bRet, "not null!!");
107 final Pointer pointer = context.getPointer("value");
108 assertNotNull(pointer);
109 assertEquals(DynamicPropertyPointer.class, pointer.getClass());
110 assertNull(pointer.getValue());
111 }
112 }