View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * Helper, returns a {@link JXPathContext} filled with {@link TestBean172} whose {@link TestBean172#getValue()} method returns the passed {@code val} value.
50       *
51       * @param val
52       * @return A {@link JXPathContext}, never {@code null}.
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 }