001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration.tree;
018:
019: import java.util.NoSuchElementException;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * Test class for DefaultConfigurationKey.
025: *
026: * @author Oliver Heger
027: * @version $Id: TestDefaultConfigurationKey.java 439648 2006-09-02 20:42:10Z oheger $
028: */
029: public class TestDefaultConfigurationKey extends TestCase {
030: /** Constant for a test key. */
031: private static final String TESTPROPS = "tables.table(0).fields.field(1)";
032:
033: /** Constant for a test attribute key. */
034: private static final String TESTATTR = "[@dataType]";
035:
036: /** Constant for a complex attribute key. */
037: private static final String TESTKEY = TESTPROPS + TESTATTR;
038:
039: /** Stores the expression engine of the key to test. */
040: DefaultExpressionEngine expressionEngine;
041:
042: /** Stores the object to be tested. */
043: DefaultConfigurationKey key;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: expressionEngine = new DefaultExpressionEngine();
048: key = new DefaultConfigurationKey(expressionEngine);
049: }
050:
051: /**
052: * Tests setting the expression engine to null. This should not be allowed.
053: */
054: public void testSetNullExpressionEngine() {
055: try {
056: key.setExpressionEngine(null);
057: fail("Could set null expression engine!");
058: } catch (IllegalArgumentException iex) {
059: // ok
060: }
061: }
062:
063: /**
064: * Tests the isAttributeKey() method with several keys.
065: */
066: public void testIsAttributeKey() {
067: assertTrue("Attribute key not detected", key
068: .isAttributeKey(TESTATTR));
069: assertFalse("Property key considered as attribute", key
070: .isAttributeKey(TESTPROPS));
071: assertFalse("Null key considered as attribute", key
072: .isAttributeKey(null));
073: }
074:
075: /**
076: * Tests if attribute keys are correctly detected if no end markers are set.
077: * (In this test case we use the same delimiter for attributes as for simple
078: * properties.)
079: */
080: public void testIsAttributeKeyWithoutEndMarkers() {
081: expressionEngine.setAttributeEnd(null);
082: expressionEngine
083: .setAttributeStart(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER);
084: assertTrue(
085: "Attribute key not detected",
086: key
087: .isAttributeKey(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER
088: + "test"));
089: assertFalse("Property key considered as attribute key", key
090: .isAttributeKey(TESTATTR));
091: }
092:
093: /**
094: * Tests removing leading delimiters.
095: */
096: public void testTrimLeft() {
097: assertEquals("Key was not left trimmed", "test.", key
098: .trimLeft(".test."));
099: assertEquals("Too much left trimming", "..test.", key
100: .trimLeft("..test."));
101: }
102:
103: /**
104: * Tests removing trailing delimiters.
105: */
106: public void testTrimRight() {
107: assertEquals("Key was not right trimmed", ".test", key
108: .trimRight(".test."));
109: assertEquals("Too much right trimming", ".test..", key
110: .trimRight(".test.."));
111: }
112:
113: /**
114: * Tests removing delimiters.
115: */
116: public void testTrim() {
117: assertEquals("Key was not trimmed", "test", key.trim(".test."));
118: assertEquals("Null key could not be processed", "", key
119: .trim(null));
120: assertEquals(
121: "Delimiter could not be processed",
122: "",
123: key
124: .trim(DefaultExpressionEngine.DEFAULT_PROPERTY_DELIMITER));
125: }
126:
127: /**
128: * Tests appending keys.
129: */
130: public void testAppend() {
131: key.append("tables").append("table(0).");
132: key.append("fields.").append("field(1)");
133: key.append(null).append(TESTATTR);
134: assertEquals("Wrong key", TESTKEY, key.toString());
135: }
136:
137: /**
138: * Tests appending keys that contain delimiters.
139: */
140: public void testAppendDelimiters() {
141: key.append("key..").append("test").append(".");
142: key.append(".more").append("..tests");
143: assertEquals("Wrong key", "key...test.more...tests", key
144: .toString());
145: }
146:
147: /**
148: * Tests appending keys that contain delimiters when no escpaped delimiter
149: * is defined.
150: */
151: public void testAppendDelimitersWithoutEscaping() {
152: expressionEngine.setEscapedDelimiter(null);
153: key.append("key.......").append("test").append(".");
154: key.append(".more").append("..tests");
155: assertEquals("Wrong constructed key", "key.test.more.tests",
156: key.toString());
157: }
158:
159: /**
160: * Tests calling append with the escape flag.
161: */
162: public void testAppendWithEscapeFlag() {
163: key.append(".key.test.", true);
164: key.append(".more").append(".tests", true);
165: assertEquals("Wrong constructed key",
166: "..key..test...more...tests", key.toString());
167: }
168:
169: /**
170: * Tests constructing keys for attributes.
171: */
172: public void testConstructAttributeKey() {
173: assertEquals("Wrong attribute key", TESTATTR, key
174: .constructAttributeKey("dataType"));
175: assertEquals("Attribute key was incorrectly converted",
176: TESTATTR, key.constructAttributeKey(TESTATTR));
177: assertEquals("Null key could not be processed", "", key
178: .constructAttributeKey(null));
179: }
180:
181: /**
182: * Tests constructing attribute keys when no end markers are defined. In
183: * this test case we use the property delimiter as attribute prefix.
184: */
185: public void testConstructAttributeKeyWithoutEndMarkers() {
186: expressionEngine.setAttributeEnd(null);
187: expressionEngine.setAttributeStart(expressionEngine
188: .getPropertyDelimiter());
189: assertEquals("Wrong attribute key", ".test", key
190: .constructAttributeKey("test"));
191: assertEquals("Attribute key was incorrectly converted",
192: ".test", key.constructAttributeKey(".test"));
193: }
194:
195: /**
196: * Tests appending attribute keys.
197: */
198: public void testAppendAttribute() {
199: key.appendAttribute("dataType");
200: assertEquals("Attribute key not correctly appended", TESTATTR,
201: key.toString());
202: }
203:
204: /**
205: * Tests appending an attribute key that is already decorated-
206: */
207: public void testAppendDecoratedAttributeKey() {
208: key.appendAttribute(TESTATTR);
209: assertEquals("Decorated attribute key not correctly appended",
210: TESTATTR, key.toString());
211: }
212:
213: /**
214: * Tests appending a null attribute key.
215: */
216: public void testAppendNullAttributeKey() {
217: key.appendAttribute(null);
218: assertEquals("Null attribute key not correctly appended", "",
219: key.toString());
220: }
221:
222: /**
223: * Tests appending an index to a key.
224: */
225: public void testAppendIndex() {
226: key.append("test").appendIndex(42);
227: assertEquals("Index was not correctly appended", "test(42)",
228: key.toString());
229: }
230:
231: /**
232: * Tests constructing a complex key by chaining multiple append operations.
233: */
234: public void testAppendComplexKey() {
235: key.append("tables").append("table.").appendIndex(0);
236: key.append("fields.").append("field").appendIndex(1);
237: key.appendAttribute("dataType");
238: assertEquals("Wrong complex key", TESTKEY, key.toString());
239: }
240:
241: /**
242: * Tests getting and setting the key's length.
243: */
244: public void testLength() {
245: key.append(TESTPROPS);
246: assertEquals("Wrong length", TESTPROPS.length(), key.length());
247: key.appendAttribute("dataType");
248: assertEquals("Wrong length", TESTKEY.length(), key.length());
249: key.setLength(TESTPROPS.length());
250: assertEquals("Wrong length after shortening", TESTPROPS
251: .length(), key.length());
252: assertEquals("Wrong resulting key", TESTPROPS, key.toString());
253: }
254:
255: /**
256: * Tests comparing configuration keys.
257: */
258: public void testEquals() {
259: DefaultConfigurationKey k1 = new DefaultConfigurationKey(
260: expressionEngine, TESTKEY);
261: DefaultConfigurationKey k2 = new DefaultConfigurationKey(
262: expressionEngine, TESTKEY);
263: assertTrue("Keys are not equal", k1.equals(k2));
264: assertTrue("Not reflexiv", k2.equals(k1));
265: assertEquals("Hash codes not equal", k1.hashCode(), k2
266: .hashCode());
267: k2.append("anotherPart");
268: assertFalse("Keys considered equal", k1.equals(k2));
269: assertFalse("Keys considered equal", k2.equals(k1));
270: assertFalse("Key equals null key", k1.equals(null));
271: assertTrue("Faild comparison with string", k1.equals(TESTKEY));
272: }
273:
274: /**
275: * Tests determining an attribute key's name.
276: */
277: public void testAttributeName() {
278: assertEquals("Plain key not detected", "test", key
279: .attributeName("test"));
280: assertEquals("Attribute markers not stripped", "dataType", key
281: .attributeName(TESTATTR));
282: assertNull("Null key not processed", key.attributeName(null));
283: }
284:
285: /**
286: * Tests to iterate over a simple key.
287: */
288: public void testIterate() {
289: key.append(TESTKEY);
290: DefaultConfigurationKey.KeyIterator it = key.iterator();
291: assertTrue("No key parts", it.hasNext());
292: assertEquals("Wrong key part", "tables", it.nextKey());
293: assertEquals("Wrong key part", "table", it.nextKey());
294: assertTrue("No index found", it.hasIndex());
295: assertEquals("Wrong index", 0, it.getIndex());
296: assertEquals("Wrong key part", "fields", it.nextKey());
297: assertFalse("Found an index", it.hasIndex());
298: assertEquals("Wrong key part", "field", it.nextKey(true));
299: assertEquals("Wrong index", 1, it.getIndex());
300: assertFalse("Found an attribute", it.isAttribute());
301: assertEquals("Wrong current key", "field", it.currentKey(true));
302: assertEquals("Wrong key part", "dataType", it.nextKey());
303: assertEquals("Wrong decorated key part", "[@dataType]", it
304: .currentKey(true));
305: assertTrue("Attribute not found", it.isAttribute());
306: assertFalse("Too many key parts", it.hasNext());
307: try {
308: it.next();
309: fail("Could iterate over the iteration's end!");
310: } catch (NoSuchElementException nex) {
311: // ok
312: }
313: }
314:
315: /**
316: * Tests an iteration where the remove() method is called. This is not
317: * supported.
318: */
319: public void testIterateWithRemove() {
320: assertFalse(key.iterator().hasNext());
321: key.append("simple");
322: DefaultConfigurationKey.KeyIterator it = key.iterator();
323: assertTrue(it.hasNext());
324: assertEquals("simple", it.next());
325: try {
326: it.remove();
327: fail("Could remove key component!");
328: } catch (UnsupportedOperationException uex) {
329: // ok
330: }
331: }
332:
333: /**
334: * Tests iterating over some funny keys.
335: */
336: public void testIterateStrangeKeys() {
337: key = new DefaultConfigurationKey(expressionEngine, "key.");
338: DefaultConfigurationKey.KeyIterator it = key.iterator();
339: assertTrue("Too few key parts", it.hasNext());
340: assertEquals("Wrong key part", "key", it.next());
341: assertFalse("Too many key parts", it.hasNext());
342:
343: key = new DefaultConfigurationKey(expressionEngine, ".");
344: it = key.iterator();
345: assertFalse("Simple delimiter key has more parts", it.hasNext());
346:
347: key = new DefaultConfigurationKey(expressionEngine,
348: "key().index()undefined(0).test");
349: it = key.iterator();
350: assertEquals("Wrong first part", "key()", it.next());
351: assertFalse("Index detected in first part", it.hasIndex());
352: assertEquals("Wrong second part", "index()undefined", it
353: .nextKey(false));
354: assertTrue("No index detected in second part", it.hasIndex());
355: assertEquals("Wrong index value", 0, it.getIndex());
356: }
357:
358: /**
359: * Tests iterating over keys with escaped delimiters.
360: */
361: public void testIterateEscapedDelimiters() {
362: key.append("my..elem");
363: key.append("trailing..dot..");
364: key.append(".strange");
365: assertEquals("my..elem.trailing..dot...strange", key.toString());
366: DefaultConfigurationKey.KeyIterator kit = key.iterator();
367: assertEquals("Wrong first part", "my.elem", kit.nextKey());
368: assertEquals("Wrong second part", "trailing.dot.", kit
369: .nextKey());
370: assertEquals("Wrong third part", "strange", kit.nextKey());
371: assertFalse("Too many parts", kit.hasNext());
372: }
373:
374: /**
375: * Tests iterating over keys when a different escaped delimiter is used.
376: */
377: public void testIterateAlternativeEscapeDelimiter() {
378: expressionEngine.setEscapedDelimiter("\\.");
379: key.append("\\.my\\.elem");
380: key.append("trailing\\.dot\\.");
381: key.append(".strange");
382: assertEquals("\\.my\\.elem.trailing\\.dot\\..strange", key
383: .toString());
384: DefaultConfigurationKey.KeyIterator kit = key.iterator();
385: assertEquals("Wrong first part", ".my.elem", kit.nextKey());
386: assertEquals("Wrong second part", "trailing.dot.", kit
387: .nextKey());
388: assertEquals("Wrong third part", "strange", kit.nextKey());
389: assertFalse("Too many parts", kit.hasNext());
390: }
391:
392: /**
393: * Tests iterating when no escape delimiter is defined.
394: */
395: public void testIterateWithoutEscapeDelimiter() {
396: expressionEngine.setEscapedDelimiter(null);
397: key.append("..my..elem.trailing..dot...strange");
398: assertEquals("Wrong key", "my..elem.trailing..dot...strange",
399: key.toString());
400: DefaultConfigurationKey.KeyIterator kit = key.iterator();
401: final String[] parts = { "my", "elem", "trailing", "dot",
402: "strange" };
403: for (int i = 0; i < parts.length; i++) {
404: assertEquals("Wrong key part " + i, parts[i], kit.next());
405: }
406: assertFalse("Too many parts", kit.hasNext());
407: }
408:
409: /**
410: * Tests iterating over an attribute key that has an index.
411: */
412: public void testAttributeKeyWithIndex() {
413: key.append(TESTATTR);
414: key.appendIndex(0);
415: assertEquals("Wrong attribute key with index",
416: TESTATTR + "(0)", key.toString());
417:
418: DefaultConfigurationKey.KeyIterator it = key.iterator();
419: assertTrue("No first element", it.hasNext());
420: it.next();
421: assertTrue("Index not found", it.hasIndex());
422: assertEquals("Incorrect index", 0, it.getIndex());
423: assertTrue("Attribute not found", it.isAttribute());
424: assertEquals("Wrong plain key", "dataType", it
425: .currentKey(false));
426: assertEquals("Wrong decorated key", TESTATTR, it
427: .currentKey(true));
428: }
429:
430: /**
431: * Tests iteration when the attribute markers equals the property delimiter.
432: */
433: public void testIterateAttributeEqualsPropertyDelimiter() {
434: expressionEngine.setAttributeEnd(null);
435: expressionEngine.setAttributeStart(expressionEngine
436: .getPropertyDelimiter());
437: key.append("this.isa.key");
438: DefaultConfigurationKey.KeyIterator kit = key.iterator();
439: assertEquals("Wrong first key part", "this", kit.next());
440: assertFalse("First part is an attribute", kit.isAttribute());
441: assertTrue("First part is not a property key", kit
442: .isPropertyKey());
443: assertEquals("Wrong second key part", "isa", kit.next());
444: assertFalse("Second part is an attribute", kit.isAttribute());
445: assertTrue("Second part is not a property key", kit
446: .isPropertyKey());
447: assertEquals("Wrong third key part", "key", kit.next());
448: assertTrue("Third part is not an attribute", kit.isAttribute());
449: assertTrue("Third part is not a property key", kit
450: .isPropertyKey());
451: assertEquals("Wrong decorated key part", "key", kit
452: .currentKey(true));
453: }
454: }
|