1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.tree;
18
19 import java.util.List;
20
21 import org.apache.commons.configuration.ConfigurationException;
22 import org.apache.commons.configuration.HierarchicalConfiguration;
23
24 /***
25 * Test class for OverrideCombiner.
26 *
27 * @version $Id: TestOverrideCombiner.java 439648 2006-09-02 20:42:10Z oheger $
28 */
29 public class TestOverrideCombiner extends AbstractCombinerTest
30 {
31 /***
32 * Creates the combiner.
33 *
34 * @return the combiner
35 */
36 protected NodeCombiner createCombiner()
37 {
38 return new OverrideCombiner();
39 }
40
41 /***
42 * Tests combination of simple elements.
43 */
44 public void testSimpleValues() throws ConfigurationException
45 {
46 HierarchicalConfiguration config = createCombinedConfiguration();
47 assertEquals("Wrong number of bgcolors", 0, config
48 .getMaxIndex("gui.bgcolor"));
49 assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor"));
50 assertEquals("Wrong selcolor", "yellow", config
51 .getString("gui.selcolor"));
52 assertEquals("Wrong fgcolor", "blue", config.getString("gui.fgcolor"));
53 assertEquals("Wrong level", 1, config.getInt("gui.level"));
54 }
55
56 /***
57 * Tests combination of attributes.
58 */
59 public void testAttributes() throws ConfigurationException
60 {
61 HierarchicalConfiguration config = createCombinedConfiguration();
62 assertEquals("Wrong value of min attribute", 1, config
63 .getInt("gui.level[@min]"));
64 assertEquals("Wrong value of default attribute", 2, config
65 .getInt("gui.level[@default]"));
66 assertEquals("Wrong number of id attributes", 0, config
67 .getMaxIndex("database.tables.table(0)[@id]"));
68 assertEquals("Wrong value of table id", 1, config
69 .getInt("database.tables.table(0)[@id]"));
70 }
71
72 /***
73 * Tests whether property values are correctly overridden.
74 */
75 public void testOverrideValues() throws ConfigurationException
76 {
77 HierarchicalConfiguration config = createCombinedConfiguration();
78 assertEquals("Wrong user", "Admin", config
79 .getString("base.services.security.login.user"));
80 assertEquals("Wrong user type", "default", config
81 .getString("base.services.security.login.user[@type]"));
82 assertEquals("Wrong password", "BeamMeUp", config
83 .getString("base.services.security.login.passwd"));
84 assertEquals("Wrong password type", "secret", config
85 .getString("base.services.security.login.passwd[@type]"));
86 }
87
88 /***
89 * Tests if a list from the first node structure overrides a list in the
90 * second structure.
91 */
92 public void testListFromFirstStructure() throws ConfigurationException
93 {
94 HierarchicalConfiguration config = createCombinedConfiguration();
95 assertEquals("Wrong number of services", 0, config
96 .getMaxIndex("net.service.url"));
97 assertEquals("Wrong service", "http://service1.org", config
98 .getString("net.service.url"));
99 assertFalse("Type attribute available", config
100 .containsKey("net.service.url[@type]"));
101 }
102
103 /***
104 * Tests if a list from the second structure is added if it is not defined
105 * in the first structure.
106 */
107 public void testListFromSecondStructure() throws ConfigurationException
108 {
109 HierarchicalConfiguration config = createCombinedConfiguration();
110 assertEquals("Wrong number of servers", 3, config
111 .getMaxIndex("net.server.url"));
112 assertEquals("Wrong server", "http://testsvr.com", config
113 .getString("net.server.url(2)"));
114 }
115
116 /***
117 * Tests the combination of the table structure. Because the table node is
118 * not declared as a list node the structures will be combined. But this
119 * won't make any difference because the values in the first table override
120 * the values in the second table. Only the node for the table element will
121 * be a ViewNode.
122 */
123 public void testCombinedTableNoList() throws ConfigurationException
124 {
125 ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
126 assertTrue("Node is not a view node", tabNode instanceof ViewNode);
127 }
128
129 /***
130 * Tests the combination of the table structure when the table node is
131 * declared as a list node. In this case the first table structure
132 * completely overrides the second and will be directly added to the
133 * resulting structure.
134 */
135 public void testCombinedTableList() throws ConfigurationException
136 {
137 combiner.addListNode("table");
138 ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
139 assertFalse("Node is a view node", tabNode instanceof ViewNode);
140 }
141
142 /***
143 * Helper method for checking the combined table structure.
144 *
145 * @param config the config
146 * @return the node for the table element
147 */
148 private ConfigurationNode checkTable(HierarchicalConfiguration config)
149 {
150 assertEquals("Wrong number of tables", 0, config
151 .getMaxIndex("database.tables.table"));
152 HierarchicalConfiguration c = config
153 .configurationAt("database.tables.table");
154 assertEquals("Wrong table name", "documents", c.getString("name"));
155 assertEquals("Wrong number of fields", 2, c
156 .getMaxIndex("fields.field.name"));
157 assertEquals("Wrong field", "docname", c
158 .getString("fields.field(1).name"));
159
160 List nds = config.getExpressionEngine().query(config.getRoot(),
161 "database.tables.table");
162 assertFalse("No node found", nds.isEmpty());
163 return (ConfigurationNode) nds.get(0);
164 }
165 }