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