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