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 static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertFalse;
022    import static org.junit.Assert.assertTrue;
023    import static org.junit.Assert.fail;
024    
025    import java.util.NoSuchElementException;
026    
027    import org.junit.Test;
028    
029    /**
030     * Test class for ConfigurationKey.
031     *
032     * @version $Id: TestConfigurationKey.java 1231749 2012-01-15 20:48:56Z oheger $
033     */
034    @SuppressWarnings("deprecation")
035    public class TestConfigurationKey
036    {
037        private static final String TESTPROPS = "tables.table(0).fields.field(1)";
038    
039        private static final String TESTATTR = "[@dataType]";
040    
041        private static final String TESTKEY = TESTPROPS + TESTATTR;
042    
043        @Test
044        public void testAppend()
045        {
046            ConfigurationKey key = new ConfigurationKey();
047            key.append("tables").append("table.").appendIndex(0);
048            key.append("fields.").append("field").appendIndex(1);
049            key.appendAttribute("dataType");
050            assertEquals(TESTKEY, key.toString());
051        }
052    
053        @Test
054        public void testIterate()
055        {
056            ConfigurationKey key = new ConfigurationKey(TESTKEY);
057            ConfigurationKey.KeyIterator it = key.iterator();
058            assertTrue(it.hasNext());
059            assertEquals("tables", it.nextKey());
060            assertEquals("table", it.nextKey());
061            assertTrue(it.hasIndex());
062            assertEquals(0, it.getIndex());
063            assertEquals("fields", it.nextKey());
064            assertFalse(it.hasIndex());
065            assertEquals("field", it.nextKey(true));
066            assertEquals(1, it.getIndex());
067            assertFalse(it.isAttribute());
068            assertEquals("field", it.currentKey(true));
069            assertEquals("dataType", it.nextKey());
070            assertEquals("[@dataType]", it.currentKey(true));
071            assertTrue(it.isAttribute());
072            assertFalse(it.hasNext());
073            try
074            {
075                it.next();
076                fail("Could iterate over the iteration's end!");
077            }
078            catch(NoSuchElementException nex)
079            {
080                //ok
081            }
082    
083            key = new ConfigurationKey();
084            assertFalse(key.iterator().hasNext());
085            key.append("simple");
086            it = key.iterator();
087            assertTrue(it.hasNext());
088            assertEquals("simple", it.next());
089            try
090            {
091                it.remove();
092                fail("Could remove key component!");
093            }
094            catch(UnsupportedOperationException uex)
095            {
096                //ok
097            }
098        }
099    
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                //ok
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         * Tests some funny keys.
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         * Tests iterating over an attribute key that has an index.
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    }