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    *     https://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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.util.Locale;
28  
29  import org.apache.commons.configuration2.tree.ImmutableNode;
30  import org.apache.commons.configuration2.tree.InMemoryNodeModel;
31  import org.apache.commons.configuration2.tree.QueryResult;
32  import org.apache.commons.jxpath.ri.Compiler;
33  import org.apache.commons.jxpath.ri.QName;
34  import org.apache.commons.jxpath.ri.compiler.NodeTest;
35  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Test class for {@code ConfigurationAttributePointer}.
41   */
42  public class TestConfigurationAttributePointer {
43  
44      /** Constant for the name of the test attribute. */
45      private static final String ATTR_NAME = "myAttr";
46  
47      /** Constant for the value of the test attribute. */
48      private static final String ATTR_VALUE = "myValue";
49  
50      /** Stores the parent node pointer. */
51      private ConfigurationNodePointer<ImmutableNode> parent;
52  
53      /** The attribute pointer to be tested. */
54      private ConfigurationAttributePointer<ImmutableNode> pointer;
55  
56      @BeforeEach
57      public void setUp() throws Exception {
58          final ImmutableNode.Builder ndBuilder = new ImmutableNode.Builder();
59          ndBuilder.name("parent").addAttribute(ATTR_NAME, ATTR_VALUE);
60          final ImmutableNode nd = ndBuilder.create();
61          parent = new ConfigurationNodePointer<>(nd, Locale.ROOT, new InMemoryNodeModel(nd).getNodeHandler());
62          pointer = new ConfigurationAttributePointer<>(parent, ATTR_NAME);
63      }
64  
65      /**
66       * Tests querying an iterator for attributes. Result should be null.
67       */
68      @Test
69      void testAttributeIterator() {
70          assertNull(pointer.attributeIterator(new QName(null, "test")));
71      }
72  
73      /**
74       * Tests querying an iterator for children. Result should be null.
75       */
76      @Test
77      void testChildIterator() {
78          assertNull(pointer.childIterator(null, false, null));
79      }
80  
81      /**
82       * Tests querying the base value.
83       */
84      @Test
85      void testGetBaseValue() {
86          assertEquals(ATTR_VALUE, pointer.getBaseValue());
87      }
88  
89      /**
90       * Tests querying the immediate node. Here a proxy for an attribute node should be returned.
91       */
92      @Test
93      void testGetImmediateNode() {
94          final Object node = pointer.getImmediateNode();
95          final QueryResult<?> proxy = assertInstanceOf(QueryResult.class, node);
96          assertTrue(proxy.isAttributeResult());
97          assertEquals(parent.getConfigurationNode(), proxy.getNode());
98          assertEquals(ATTR_NAME, proxy.getAttributeName());
99      }
100 
101     /**
102      * Tests the length.
103      */
104     @Test
105     void testGetLength() {
106         assertEquals(1, pointer.getLength());
107     }
108 
109     /**
110      * Tests querying the node name.
111      */
112     @Test
113     void testGetName() {
114         final QName qName = pointer.getName();
115         assertEquals(ATTR_NAME, qName.getName());
116         assertNull(qName.getPrefix());
117     }
118 
119     /**
120      * Tests whether the correct pointer is returned.
121      */
122     @Test
123     void testGetParentPointer() {
124         assertSame(parent, pointer.getParentPointer());
125     }
126 
127     /**
128      * Tests querying the attribute's value.
129      */
130     @Test
131     void testGetValue() {
132         assertEquals(ATTR_VALUE, pointer.getValue());
133     }
134 
135     /**
136      * Tests the attribute flag.
137      */
138     @Test
139     void testIsAttribute() {
140         assertTrue(pointer.isAttribute());
141     }
142 
143     /**
144      * Tests the collection flag.
145      */
146     @Test
147     void testIsCollection() {
148         assertFalse(pointer.isCollection());
149     }
150 
151     /**
152      * Tests the leaf flag.
153      */
154     @Test
155     void testIsLeaf() {
156         assertTrue(pointer.isLeaf());
157     }
158 
159     /**
160      * Tries to set a new value.
161      */
162     @Test
163     void testSetValue() {
164         assertThrows(UnsupportedOperationException.class, () -> pointer.setValue("newValue"));
165     }
166 
167     /**
168      * Tests the testNode() method.
169      */
170     @Test
171     void testTestNode() {
172         NodeTest test = new NodeTypeTest(Compiler.NODE_TYPE_TEXT);
173         assertTrue(pointer.testNode(test));
174         test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
175         assertFalse(pointer.testNode(test));
176     }
177 }