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;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  import org.w3c.dom.Element;
28  
29  /**
30   * Test BasicNodeSet
31   */
32  public class BasicNodeSetTest extends AbstractJXPathTest {
33  
34      /** JXPathContext */
35      protected JXPathContext context;
36      /** BasicNodeSet */
37      protected BasicNodeSet nodeSet;
38  
39      /**
40       * Add the pointers for the specified path to {@code nodeSet}.
41       *
42       * @param xpath
43       */
44      protected void addPointers(final String xpath) {
45          for (final Iterator<Pointer> iter = context.iteratePointers(xpath); iter.hasNext();) {
46              nodeSet.add(iter.next());
47          }
48          nudge();
49      }
50  
51      /**
52       * Do assertions on DOM element names.
53       *
54       * @param names    List of expected names
55       * @param elements List of DOM elements
56       */
57      protected void assertElementNames(final List names, final List elements) {
58          assertEquals(names.size(), elements.size());
59          final Iterator nameIter = names.iterator();
60          final Iterator elementIter = elements.iterator();
61          while (elementIter.hasNext()) {
62              assertEquals(nameIter.next(), ((Element) elementIter.next()).getTagName());
63          }
64      }
65  
66      /**
67       * Do assertions on DOM element values.
68       *
69       * @param values   List of expected values
70       * @param elements List of DOM elements
71       */
72      protected void assertElementValues(final List values, final List elements) {
73          assertEquals(values.size(), elements.size());
74          final Iterator valueIter = values.iterator();
75          final Iterator elementIter = elements.iterator();
76          while (elementIter.hasNext()) {
77              assertEquals(valueIter.next(), ((Element) elementIter.next()).getFirstChild().getNodeValue());
78          }
79      }
80  
81      /**
82       * "Nudge" the nodeSet.
83       */
84      protected void nudge() {
85          nodeSet.getPointers();
86          nodeSet.getValues();
87          nodeSet.getNodes();
88      }
89  
90      /**
91       * Remove the pointers for the specified path from {@code nodeSet}.
92       *
93       * @param xpath
94       */
95      protected void removePointers(final String xpath) {
96          for (final Iterator<Pointer> iter = context.iteratePointers(xpath); iter.hasNext();) {
97              nodeSet.remove(iter.next());
98          }
99          nudge();
100     }
101 
102     @Override
103     @BeforeEach
104     protected void setUp() throws Exception {
105         super.setUp();
106         context = JXPathContext.newContext(new TestMixedModelBean());
107         nodeSet = new BasicNodeSet();
108     }
109 
110     /**
111      * Test adding pointers.
112      */
113     @Test
114     public void testAdd() {
115         addPointers("/bean/integers");
116         assertEquals(list("/bean/integers[1]", "/bean/integers[2]", "/bean/integers[3]", "/bean/integers[4]").toString(), nodeSet.getPointers().toString());
117         assertEquals(list(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)), nodeSet.getValues());
118         assertEquals(list(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)), nodeSet.getNodes());
119     }
120 
121     /**
122      * Demonstrate when nodes != values: in XML models.
123      */
124     @Test
125     public void testNodes() {
126         addPointers("/document/vendor/contact");
127         assertEquals(
128                 list("/document/vendor[1]/contact[1]", "/document/vendor[1]/contact[2]", "/document/vendor[1]/contact[3]", "/document/vendor[1]/contact[4]")
129                         .toString(),
130                 nodeSet.getPointers().toString());
131         assertEquals(list("John", "Jack", "Jim", "Jack Black"), nodeSet.getValues());
132         assertElementNames(list("contact", "contact", "contact", "contact"), nodeSet.getNodes());
133         assertElementValues(list("John", "Jack", "Jim", "Jack Black"), nodeSet.getNodes());
134     }
135 
136     /**
137      * Test removing a pointer.
138      */
139     @Test
140     public void testRemove() {
141         addPointers("/bean/integers");
142         removePointers("/bean/integers[4]");
143         assertEquals(list("/bean/integers[1]", "/bean/integers[2]", "/bean/integers[3]").toString(), nodeSet.getPointers().toString());
144         assertEquals(list(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)), nodeSet.getValues());
145         assertEquals(list(Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)), nodeSet.getNodes());
146     }
147 }