001: package org.apache.commons.configuration;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.NoSuchElementException;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Test class for ConfigurationKey.
026: *
027: * @version $Id: TestConfigurationKey.java 439648 2006-09-02 20:42:10Z oheger $
028: */
029: public class TestConfigurationKey extends TestCase {
030: private static final String TESTPROPS = "tables.table(0).fields.field(1)";
031:
032: private static final String TESTATTR = "[@dataType]";
033:
034: private static final String TESTKEY = TESTPROPS + TESTATTR;
035:
036: public void testAppend() {
037: ConfigurationKey key = new ConfigurationKey();
038: key.append("tables").append("table.").appendIndex(0);
039: key.append("fields.").append("field").appendIndex(1);
040: key.appendAttribute("dataType");
041: assertEquals(TESTKEY, key.toString());
042: }
043:
044: public void testIterate() {
045: ConfigurationKey key = new ConfigurationKey(TESTKEY);
046: ConfigurationKey.KeyIterator it = key.iterator();
047: assertTrue(it.hasNext());
048: assertEquals("tables", it.nextKey());
049: assertEquals("table", it.nextKey());
050: assertTrue(it.hasIndex());
051: assertEquals(0, it.getIndex());
052: assertEquals("fields", it.nextKey());
053: assertFalse(it.hasIndex());
054: assertEquals("field", it.nextKey(true));
055: assertEquals(1, it.getIndex());
056: assertFalse(it.isAttribute());
057: assertEquals("field", it.currentKey(true));
058: assertEquals("dataType", it.nextKey());
059: assertEquals("[@dataType]", it.currentKey(true));
060: assertTrue(it.isAttribute());
061: assertFalse(it.hasNext());
062: try {
063: it.next();
064: fail("Could iterate over the iteration's end!");
065: } catch (NoSuchElementException nex) {
066: //ok
067: }
068:
069: key = new ConfigurationKey();
070: assertFalse(key.iterator().hasNext());
071: key.append("simple");
072: it = key.iterator();
073: assertTrue(it.hasNext());
074: assertEquals("simple", it.next());
075: try {
076: it.remove();
077: fail("Could remove key component!");
078: } catch (UnsupportedOperationException uex) {
079: //ok
080: }
081: }
082:
083: public void testAttribute() {
084: assertTrue(ConfigurationKey.isAttributeKey(TESTATTR));
085: assertFalse(ConfigurationKey.isAttributeKey(TESTPROPS));
086: assertFalse(ConfigurationKey.isAttributeKey(TESTKEY));
087:
088: ConfigurationKey key = new ConfigurationKey(TESTPROPS);
089: key.append(TESTATTR);
090: assertEquals(TESTKEY, key.toString());
091: }
092:
093: public void testLength() {
094: ConfigurationKey key = new ConfigurationKey(TESTPROPS);
095: assertEquals(TESTPROPS.length(), key.length());
096: key.appendAttribute("dataType");
097: assertEquals(TESTKEY.length(), key.length());
098: key.setLength(TESTPROPS.length());
099: assertEquals(TESTPROPS.length(), key.length());
100: assertEquals(TESTPROPS, key.toString());
101: }
102:
103: public void testConstructAttributeKey() {
104: assertEquals("[@attribute]", ConfigurationKey
105: .constructAttributeKey("attribute"));
106: assertEquals("attribute", ConfigurationKey
107: .attributeName("[@attribute]"));
108: assertEquals("attribute", ConfigurationKey
109: .attributeName("attribute"));
110: }
111:
112: public void testEquals() {
113: ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
114: ConfigurationKey k2 = new ConfigurationKey(TESTKEY);
115: assertTrue(k1.equals(k2));
116: assertTrue(k2.equals(k1));
117: assertEquals(k1.hashCode(), k2.hashCode());
118: k2.append("anotherPart");
119: assertFalse(k1.equals(k2));
120: assertFalse(k2.equals(k1));
121: assertFalse(k1.equals(null));
122: assertTrue(k1.equals(TESTKEY));
123: }
124:
125: public void testCommonKey() {
126: ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
127: ConfigurationKey k2 = new ConfigurationKey(
128: "tables.table(0).name");
129: ConfigurationKey kc = k1.commonKey(k2);
130: assertEquals(new ConfigurationKey("tables.table(0)"), kc);
131: assertEquals(kc, k2.commonKey(k1));
132:
133: k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
134: kc = k1.commonKey(k2);
135: assertEquals(new ConfigurationKey("tables"), kc);
136:
137: k2 = new ConfigurationKey("completely.different.key");
138: kc = k1.commonKey(k2);
139: assertEquals(0, kc.length());
140:
141: k2 = new ConfigurationKey();
142: kc = k1.commonKey(k2);
143: assertEquals(0, kc.length());
144:
145: kc = k1.commonKey(k1);
146: assertEquals(kc, k1);
147:
148: try {
149: kc.commonKey(null);
150: fail("Could construct common key with null key!");
151: } catch (IllegalArgumentException iex) {
152: //ok
153: }
154: }
155:
156: public void testDifferenceKey() {
157: ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
158: ConfigurationKey kd = k1.differenceKey(k1);
159: assertEquals(0, kd.length());
160:
161: ConfigurationKey k2 = new ConfigurationKey(
162: "tables.table(0).name");
163: kd = k1.differenceKey(k2);
164: assertEquals("name", kd.toString());
165:
166: k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
167: kd = k1.differenceKey(k2);
168: assertEquals("table(1).fields.field(1)", kd.toString());
169:
170: k2 = new ConfigurationKey("completely.different.key");
171: kd = k1.differenceKey(k2);
172: assertEquals(k2, kd);
173: }
174:
175: public void testEscapedDelimiters() {
176: ConfigurationKey k = new ConfigurationKey();
177: k.append("my..elem");
178: k.append("trailing..dot..");
179: k.append("strange");
180: assertEquals("my..elem.trailing..dot...strange", k.toString());
181:
182: ConfigurationKey.KeyIterator kit = k.iterator();
183: assertEquals("my.elem", kit.nextKey());
184: assertEquals("trailing.dot.", kit.nextKey());
185: assertEquals("strange", kit.nextKey());
186: assertFalse(kit.hasNext());
187: }
188:
189: /**
190: * Tests some funny keys.
191: */
192: public void testIterateStrangeKeys() {
193: ConfigurationKey k = new ConfigurationKey("key.");
194: ConfigurationKey.KeyIterator it = k.iterator();
195: assertTrue(it.hasNext());
196: assertEquals("key", it.next());
197: assertFalse(it.hasNext());
198:
199: k = new ConfigurationKey(".");
200: it = k.iterator();
201: assertFalse(it.hasNext());
202:
203: k = new ConfigurationKey("key().index()undefined(0).test");
204: it = k.iterator();
205: assertEquals("key()", it.next());
206: assertFalse(it.hasIndex());
207: assertEquals("index()undefined", it.nextKey(false));
208: assertTrue(it.hasIndex());
209: assertEquals(0, it.getIndex());
210: }
211:
212: /**
213: * Tests iterating over an attribute key that has an index.
214: */
215: public void testAttributeKeyWithIndex() {
216: ConfigurationKey k = new ConfigurationKey(TESTATTR);
217: k.appendIndex(0);
218: assertEquals("Wrong attribute key with index",
219: TESTATTR + "(0)", k.toString());
220:
221: ConfigurationKey.KeyIterator it = k.iterator();
222: assertTrue("No first element", it.hasNext());
223: it.next();
224: assertTrue("Index not found", it.hasIndex());
225: assertEquals("Incorrect index", 0, it.getIndex());
226: assertTrue("Attribute not found", it.isAttribute());
227: assertEquals("Wrong plain key", "dataType", it
228: .currentKey(false));
229: assertEquals("Wrong decorated key", TESTATTR, it
230: .currentKey(true));
231: }
232: }
|