1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import junit.framework.TestCase;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33 @Category(SmallTests.class)
34 public class TestCompoundConfiguration extends TestCase {
35 private Configuration baseConf;
36 private int baseConfSize;
37
38 @Override
39 protected void setUp() throws Exception {
40 baseConf = new Configuration();
41 baseConf.set("A", "1");
42 baseConf.setInt("B", 2);
43 baseConf.set("C", "3");
44 baseConfSize = baseConf.size();
45 }
46
47 @Test
48 public void testBasicFunctionality() throws ClassNotFoundException {
49 CompoundConfiguration compoundConf = new CompoundConfiguration()
50 .add(baseConf);
51 assertEquals("1", compoundConf.get("A"));
52 assertEquals(2, compoundConf.getInt("B", 0));
53 assertEquals(3, compoundConf.getInt("C", 0));
54 assertEquals(0, compoundConf.getInt("D", 0));
55
56 assertEquals(CompoundConfiguration.class, compoundConf
57 .getClassByName(CompoundConfiguration.class.getName()));
58 try {
59 compoundConf.getClassByName("bad_class_name");
60 fail("Trying to load bad_class_name should throw an exception");
61 } catch (ClassNotFoundException e) {
62
63 }
64 }
65
66 @Test
67 public void testPut() {
68 CompoundConfiguration compoundConf = new CompoundConfiguration()
69 .add(baseConf);
70 assertEquals("1", compoundConf.get("A"));
71 assertEquals(2, compoundConf.getInt("B", 0));
72 assertEquals(3, compoundConf.getInt("C", 0));
73 assertEquals(0, compoundConf.getInt("D", 0));
74
75 compoundConf.set("A", "1337");
76 compoundConf.set("string", "stringvalue");
77 assertEquals(1337, compoundConf.getInt("A", 0));
78 assertEquals("stringvalue", compoundConf.get("string"));
79
80
81 assertEquals("1", baseConf.get("A"));
82 assertNull(baseConf.get("string"));
83
84
85 baseConf.set("setInParent", "fromParent");
86 assertEquals("fromParent", compoundConf.get("setInParent"));
87 }
88
89 @Test
90 public void testWithConfig() {
91 Configuration conf = new Configuration();
92 conf.set("B", "2b");
93 conf.set("C", "33");
94 conf.set("D", "4");
95
96 CompoundConfiguration compoundConf = new CompoundConfiguration()
97 .add(baseConf)
98 .add(conf);
99 assertEquals("1", compoundConf.get("A"));
100 assertEquals("2b", compoundConf.get("B"));
101 assertEquals(33, compoundConf.getInt("C", 0));
102 assertEquals("4", compoundConf.get("D"));
103 assertEquals(4, compoundConf.getInt("D", 0));
104 assertNull(compoundConf.get("E"));
105 assertEquals(6, compoundConf.getInt("F", 6));
106
107 int cnt = 0;
108 for (Map.Entry<String,String> entry : compoundConf) {
109 cnt++;
110 if (entry.getKey().equals("B")) assertEquals("2b", entry.getValue());
111 else if (entry.getKey().equals("G")) assertEquals(null, entry.getValue());
112 }
113
114 assertEquals(baseConfSize + 1, cnt);
115 }
116
117 private ImmutableBytesWritable strToIbw(String s) {
118 return new ImmutableBytesWritable(Bytes.toBytes(s));
119 }
120
121 @Test
122 public void testWithIbwMap() {
123 Map<ImmutableBytesWritable, ImmutableBytesWritable> map =
124 new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
125 map.put(strToIbw("B"), strToIbw("2b"));
126 map.put(strToIbw("C"), strToIbw("33"));
127 map.put(strToIbw("D"), strToIbw("4"));
128
129 map.put(strToIbw("G"), null);
130
131 CompoundConfiguration compoundConf = new CompoundConfiguration()
132 .add(baseConf)
133 .addWritableMap(map);
134 assertEquals("1", compoundConf.get("A"));
135 assertEquals("2b", compoundConf.get("B"));
136 assertEquals(33, compoundConf.getInt("C", 0));
137 assertEquals("4", compoundConf.get("D"));
138 assertEquals(4, compoundConf.getInt("D", 0));
139 assertNull(compoundConf.get("E"));
140 assertEquals(6, compoundConf.getInt("F", 6));
141 assertNull(compoundConf.get("G"));
142
143 int cnt = 0;
144 for (Map.Entry<String,String> entry : compoundConf) {
145 cnt++;
146 if (entry.getKey().equals("B")) assertEquals("2b", entry.getValue());
147 else if (entry.getKey().equals("G")) assertEquals(null, entry.getValue());
148 }
149
150 assertEquals(baseConfSize + 2, cnt);
151
152
153 CompoundConfiguration conf2 = new CompoundConfiguration();
154 conf2.set("X", "modification");
155 conf2.set("D", "not4");
156 assertEquals("modification", conf2.get("X"));
157 assertEquals("not4", conf2.get("D"));
158 conf2.addWritableMap(map);
159 assertEquals("4", conf2.get("D"));
160 }
161
162 @Test
163 public void testWithStringMap() {
164 Map<String, String> map = new HashMap<String, String>();
165 map.put("B", "2b");
166 map.put("C", "33");
167 map.put("D", "4");
168
169 map.put("G", null);
170
171 CompoundConfiguration compoundConf = new CompoundConfiguration().addStringMap(map);
172 assertEquals("2b", compoundConf.get("B"));
173 assertEquals(33, compoundConf.getInt("C", 0));
174 assertEquals("4", compoundConf.get("D"));
175 assertEquals(4, compoundConf.getInt("D", 0));
176 assertNull(compoundConf.get("E"));
177 assertEquals(6, compoundConf.getInt("F", 6));
178 assertNull(compoundConf.get("G"));
179
180 int cnt = 0;
181 for (Map.Entry<String,String> entry : compoundConf) {
182 cnt++;
183 if (entry.getKey().equals("B")) assertEquals("2b", entry.getValue());
184 else if (entry.getKey().equals("G")) assertEquals(null, entry.getValue());
185 }
186
187 assertEquals(4, cnt);
188
189
190 CompoundConfiguration conf2 = new CompoundConfiguration();
191 conf2.set("X", "modification");
192 conf2.set("D", "not4");
193 assertEquals("modification", conf2.get("X"));
194 assertEquals("not4", conf2.get("D"));
195 conf2.addStringMap(map);
196 assertEquals("4", conf2.get("D"));
197 }
198
199 @Test
200 public void testLaterConfigsOverrideEarlier() {
201 Map<String, String> map1 = new HashMap<String, String>();
202 map1.put("A", "2");
203 map1.put("D", "5");
204 Map<String, String> map2 = new HashMap<String, String>();
205 String newValueForA = "3", newValueForB = "4";
206 map2.put("A", newValueForA);
207 map2.put("B", newValueForB);
208
209 CompoundConfiguration compoundConf = new CompoundConfiguration()
210 .addStringMap(map1).add(baseConf);
211 assertEquals("1", compoundConf.get("A"));
212 assertEquals("5", compoundConf.get("D"));
213 compoundConf.addStringMap(map2);
214 assertEquals(newValueForA, compoundConf.get("A"));
215 assertEquals(newValueForB, compoundConf.get("B"));
216 assertEquals("5", compoundConf.get("D"));
217
218 int cnt = 0;
219 for (Map.Entry<String,String> entry : compoundConf) {
220 cnt++;
221 if (entry.getKey().equals("A")) assertEquals(newValueForA, entry.getValue());
222 else if (entry.getKey().equals("B")) assertEquals(newValueForB, entry.getValue());
223 }
224
225 assertEquals(baseConfSize + 1, cnt);
226 }
227 }