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