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  package org.apache.commons.configuration2.tree.xpath;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Set;
26  
27  import org.apache.commons.configuration2.tree.ImmutableNode;
28  import org.apache.commons.jxpath.ri.QName;
29  import org.apache.commons.jxpath.ri.model.NodePointer;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test class for {@code ConfigurationNodeIteratorAttributes}.
35   */
36  public class TestConfigurationIteratorAttributes extends AbstractXPathTest {
37      /** Constant for the name of another test attribute. */
38      private static final String TEST_ATTR = "test";
39  
40      /** Constant for a namespace prefix. */
41      private static final String NAMESPACE = "commons";
42  
43      /** Constant for an attribute with a namespace prefix. */
44      private static final String NS_ATTR = NAMESPACE + ":attr";
45  
46      /** Stores the node pointer of the test node. */
47      private ConfigurationNodePointer<ImmutableNode> pointer;
48  
49      @Override
50      @BeforeEach
51      public void setUp() throws Exception {
52          super.setUp();
53  
54          // Adds further attributes to the test node
55          final ImmutableNode orgNode = root.getChildren().get(1);
56          final ImmutableNode testNode = orgNode.setAttribute(TEST_ATTR, "yes").setAttribute(NS_ATTR, "configuration");
57          pointer = new ConfigurationNodePointer<>(testNode, Locale.getDefault(), handler);
58      }
59  
60      /**
61       * Tests to iterate over all attributes.
62       */
63      @Test
64      public void testIterateAllAttributes() {
65          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, "*"));
66          assertEquals(3, iteratorSize(it));
67          final List<NodePointer> attrs = iterationElements(it);
68          final Set<String> attrNames = new HashSet<>();
69          for (final NodePointer np : attrs) {
70              attrNames.add(np.getName().getName());
71          }
72          assertTrue(attrNames.contains(ATTR_NAME));
73          assertTrue(attrNames.contains(TEST_ATTR));
74          assertTrue(attrNames.contains(NS_ATTR));
75      }
76  
77      /**
78       * Tests whether a specific attribute with a namespace can be selected.
79       */
80      @Test
81      public void testIterateNamespaceAttribute() {
82          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(NAMESPACE, "attr"));
83          assertEquals(1, iteratorSize(it));
84          assertEquals(NS_ATTR, iterationElements(it).get(0).getName().getName());
85      }
86  
87      /**
88       * Tests iteration if an unknown namespace is specified.
89       */
90      @Test
91      public void testIterateNamespaceUnknown() {
92          final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName("test", "*"));
93          assertEquals(0, iteratorSize(it));
94      }
95  
96      /**
97       * Tests whether a wildcard can be used together with a namespace.
98       */
99      @Test
100     public void testIterateNamespaceWildcard() {
101         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(NAMESPACE, "*"));
102         assertEquals(1, iteratorSize(it));
103         assertEquals(NS_ATTR, iterationElements(it).get(0).getName().getName());
104     }
105 
106     /**
107      * Tests to iterate over attributes with a specific name.
108      */
109     @Test
110     public void testIterateSpecificAttribute() {
111         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, TEST_ATTR));
112         assertEquals(1, iteratorSize(it));
113         assertEquals(TEST_ATTR, iterationElements(it).get(0).getName().getName());
114     }
115 
116     /**
117      * Tests to iterate over non existing attributes.
118      */
119     @Test
120     public void testIterateUnknownAttribute() {
121         final ConfigurationNodeIteratorAttribute<ImmutableNode> it = new ConfigurationNodeIteratorAttribute<>(pointer, new QName(null, "unknown"));
122         assertEquals(0, iteratorSize(it));
123     }
124 }