KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestConfigurationKey


1 package org.apache.commons.configuration;
2
3 /*
4  * Copyright 2002-2004 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 junit.framework.TestCase;
20
21 /**
22  * Test class for ConfigurationKey.
23  *
24  * @version $Id: TestConfigurationKey.java 155408 2005-02-26 12:56:39Z dirkv $
25  */

26 public class TestConfigurationKey extends TestCase
27 {
28     private static final String JavaDoc TESTPROPS = "tables.table(0).fields.field(1)";
29     
30     private static final String JavaDoc TESTATTR = "[@dataType]";
31     
32     private static final String JavaDoc TESTKEY = TESTPROPS + TESTATTR;
33     
34     public void testAppend()
35     {
36         ConfigurationKey key = new ConfigurationKey();
37         key.append("tables").append("table.").appendIndex(0);
38         key.append("fields.").append("field").appendIndex(1);
39         key.appendAttribute("dataType");
40         assertEquals(TESTKEY, key.toString());
41     }
42     
43     public void testIterate()
44     {
45         ConfigurationKey key = new ConfigurationKey(TESTKEY);
46         ConfigurationKey.KeyIterator it = key.iterator();
47         assertTrue(it.hasNext());
48         assertEquals("tables", it.nextKey());
49         assertEquals("table", it.nextKey());
50         assertTrue(it.hasIndex());
51         assertEquals(0, it.getIndex());
52         assertEquals("fields", it.nextKey());
53         assertFalse(it.hasIndex());
54         assertEquals("field", it.nextKey(true));
55         assertEquals(1, it.getIndex());
56         assertFalse(it.isAttribute());
57         assertEquals("field", it.currentKey(true));
58         assertEquals("dataType", it.nextKey());
59         assertEquals("[@dataType]", it.currentKey(true));
60         assertTrue(it.isAttribute());
61         assertFalse(it.hasNext());
62         
63         key = new ConfigurationKey();
64         assertFalse(key.iterator().hasNext());
65         key.append("simple");
66         it = key.iterator();
67         assertTrue(it.hasNext());
68         assertEquals("simple", it.next());
69     }
70     
71     public void testAttribute()
72     {
73         assertTrue(ConfigurationKey.isAttributeKey(TESTATTR));
74         assertFalse(ConfigurationKey.isAttributeKey(TESTPROPS));
75         assertFalse(ConfigurationKey.isAttributeKey(TESTKEY));
76         
77         ConfigurationKey key = new ConfigurationKey(TESTPROPS);
78         key.append(TESTATTR);
79         assertEquals(TESTKEY, key.toString());
80     }
81     
82     public void testLength()
83     {
84         ConfigurationKey key = new ConfigurationKey(TESTPROPS);
85         assertEquals(TESTPROPS.length(), key.length());
86         key.appendAttribute("dataType");
87         assertEquals(TESTKEY.length(), key.length());
88         key.setLength(TESTPROPS.length());
89         assertEquals(TESTPROPS.length(), key.length());
90         assertEquals(TESTPROPS, key.toString());
91     }
92     
93     public void testConstructAttributeKey()
94     {
95         assertEquals("[@attribute]", ConfigurationKey.constructAttributeKey("attribute"));
96         assertEquals("attribute", ConfigurationKey.attributeName("[@attribute]"));
97         assertEquals("attribute", ConfigurationKey.attributeName("attribute"));
98     }
99     
100     public void testEquals()
101     {
102         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
103         ConfigurationKey k2 = new ConfigurationKey(TESTKEY);
104         assertTrue(k1.equals(k2));
105         assertTrue(k2.equals(k1));
106         k2.append("anotherPart");
107         assertFalse(k1.equals(k2));
108         assertFalse(k2.equals(k1));
109         assertFalse(k1.equals(null));
110         assertTrue(k1.equals(TESTKEY));
111     }
112     
113     public void testCommonKey()
114     {
115         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
116         ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
117         ConfigurationKey kc = k1.commonKey(k2);
118         assertEquals(new ConfigurationKey("tables.table(0)"), kc);
119         assertEquals(kc, k2.commonKey(k1));
120         
121         k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
122         kc = k1.commonKey(k2);
123         assertEquals(new ConfigurationKey("tables"), kc);
124         
125         k2 = new ConfigurationKey("completely.different.key");
126         kc = k1.commonKey(k2);
127         assertEquals(0, kc.length());
128         
129         k2 = new ConfigurationKey();
130         kc = k1.commonKey(k2);
131         assertEquals(0, kc.length());
132         
133         kc = k1.commonKey(k1);
134         assertEquals(kc, k1);
135     }
136     
137     public void testDifferenceKey()
138     {
139         ConfigurationKey k1 = new ConfigurationKey(TESTKEY);
140         ConfigurationKey kd = k1.differenceKey(k1);
141         assertEquals(0, kd.length());
142         
143         ConfigurationKey k2 = new ConfigurationKey("tables.table(0).name");
144         kd = k1.differenceKey(k2);
145         assertEquals("name", kd.toString());
146         
147         k2 = new ConfigurationKey("tables.table(1).fields.field(1)");
148         kd = k1.differenceKey(k2);
149         assertEquals("table(1).fields.field(1)", kd.toString());
150         
151         k2 = new ConfigurationKey("completely.different.key");
152         kd = k1.differenceKey(k2);
153         assertEquals(k2, kd);
154     }
155 }
156
Popular Tags