1   package org.apache.commons.configuration;
2   
3   /*
4    * Copyright 2002-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.NoSuchElementException;
20  
21  import junit.framework.TestCase;
22  
23  /***
24   * Test class for ConfigurationKey. 
25   * 
26   * @version $Id: TestConfigurationKey.java 233505 2005-08-19 16:16:31Z oheger $
27   */
28  public class TestConfigurationKey extends TestCase
29  {
30      private static final String TESTPROPS = "tables.table(0).fields.field(1)";
31      
32      private static final String TESTATTR = "[@dataType]";
33      
34      private static final String TESTKEY = TESTPROPS + TESTATTR;
35      
36      public void testAppend()
37      {
38          ConfigurationKey key = new ConfigurationKey();
39          key.append("tables").append("table.").appendIndex(0);
40          key.append("fields.").append("field").appendIndex(1);
41          key.appendAttribute("dataType");
42          assertEquals(TESTKEY, key.toString());
43      }
44      
45      public void testIterate()
46      {
47          ConfigurationKey key = new ConfigurationKey(TESTKEY);
48          ConfigurationKey.KeyIterator it = key.iterator();
49          assertTrue(it.hasNext());
50          assertEquals("tables", it.nextKey());
51          assertEquals("table", it.nextKey());
52          assertTrue(it.hasIndex());
53          assertEquals(0, it.getIndex());
54          assertEquals("fields", it.nextKey());
55          assertFalse(it.hasIndex());
56          assertEquals("field", it.nextKey(true));
57          assertEquals(1, it.getIndex());
58          assertFalse(it.isAttribute());
59          assertEquals("field", it.currentKey(true));
60          assertEquals("dataType", it.nextKey());
61          assertEquals("[@dataType]", it.currentKey(true));
62          assertTrue(it.isAttribute());
63          assertFalse(it.hasNext());
64          try
65          {
66              it.next();
67              fail("Could iterate over the iteration's end!");
68          }
69          catch(NoSuchElementException nex)
70          {
71              //ok
72          }
73          
74          key = new ConfigurationKey();
75          assertFalse(key.iterator().hasNext());
76          key.append("simple");
77          it = key.iterator();
78          assertTrue(it.hasNext());
79          assertEquals("simple", it.next());
80          try
81          {
82              it.remove();
83              fail("Could remove key component!");
84          }
85          catch(UnsupportedOperationException uex)
86          {
87              //ok
88          }
89      }
90      
91      public void testAttribute()
92      {
93          assertTrue(ConfigurationKey.isAttributeKey(TESTATTR));
94          assertFalse(ConfigurationKey.isAttributeKey(TESTPROPS));
95          assertFalse(ConfigurationKey.isAttributeKey(TESTKEY));
96          
97          ConfigurationKey key = new ConfigurationKey(TESTPROPS);
98          key.append(TESTATTR);
99          assertEquals(TESTKEY, key.toString());
100     }
101     
102     public void testLength()
103     {
104         ConfigurationKey key = new ConfigurationKey(TESTPROPS);
105         assertEquals(TESTPROPS.length(), key.length());
106         key.appendAttribute("dataType");
107         assertEquals(TESTKEY.length(), key.length());
108         key.setLength(TESTPROPS.length());
109         assertEquals(TESTPROPS.length(), key.length());
110         assertEquals(TESTPROPS, key.toString());
111     }
112     
113     public void testConstructAttributeKey()
114     {
115         assertEquals("[@attribute]", ConfigurationKey.constructAttributeKey("attribute"));
116         assertEquals("attribute", ConfigurationKey.attributeName("[@attribute]"));
117         assertEquals("attribute", ConfigurationKey.attributeName("attribute"));
118     }
119     
120     public void testEquals()
121     {
122         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
123         ConfigurationKey k2 = new ConfigurationKey(TESTKEY);
124         assertTrue(k1.equals(k2));
125         assertTrue(k2.equals(k1));
126         assertEquals(k1.hashCode(), k2.hashCode());
127         k2.append("anotherPart");
128         assertFalse(k1.equals(k2));
129         assertFalse(k2.equals(k1));
130         assertFalse(k1.equals(null));
131         assertTrue(k1.equals(TESTKEY));        
132     }
133     
134     public void testCommonKey()
135     {
136         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
137         ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
138         ConfigurationKey kc = k1.commonKey(k2);
139         assertEquals(new ConfigurationKey("tables.table(0)"), kc);
140         assertEquals(kc, k2.commonKey(k1));
141         
142         k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
143         kc = k1.commonKey(k2);
144         assertEquals(new ConfigurationKey("tables"), kc);
145         
146         k2 = new ConfigurationKey("completely.different.key");
147         kc = k1.commonKey(k2);
148         assertEquals(0, kc.length());
149         
150         k2 = new ConfigurationKey();
151         kc = k1.commonKey(k2);
152         assertEquals(0, kc.length());
153         
154         kc = k1.commonKey(k1);
155         assertEquals(kc, k1);
156         
157         try
158         {
159             kc.commonKey(null);
160             fail("Could construct common key with null key!");
161         }
162         catch(IllegalArgumentException iex)
163         {
164             //ok
165         }
166     }
167     
168     public void testDifferenceKey()
169     {
170         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
171         ConfigurationKey kd = k1.differenceKey(k1);
172         assertEquals(0, kd.length());
173         
174         ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
175         kd = k1.differenceKey(k2);
176         assertEquals("name", kd.toString());
177         
178         k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
179         kd = k1.differenceKey(k2);
180         assertEquals("table(1).fields.field(1)", kd.toString());
181         
182         k2 = new ConfigurationKey("completely.different.key");
183         kd = k1.differenceKey(k2);
184         assertEquals(k2, kd);
185     }
186     
187     public void testEscapedDelimiters()
188     {
189         ConfigurationKey k = new ConfigurationKey();
190         k.append("my..elem");
191         k.append("trailing..dot..");
192         k.append("strange");
193         assertEquals("my..elem.trailing..dot...strange", k.toString());
194         
195         ConfigurationKey.KeyIterator kit = k.iterator();
196         assertEquals("my.elem", kit.nextKey());
197         assertEquals("trailing.dot.", kit.nextKey());
198         assertEquals("strange", kit.nextKey());
199         assertFalse(kit.hasNext());
200     }
201     
202     /***
203      * Tests some funny keys.
204      */
205     public void testIterateStrangeKeys()
206     {
207         ConfigurationKey k = new ConfigurationKey("key.");
208         ConfigurationKey.KeyIterator it = k.iterator();
209         assertTrue(it.hasNext());
210         assertEquals("key", it.next());
211         assertFalse(it.hasNext());
212         
213         k = new ConfigurationKey(".");
214         it = k.iterator();
215         assertFalse(it.hasNext());
216         
217         k = new ConfigurationKey("key().index()undefined(0).test");
218         it = k.iterator();
219         assertEquals("key()", it.next());
220         assertFalse(it.hasIndex());
221         assertEquals("index()undefined", it.nextKey(false));
222         assertTrue(it.hasIndex());
223         assertEquals(0, it.getIndex());
224     }
225 }