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  
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       * Helper, returns a {@link JXPathContext} filled with a Map whose "value" key is associated to the passed {@code val} value.
36       *
37       * @param val
38       * @return A {@link JXPathContext}, never {@code null}.
39       */
40      private JXPathContext getContext(final String val, final boolean lenient) {
41          final HashMap map = new HashMap();
42          // if (val!=null) // no diffs
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 }