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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.configuration.tree;
19
import static org.junit.Assert.assertEquals;
20
import static org.junit.Assert.assertFalse;
21
import static org.junit.Assert.assertNull;
22
import static org.junit.Assert.assertTrue;
23
import static org.junit.Assert.fail;
25
import java.util.NoSuchElementException;
27
import org.junit.Before;
28
import org.junit.Test;
31
* Test class for DefaultConfigurationKey.
33
* @author Oliver Heger
34
* @version $Id: TestDefaultConfigurationKey.java 1225914 2011-12-30 20:26:36Z oheger $
36
public class TestDefaultConfigurationKey
38
/** Constant for a test key. */
39
private static final String TESTPROPS = "tables.table(0).fields.field(1)";
41
/** Constant for a test attribute key. */
42
private static final String TESTATTR = "[@dataType]";
44
/** Constant for a complex attribute key. */
45
private static final String TESTKEY = TESTPROPS + TESTATTR;
47
/** Stores the expression engine of the key to test. */
48
DefaultExpressionEngine expressionEngine;
50
/** Stores the object to be tested. */
51
DefaultConfigurationKey key;
54
public void setUp() throws Exception
56
expressionEngine = new DefaultExpressionEngine();
57
key = new DefaultConfigurationKey(expressionEngine);
61
* Tests setting the expression engine to null. This should not be allowed.
63
@Test(expected = IllegalArgumentException.class)
64
public void testSetNullExpressionEngine()
66
key.setExpressionEngine(null);
70
* Tests the isAttributeKey() method with several keys.
73
public void testIsAttributeKey()
75
assertTrue("Attribute key not detected", key.isAttributeKey(TESTATTR));
76
assertFalse("Property key considered as attribute", key
77
.isAttributeKey(TESTPROPS));
78
assertFalse("Null key considered as attribute", key
79
.isAttributeKey(null));
83
* Tests if attribute keys are correctly detected if no end markers are set.
84
* (In this test case we use the same delimiter for attributes as for simple
88
public void testIsAttributeKeyWithoutEndMarkers()
90
expressionEngine.setAttributeEnd(null);
92
.setAttributeStart(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER);
94
"Attribute key not detected",
96
.isAttributeKey(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER
98
assertFalse("Property key considered as attribute key", key
99
.isAttributeKey(TESTATTR));
103
* Tests removing leading delimiters.
106
public void testTrimLeft()
108
assertEquals("Key was not left trimmed", "test.", key
109
.trimLeft(".test."));
110
assertEquals("Too much left trimming", "..test.", key
111
.trimLeft("..test."));
115
* Tests removing trailing delimiters.
118
public void testTrimRight()
120
assertEquals("Key was not right trimmed", ".test", key
121
.trimRight(".test."));
122
assertEquals("Too much right trimming", ".test..", key
123
.trimRight(".test.."));
127
* Tests removing delimiters.
130
public void testTrim()
132
assertEquals("Key was not trimmed", "test", key.trim(".test."));
133
assertEquals("Null key could not be processed", "", key.trim(null));
134
assertEquals("Delimiter could not be processed", "", key
135
.trim(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER));
139
* Tests appending keys.
142
public void testAppend()
144
key.append("tables").append("table(0).");
145
key.append("fields.").append("field(1)");
146
key.append(null).append(TESTATTR);
147
assertEquals("Wrong key", TESTKEY, key.toString());
151
* Tests appending keys that contain delimiters.
154
public void testAppendDelimiters()
156
key.append("key..").append("test").append(".");
157
key.append(".more").append("..tests");
158
assertEquals("Wrong key", "key...test.more...tests", key.toString());
162
* Tests appending keys that contain delimiters when no escpaped delimiter
166
public void testAppendDelimitersWithoutEscaping()
168
expressionEngine.setEscapedDelimiter(null);
169
key.append("key.......").append("test").append(".");
170
key.append(".more").append("..tests");
171
assertEquals("Wrong constructed key", "key.test.more.tests", key
176
* Tests calling append with the escape flag.
179
public void testAppendWithEscapeFlag()
181
key.append(".key.test.", true);
182
key.append(".more").append(".tests", true);
183
assertEquals("Wrong constructed key", "..key..test...more...tests", key
188
* Tests constructing keys for attributes.
191
public void testConstructAttributeKey()
193
assertEquals("Wrong attribute key", TESTATTR, key
194
.constructAttributeKey("dataType"));
195
assertEquals("Attribute key was incorrectly converted", TESTATTR, key
196
.constructAttributeKey(TESTATTR));
197
assertEquals("Null key could not be processed", "", key
198
.constructAttributeKey(null));
202
* Tests constructing attribute keys when no end markers are defined. In
203
* this test case we use the property delimiter as attribute prefix.
206
public void testConstructAttributeKeyWithoutEndMarkers()
208
expressionEngine.setAttributeEnd(null);
209
expressionEngine.setAttributeStart(expressionEngine
210
.getPropertyDelimiter());
211
assertEquals("Wrong attribute key", ".test", key
212
.constructAttributeKey("test"));
213
assertEquals("Attribute key was incorrectly converted", ".test", key
214
.constructAttributeKey(".test"));
218
* Tests appending attribute keys.
221
public void testAppendAttribute()
223
key.appendAttribute("dataType");
224
assertEquals("Attribute key not correctly appended", TESTATTR, key
229
* Tests appending an attribute key that is already decorated-
232
public void testAppendDecoratedAttributeKey()
234
key.appendAttribute(TESTATTR);
235
assertEquals("Decorated attribute key not correctly appended",
236
TESTATTR, key.toString());
240
* Tests appending a null attribute key.
243
public void testAppendNullAttributeKey()
245
key.appendAttribute(null);
246
assertEquals("Null attribute key not correctly appended", "", key
251
* Tests appending an index to a key.
254
public void testAppendIndex()
256
key.append("test").appendIndex(42);
257
assertEquals("Index was not correctly appended", "test(42)", key
262
* Tests constructing a complex key by chaining multiple append operations.
265
public void testAppendComplexKey()
267
key.append("tables").append("table.").appendIndex(0);
268
key.append("fields.").append("field").appendIndex(1);
269
key.appendAttribute("dataType");
270
assertEquals("Wrong complex key", TESTKEY, key.toString());
274
* Tests getting and setting the key's length.
277
public void testLength()
279
key.append(TESTPROPS);
280
assertEquals("Wrong length", TESTPROPS.length(), key.length());
281
key.appendAttribute("dataType");
282
assertEquals("Wrong length", TESTKEY.length(), key.length());
283
key.setLength(TESTPROPS.length());
284
assertEquals("Wrong length after shortening", TESTPROPS.length(), key
286
assertEquals("Wrong resulting key", TESTPROPS, key.toString());
290
* Tests comparing configuration keys.
293
public void testEquals()
295
DefaultConfigurationKey k1 = new DefaultConfigurationKey(
296
expressionEngine, TESTKEY);
297
DefaultConfigurationKey k2 = new DefaultConfigurationKey(
298
expressionEngine, TESTKEY);
299
assertTrue("Keys are not equal", k1.equals(k2));
300
assertTrue("Not reflexiv", k2.equals(k1));
301
assertEquals("Hash codes not equal", k1.hashCode(), k2.hashCode());
302
k2.append("anotherPart");
303
assertFalse("Keys considered equal", k1.equals(k2));
304
assertFalse("Keys considered equal", k2.equals(k1));
305
assertFalse("Key equals null key", k1.equals(null));
306
assertTrue("Faild comparison with string", k1.equals(TESTKEY));
310
* Tests determining an attribute key's name.
313
public void testAttributeName()
315
assertEquals("Plain key not detected", "test", key
316
.attributeName("test"));
317
assertEquals("Attribute markers not stripped", "dataType", key
318
.attributeName(TESTATTR));
319
assertNull("Null key not processed", key.attributeName(null));
323
* Tests to iterate over a simple key.
326
public void testIterate()
329
DefaultConfigurationKey.KeyIterator it = key.iterator();
330
assertTrue("No key parts", it.hasNext());
331
assertEquals("Wrong key part", "tables", it.nextKey());
332
assertEquals("Wrong key part", "table", it.nextKey());
333
assertTrue("No index found", it.hasIndex());
334
assertEquals("Wrong index", 0, it.getIndex());
335
assertEquals("Wrong key part", "fields", it.nextKey());
336
assertFalse("Found an index", it.hasIndex());
337
assertEquals("Wrong key part", "field", it.nextKey(true));
338
assertEquals("Wrong index", 1, it.getIndex());
339
assertFalse("Found an attribute", it.isAttribute());
340
assertEquals("Wrong current key", "field", it.currentKey(true));
341
assertEquals("Wrong key part", "dataType", it.nextKey());
342
assertEquals("Wrong decorated key part", "[@dataType]", it
344
assertTrue("Attribute not found", it.isAttribute());
345
assertFalse("Too many key parts", it.hasNext());
349
fail("Could iterate over the iteration's end!");
351
catch (NoSuchElementException nex)
358
* Tests an iteration where the remove() method is called. This is not
361
@Test(expected = UnsupportedOperationException.class)
362
public void testIterateWithRemove()
364
assertFalse(key.iterator().hasNext());
365
key.append("simple");
366
DefaultConfigurationKey.KeyIterator it = key.iterator();
367
assertTrue(it.hasNext());
368
assertEquals("simple", it.next());
373
* Tests iterating over some funny keys.
376
public void testIterateStrangeKeys()
378
key = new DefaultConfigurationKey(expressionEngine, "key.");
379
DefaultConfigurationKey.KeyIterator it = key.iterator();
380
assertTrue("Too few key parts", it.hasNext());
381
assertEquals("Wrong key part", "key", it.next());
382
assertFalse("Too many key parts", it.hasNext());
384
key = new DefaultConfigurationKey(expressionEngine, ".");
386
assertFalse("Simple delimiter key has more parts", it.hasNext());
388
key = new DefaultConfigurationKey(expressionEngine,
389
"key().index()undefined(0).test");
391
assertEquals("Wrong first part", "key()", it.next());
392
assertFalse("Index detected in first part", it.hasIndex());
393
assertEquals("Wrong second part", "index()undefined", it.nextKey(false));
394
assertTrue("No index detected in second part", it.hasIndex());
395
assertEquals("Wrong index value", 0, it.getIndex());
399
* Tests iterating over keys with escaped delimiters.
402
public void testIterateEscapedDelimiters()
404
key.append("my..elem");
405
key.append("trailing..dot..");
406
key.append(".strange");
407
assertEquals("my..elem.trailing..dot...strange", key.toString());
408
DefaultConfigurationKey.KeyIterator kit = key.iterator();
409
assertEquals("Wrong first part", "my.elem", kit.nextKey());
410
assertEquals("Wrong second part", "trailing.dot.", kit.nextKey());
411
assertEquals("Wrong third part", "strange", kit.nextKey());
412
assertFalse("Too many parts", kit.hasNext());
416
* Tests iterating over keys when a different escaped delimiter is used.
419
public void testIterateAlternativeEscapeDelimiter()
421
expressionEngine.setEscapedDelimiter("\\.");
422
key.append("\\.my\\.elem");
423
key.append("trailing\\.dot\\.");
424
key.append(".strange");
425
assertEquals("\\.my\\.elem.trailing\\.dot\\..strange", key.toString());
426
DefaultConfigurationKey.KeyIterator kit = key.iterator();
427
assertEquals("Wrong first part", ".my.elem", kit.nextKey());
428
assertEquals("Wrong second part", "trailing.dot.", kit.nextKey());
429
assertEquals("Wrong third part", "strange", kit.nextKey());
430
assertFalse("Too many parts", kit.hasNext());
434
* Tests iterating when no escape delimiter is defined.
437
public void testIterateWithoutEscapeDelimiter()
439
expressionEngine.setEscapedDelimiter(null);
440
key.append("..my..elem.trailing..dot...strange");
441
assertEquals("Wrong key", "my..elem.trailing..dot...strange", key
443
DefaultConfigurationKey.KeyIterator kit = key.iterator();
444
final String[] parts =
445
{ "my", "elem", "trailing", "dot", "strange"};
446
for (int i = 0; i < parts.length; i++)
448
assertEquals("Wrong key part " + i, parts[i], kit.next());
450
assertFalse("Too many parts", kit.hasNext());
454
* Tests whether a key with brackets in it can be iterated over.
457
public void testIterateWithBrackets()
459
key.append("directory.platform(x86).path");
460
DefaultConfigurationKey.KeyIterator kit = key.iterator();
461
String part = kit.nextKey();
462
assertEquals("Wrong part 1", "directory", part);
463
assertFalse("Has index 1", kit.hasIndex());
464
part = kit.nextKey();
465
assertEquals("Wrong part 2", "platform(x86)", part);
466
assertFalse("Has index 2", kit.hasIndex());
467
part = kit.nextKey();
468
assertEquals("Wrong part 3", "path", part);
469
assertFalse("Has index 3", kit.hasIndex());
470
assertFalse("Too many elements", kit.hasNext());
474
* Tests iterating over an attribute key that has an index.
477
public void testAttributeKeyWithIndex()
479
key.append(TESTATTR);
481
assertEquals("Wrong attribute key with index", TESTATTR + "(0)", key
484
DefaultConfigurationKey.KeyIterator it = key.iterator();
485
assertTrue("No first element", it.hasNext());
487
assertTrue("Index not found", it.hasIndex());
488
assertEquals("Incorrect index", 0, it.getIndex());
489
assertTrue("Attribute not found", it.isAttribute());
490
assertEquals("Wrong plain key", "dataType", it.currentKey(false));
491
assertEquals("Wrong decorated key", TESTATTR, it.currentKey(true));
495
* Tests iteration when the attribute markers equals the property delimiter.
498
public void testIterateAttributeEqualsPropertyDelimiter()
500
expressionEngine.setAttributeEnd(null);
501
expressionEngine.setAttributeStart(expressionEngine
502
.getPropertyDelimiter());
503
key.append("this.isa.key");
504
DefaultConfigurationKey.KeyIterator kit = key.iterator();
505
assertEquals("Wrong first key part", "this", kit.next());
506
assertFalse("First part is an attribute", kit.isAttribute());
507
assertTrue("First part is not a property key", kit.isPropertyKey());
508
assertEquals("Wrong second key part", "isa", kit.next());
509
assertFalse("Second part is an attribute", kit.isAttribute());
510
assertTrue("Second part is not a property key", kit.isPropertyKey());
511
assertEquals("Wrong third key part", "key", kit.next());
512
assertTrue("Third part is not an attribute", kit.isAttribute());
513
assertTrue("Third part is not a property key", kit.isPropertyKey());
514
assertEquals("Wrong decorated key part", "key", kit.currentKey(true));