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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21
22 import org.apache.commons.configuration.ConfigurationException;
23 import org.apache.commons.configuration.HierarchicalConfiguration;
24 import org.junit.Test;
25
26
27
28
29
30
31 public class TestUnionCombiner extends AbstractCombinerTest
32 {
33
34
35
36
37
38 @Override
39 protected NodeCombiner createCombiner()
40 {
41 return new UnionCombiner();
42 }
43
44
45
46
47 @Test
48 public void testSimpleValues() throws ConfigurationException
49 {
50 HierarchicalConfiguration config = createCombinedConfiguration();
51 assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
52 assertEquals("Wrong first color", "green", config
53 .getString("gui.bgcolor(0)"));
54 assertEquals("Wrong second color", "black", config
55 .getString("gui.bgcolor(1)"));
56 assertEquals("Wrong number of selcolors", 0, config
57 .getMaxIndex("gui.selcolor"));
58 assertEquals("Wrong selcolor", "yellow", config
59 .getString("gui.selcolor"));
60 }
61
62
63
64
65 @Test
66 public void testSimpleValuesWithAttributes() throws ConfigurationException
67 {
68 HierarchicalConfiguration config = createCombinedConfiguration();
69 assertEquals("Too few level elements", 1, config
70 .getMaxIndex("gui.level"));
71 assertEquals("Wrong value of first element", 1, config
72 .getInt("gui.level(0)"));
73 assertEquals("Wrong value of second element", 4, config
74 .getInt("gui.level(1)"));
75 assertEquals("Wrong value of first attribute", 2, config
76 .getInt("gui.level(0)[@default]"));
77 assertFalse("Found wrong attribute", config
78 .containsKey("gui.level(0)[@min]"));
79 assertEquals("Wrong value of second attribute", 1, config
80 .getInt("gui.level(1)[@min]"));
81 }
82
83
84
85
86 @Test
87 public void testAttributes() throws ConfigurationException
88 {
89 HierarchicalConfiguration config = createCombinedConfiguration();
90 assertEquals("Too few attributes", 1, config
91 .getMaxIndex("database.tables.table(0)[@id]"));
92 assertEquals("Wrong value of first attribute", 1, config
93 .getInt("database.tables.table(0)[@id](0)"));
94 assertEquals("Wrong value of second attribute", 2, config
95 .getInt("database.tables.table(0)[@id](1)"));
96 }
97
98
99
100
101 @Test
102 public void testLists() throws ConfigurationException
103 {
104 HierarchicalConfiguration config = createCombinedConfiguration();
105 assertEquals("Too few list elements", 2, config
106 .getMaxIndex("net.service.url"));
107 assertEquals("Wrong first service", "http://service1.org", config
108 .getString("net.service.url(0)"));
109 assertEquals("Wrong second service", "http://service2.org", config
110 .getString("net.service.url(1)"));
111 assertEquals("Wrong service attribute", 2, config
112 .getInt("net.service.url(2)[@type]"));
113 assertEquals("Wrong number of server elements", 3, config
114 .getMaxIndex("net.server.url"));
115 }
116
117
118
119
120
121
122 @Test
123 public void testTableList() throws ConfigurationException
124 {
125 combiner.addListNode("table");
126 HierarchicalConfiguration config = createCombinedConfiguration();
127 assertEquals("Wrong name of first table", "documents", config
128 .getString("database.tables.table(0).name"));
129 assertEquals("Wrong id of first table", 1, config
130 .getInt("database.tables.table(0)[@id]"));
131 assertEquals("Wrong name of second table", "tasks", config
132 .getString("database.tables.table(1).name"));
133 assertEquals("Wrong id of second table", 2, config
134 .getInt("database.tables.table(1)[@id]"));
135 }
136 }