1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42 public class TestConfigurationAttributePointer {
43
44 private static final String ATTR_NAME = "myAttr";
45
46
47 private static final String ATTR_VALUE = "myValue";
48
49
50 private ConfigurationNodePointer<ImmutableNode> parent;
51
52
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
66
67 @Test
68 public void testAttributeIterator() {
69 assertNull(pointer.attributeIterator(new QName(null, "test")));
70 }
71
72
73
74
75 @Test
76 public void testChildIterator() {
77 assertNull(pointer.childIterator(null, false, null));
78 }
79
80
81
82
83 @Test
84 public void testGetBaseValue() {
85 assertEquals(ATTR_VALUE, pointer.getBaseValue());
86 }
87
88
89
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
102
103 @Test
104 public void testGetLength() {
105 assertEquals(1, pointer.getLength());
106 }
107
108
109
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
120
121 @Test
122 public void testGetParentPointer() {
123 assertSame(parent, pointer.getParentPointer());
124 }
125
126
127
128
129 @Test
130 public void testGetValue() {
131 assertEquals(ATTR_VALUE, pointer.getValue());
132 }
133
134
135
136
137 @Test
138 public void testIsAttribute() {
139 assertTrue(pointer.isAttribute());
140 }
141
142
143
144
145 @Test
146 public void testIsCollection() {
147 assertFalse(pointer.isCollection());
148 }
149
150
151
152
153 @Test
154 public void testIsLeaf() {
155 assertTrue(pointer.isLeaf());
156 }
157
158
159
160
161 @Test
162 public void testSetValue() {
163 assertThrows(UnsupportedOperationException.class, () -> pointer.setValue("newValue"));
164 }
165
166
167
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 }