1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
27  import org.apache.hadoop.hbase.regionserver.CompoundConfiguration;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.SmallTests;
30  
31  import org.junit.experimental.categories.Category;
32  import org.junit.Test;
33  
34  import junit.framework.TestCase;
35  
36  @Category(SmallTests.class)
37  public class TestCompoundConfiguration extends TestCase {
38    private Configuration baseConf;
39    private int baseConfSize;
40  
41    @Override
42    protected void setUp() throws Exception {
43      baseConf = new Configuration();
44      baseConf.set("A", "1");
45      baseConf.setInt("B", 2);
46      baseConf.set("C", "3");
47      baseConfSize = baseConf.size();
48    }
49  
50    @Test
51    public void testBasicFunctionality() throws ClassNotFoundException {
52      CompoundConfiguration compoundConf = new CompoundConfiguration()
53          .add(baseConf);
54      assertEquals("1", compoundConf.get("A"));
55      assertEquals(2, compoundConf.getInt("B", 0));
56      assertEquals(3, compoundConf.getInt("C", 0));
57      assertEquals(0, compoundConf.getInt("D", 0));
58  
59      assertEquals(CompoundConfiguration.class, compoundConf
60          .getClassByName(CompoundConfiguration.class.getName()));
61      try {
62        compoundConf.getClassByName("bad_class_name");
63        fail("Trying to load bad_class_name should throw an exception");
64      } catch (ClassNotFoundException e) {
65        // win!
66      }
67    }
68  
69    @Test
70    public void testWithConfig() {
71      Configuration conf = new Configuration();
72      conf.set("B", "2b");
73      conf.set("C", "33");
74      conf.set("D", "4");
75  
76      CompoundConfiguration compoundConf = new CompoundConfiguration()
77          .add(baseConf)
78          .add(conf);
79      assertEquals("1", compoundConf.get("A"));
80      assertEquals("2b", compoundConf.get("B"));
81      assertEquals(33, compoundConf.getInt("C", 0));
82      assertEquals("4", compoundConf.get("D"));
83      assertEquals(4, compoundConf.getInt("D", 0));
84      assertNull(compoundConf.get("E"));
85      assertEquals(6, compoundConf.getInt("F", 6));
86  
87      int cnt = 0;
88      for (Map.Entry<String,String> entry : compoundConf) {
89        cnt++;
90        if (entry.getKey().equals("B")) assertEquals("2b", entry.getValue());
91        else if (entry.getKey().equals("G")) assertEquals(null, entry.getValue());
92      }
93      // verify that entries from ImmutableConfigMap's are merged in the iterator's view
94      assertEquals(baseConfSize + 1, cnt);
95    }
96  
97    private ImmutableBytesWritable strToIbw(String s) {
98      return new ImmutableBytesWritable(Bytes.toBytes(s));
99    }
100 
101   @Test
102   public void testWithIbwMap() {
103     Map<ImmutableBytesWritable, ImmutableBytesWritable> map =
104       new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
105     map.put(strToIbw("B"), strToIbw("2b"));
106     map.put(strToIbw("C"), strToIbw("33"));
107     map.put(strToIbw("D"), strToIbw("4"));
108     // unlike config, note that IBW Maps can accept null values
109     map.put(strToIbw("G"), null);
110 
111     CompoundConfiguration compoundConf = new CompoundConfiguration()
112       .add(baseConf)
113       .add(map);
114     assertEquals("1", compoundConf.get("A"));
115     assertEquals("2b", compoundConf.get("B"));
116     assertEquals(33, compoundConf.getInt("C", 0));
117     assertEquals("4", compoundConf.get("D"));
118     assertEquals(4, compoundConf.getInt("D", 0));
119     assertNull(compoundConf.get("E"));
120     assertEquals(6, compoundConf.getInt("F", 6));
121     assertNull(compoundConf.get("G"));
122 
123     int cnt = 0;
124     for (Map.Entry<String,String> entry : compoundConf) {
125       cnt++;
126       if (entry.getKey().equals("B")) assertEquals("2b", entry.getValue());
127       else if (entry.getKey().equals("G")) assertEquals(null, entry.getValue());
128     }
129     // verify that entries from ImmutableConfigMap's are merged in the iterator's view
130     assertEquals(baseConfSize + 2, cnt);
131   }
132 
133   @Test
134   public void testLaterConfigsOverrideEarlier() {
135     Configuration map1 = new Configuration(false);
136     map1.set("A", "2");
137     map1.set("D", "5");
138     Configuration map2 = new Configuration(false);
139     String newValueForA = "3", newValueForB = "4";
140     map2.set("A", newValueForA);
141     map2.set("B", newValueForB);
142 
143     CompoundConfiguration compoundConf = new CompoundConfiguration()
144       .add(map1).add(baseConf);
145     assertEquals("1", compoundConf.get("A"));
146     assertEquals("5", compoundConf.get("D"));
147     compoundConf.add(map2);
148     assertEquals(newValueForA, compoundConf.get("A"));
149     assertEquals(newValueForB, compoundConf.get("B"));
150     assertEquals("5", compoundConf.get("D"));
151 
152     int cnt = 0;
153     for (Map.Entry<String,String> entry : compoundConf) {
154       cnt++;
155       if (entry.getKey().equals("A")) assertEquals(newValueForA, entry.getValue());
156       else if (entry.getKey().equals("B")) assertEquals(newValueForB, entry.getValue());
157     }
158     // verify that entries from ImmutableConfigMap's are merged in the iterator's view
159     assertEquals(baseConfSize + 1, cnt);
160   }
161 
162   @org.junit.Rule
163   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
164     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
165 }