1 package org.apache.commons.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.NoSuchElementException;
26
27 import org.junit.Test;
28
29
30
31
32
33
34 @SuppressWarnings("deprecation")
35 public class TestConfigurationKey
36 {
37 private static final String TESTPROPS = "tables.table(0).fields.field(1)";
38
39 private static final String TESTATTR = "[@dataType]";
40
41 private static final String TESTKEY = TESTPROPS + TESTATTR;
42
43 @Test
44 public void testAppend()
45 {
46 ConfigurationKey key = new ConfigurationKey();
47 key.append("tables").append("table.").appendIndex(0);
48 key.append("fields.").append("field").appendIndex(1);
49 key.appendAttribute("dataType");
50 assertEquals(TESTKEY, key.toString());
51 }
52
53 @Test
54 public void testIterate()
55 {
56 ConfigurationKey key = new ConfigurationKey(TESTKEY);
57 ConfigurationKey.KeyIterator it = key.iterator();
58 assertTrue(it.hasNext());
59 assertEquals("tables", it.nextKey());
60 assertEquals("table", it.nextKey());
61 assertTrue(it.hasIndex());
62 assertEquals(0, it.getIndex());
63 assertEquals("fields", it.nextKey());
64 assertFalse(it.hasIndex());
65 assertEquals("field", it.nextKey(true));
66 assertEquals(1, it.getIndex());
67 assertFalse(it.isAttribute());
68 assertEquals("field", it.currentKey(true));
69 assertEquals("dataType", it.nextKey());
70 assertEquals("[@dataType]", it.currentKey(true));
71 assertTrue(it.isAttribute());
72 assertFalse(it.hasNext());
73 try
74 {
75 it.next();
76 fail("Could iterate over the iteration's end!");
77 }
78 catch(NoSuchElementException nex)
79 {
80
81 }
82
83 key = new ConfigurationKey();
84 assertFalse(key.iterator().hasNext());
85 key.append("simple");
86 it = key.iterator();
87 assertTrue(it.hasNext());
88 assertEquals("simple", it.next());
89 try
90 {
91 it.remove();
92 fail("Could remove key component!");
93 }
94 catch(UnsupportedOperationException uex)
95 {
96
97 }
98 }
99
100 @Test
101 public void testAttribute()
102 {
103 assertTrue(ConfigurationKey.isAttributeKey(TESTATTR));
104 assertFalse(ConfigurationKey.isAttributeKey(TESTPROPS));
105 assertFalse(ConfigurationKey.isAttributeKey(TESTKEY));
106
107 ConfigurationKey key = new ConfigurationKey(TESTPROPS);
108 key.append(TESTATTR);
109 assertEquals(TESTKEY, key.toString());
110 }
111
112 @Test
113 public void testLength()
114 {
115 ConfigurationKey key = new ConfigurationKey(TESTPROPS);
116 assertEquals(TESTPROPS.length(), key.length());
117 key.appendAttribute("dataType");
118 assertEquals(TESTKEY.length(), key.length());
119 key.setLength(TESTPROPS.length());
120 assertEquals(TESTPROPS.length(), key.length());
121 assertEquals(TESTPROPS, key.toString());
122 }
123
124 @Test
125 public void testConstructAttributeKey()
126 {
127 assertEquals("[@attribute]", ConfigurationKey.constructAttributeKey("attribute"));
128 assertEquals("attribute", ConfigurationKey.attributeName("[@attribute]"));
129 assertEquals("attribute", ConfigurationKey.attributeName("attribute"));
130 }
131
132 @Test
133 public void testEquals()
134 {
135 ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
136 ConfigurationKey k2 = new ConfigurationKey(TESTKEY);
137 assertTrue(k1.equals(k2));
138 assertTrue(k2.equals(k1));
139 assertEquals(k1.hashCode(), k2.hashCode());
140 k2.append("anotherPart");
141 assertFalse(k1.equals(k2));
142 assertFalse(k2.equals(k1));
143 assertFalse(k1.equals(null));
144 assertTrue(k1.equals(TESTKEY));
145 }
146
147 @Test
148 public void testCommonKey()
149 {
150 ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
151 ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
152 ConfigurationKey kc = k1.commonKey(k2);
153 assertEquals(new ConfigurationKey("tables.table(0)"), kc);
154 assertEquals(kc, k2.commonKey(k1));
155
156 k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
157 kc = k1.commonKey(k2);
158 assertEquals(new ConfigurationKey("tables"), kc);
159
160 k2 = new ConfigurationKey("completely.different.key");
161 kc = k1.commonKey(k2);
162 assertEquals(0, kc.length());
163
164 k2 = new ConfigurationKey();
165 kc = k1.commonKey(k2);
166 assertEquals(0, kc.length());
167
168 kc = k1.commonKey(k1);
169 assertEquals(kc, k1);
170
171 try
172 {
173 kc.commonKey(null);
174 fail("Could construct common key with null key!");
175 }
176 catch(IllegalArgumentException iex)
177 {
178
179 }
180 }
181
182 @Test
183 public void testDifferenceKey()
184 {
185 ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
186 ConfigurationKey kd = k1.differenceKey(k1);
187 assertEquals(0, kd.length());
188
189 ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
190 kd = k1.differenceKey(k2);
191 assertEquals("name", kd.toString());
192
193 k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
194 kd = k1.differenceKey(k2);
195 assertEquals("table(1).fields.field(1)", kd.toString());
196
197 k2 = new ConfigurationKey("completely.different.key");
198 kd = k1.differenceKey(k2);
199 assertEquals(k2, kd);
200 }
201
202 @Test
203 public void testEscapedDelimiters()
204 {
205 ConfigurationKey k = new ConfigurationKey();
206 k.append("my..elem");
207 k.append("trailing..dot..");
208 k.append("strange");
209 assertEquals("my..elem.trailing..dot...strange", k.toString());
210
211 ConfigurationKey.KeyIterator kit = k.iterator();
212 assertEquals("my.elem", kit.nextKey());
213 assertEquals("trailing.dot.", kit.nextKey());
214 assertEquals("strange", kit.nextKey());
215 assertFalse(kit.hasNext());
216 }
217
218
219
220
221 @Test
222 public void testIterateStrangeKeys()
223 {
224 ConfigurationKey k = new ConfigurationKey("key.");
225 ConfigurationKey.KeyIterator it = k.iterator();
226 assertTrue(it.hasNext());
227 assertEquals("key", it.next());
228 assertFalse(it.hasNext());
229
230 k = new ConfigurationKey(".");
231 it = k.iterator();
232 assertFalse(it.hasNext());
233
234 k = new ConfigurationKey("key().index()undefined(0).test");
235 it = k.iterator();
236 assertEquals("key()", it.next());
237 assertFalse(it.hasIndex());
238 assertEquals("index()undefined", it.nextKey(false));
239 assertTrue(it.hasIndex());
240 assertEquals(0, it.getIndex());
241 }
242
243
244
245
246 @Test
247 public void testAttributeKeyWithIndex()
248 {
249 ConfigurationKey k = new ConfigurationKey(TESTATTR);
250 k.appendIndex(0);
251 assertEquals("Wrong attribute key with index", TESTATTR + "(0)", k.toString());
252
253 ConfigurationKey.KeyIterator it = k.iterator();
254 assertTrue("No first element", it.hasNext());
255 it.next();
256 assertTrue("Index not found", it.hasIndex());
257 assertEquals("Incorrect index", 0, it.getIndex());
258 assertTrue("Attribute not found", it.isAttribute());
259 assertEquals("Wrong plain key", "dataType", it.currentKey(false));
260 assertEquals("Wrong decorated key", TESTATTR, it.currentKey(true));
261 }
262 }