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  
22  import java.util.Iterator;
23  
24  import org.apache.commons.jxpath.JXPathContext;
25  import org.apache.commons.jxpath.Pointer;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Testcase proving JXPATH-118 issue with asPath() returning wrong names.
30   */
31  public class JXPath118Test {
32  
33      public static class SomeChildClass {
34  
35          private int foo = 1;
36          private int bar = 2;
37          private int baz = 3;
38  
39          public int getBar() {
40              return bar;
41          }
42  
43          public int getBaz() {
44              return baz;
45          }
46  
47          public int getFoo() {
48              return foo;
49          }
50  
51          public void setBar(final int bar) {
52              this.bar = bar;
53          }
54  
55          public void setBaz(final int baz) {
56              this.baz = baz;
57          }
58  
59          public void setFoo(final int foo) {
60              this.foo = foo;
61          }
62      }
63  
64      @Test
65      public void testJXPATH118IssueWithAsPath() throws Exception {
66          final Object contextBean = new SomeChildClass();
67          final JXPathContext context = JXPathContext.newContext(contextBean);
68          final Iterator<Pointer> iteratePointers = context.iteratePointers("//*");
69          assertEquals("/bar", iteratePointers.next().asPath());
70          assertEquals("/baz", iteratePointers.next().asPath());
71          assertEquals("/foo", iteratePointers.next().asPath());
72      }
73  }