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 org.apache.commons.configuration.ConfigurationException;
20 import org.apache.commons.configuration.HierarchicalConfiguration;
21
22 /***
23 * Test class for UnionCombiner.
24 *
25 * @version $Id: TestUnionCombiner.java 439648 2006-09-02 20:42:10Z oheger $
26 */
27 public class TestUnionCombiner extends AbstractCombinerTest
28 {
29 /***
30 * Creates the combiner.
31 *
32 * @return the combiner
33 */
34 protected NodeCombiner createCombiner()
35 {
36 return new UnionCombiner();
37 }
38
39 /***
40 * Tests combination of simple values (no lists).
41 */
42 public void testSimpleValues() throws ConfigurationException
43 {
44 HierarchicalConfiguration config = createCombinedConfiguration();
45 assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
46 assertEquals("Wrong first color", "green", config
47 .getString("gui.bgcolor(0)"));
48 assertEquals("Wrong second color", "black", config
49 .getString("gui.bgcolor(1)"));
50 assertEquals("Wrong number of selcolors", 0, config
51 .getMaxIndex("gui.selcolor"));
52 assertEquals("Wrong selcolor", "yellow", config
53 .getString("gui.selcolor"));
54 }
55
56 /***
57 * Tests combinations of elements with attributes.
58 */
59 public void testSimpleValuesWithAttributes() throws ConfigurationException
60 {
61 HierarchicalConfiguration config = createCombinedConfiguration();
62 assertEquals("Too few level elements", 1, config
63 .getMaxIndex("gui.level"));
64 assertEquals("Wrong value of first element", 1, config
65 .getInt("gui.level(0)"));
66 assertEquals("Wrong value of second element", 4, config
67 .getInt("gui.level(1)"));
68 assertEquals("Wrong value of first attribute", 2, config
69 .getInt("gui.level(0)[@default]"));
70 assertFalse("Found wrong attribute", config
71 .containsKey("gui.level(0)[@min]"));
72 assertEquals("Wrong value of second attribute", 1, config
73 .getInt("gui.level(1)[@min]"));
74 }
75
76 /***
77 * Tests combination of attributes.
78 */
79 public void testAttributes() throws ConfigurationException
80 {
81 HierarchicalConfiguration config = createCombinedConfiguration();
82 assertEquals("Too few attributes", 1, config
83 .getMaxIndex("database.tables.table(0)[@id]"));
84 assertEquals("Wrong value of first attribute", 1, config
85 .getInt("database.tables.table(0)[@id](0)"));
86 assertEquals("Wrong value of second attribute", 2, config
87 .getInt("database.tables.table(0)[@id](1)"));
88 }
89
90 /***
91 * Tests combination of lists.
92 */
93 public void testLists() throws ConfigurationException
94 {
95 HierarchicalConfiguration config = createCombinedConfiguration();
96 assertEquals("Too few list elements", 2, config
97 .getMaxIndex("net.service.url"));
98 assertEquals("Wrong first service", "http://service1.org", config
99 .getString("net.service.url(0)"));
100 assertEquals("Wrong second service", "http://service2.org", config
101 .getString("net.service.url(1)"));
102 assertEquals("Wrong service attribute", 2, config
103 .getInt("net.service.url(2)[@type]"));
104 assertEquals("Wrong number of server elements", 3, config
105 .getMaxIndex("net.server.url"));
106 }
107
108 /***
109 * Tests combining a list of tables. Per default the table elements will be
110 * combined. But if they are defined as list elements, the resulting tree
111 * should contain two table nodes.
112 */
113 public void testTableList() throws ConfigurationException
114 {
115 combiner.addListNode("table");
116 HierarchicalConfiguration config = createCombinedConfiguration();
117 assertEquals("Wrong name of first table", "documents", config
118 .getString("database.tables.table(0).name"));
119 assertEquals("Wrong id of first table", 1, config
120 .getInt("database.tables.table(0)[@id]"));
121 assertEquals("Wrong name of second table", "tasks", config
122 .getString("database.tables.table(1).name"));
123 assertEquals("Wrong id of second table", 2, config
124 .getInt("database.tables.table(1)[@id]"));
125 }
126 }