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  
40    @Override
41    protected void setUp() throws Exception {
42      baseConf = new Configuration();
43      baseConf.set("A", "1");
44      baseConf.setInt("B", 2);
45      baseConf.set("C", "3");
46    }
47  
48    @Test
49    public void testBasicFunctionality() throws ClassNotFoundException {
50      CompoundConfiguration compoundConf = new CompoundConfiguration()
51          .add(baseConf);
52      assertEquals("1", compoundConf.get("A"));
53      assertEquals(2, compoundConf.getInt("B", 0));
54      assertEquals(3, compoundConf.getInt("C", 0));
55      assertEquals(0, compoundConf.getInt("D", 0));
56  
57      assertEquals(CompoundConfiguration.class, compoundConf
58          .getClassByName(CompoundConfiguration.class.getName()));
59      try {
60        compoundConf.getClassByName("bad_class_name");
61        fail("Trying to load bad_class_name should throw an exception");
62      } catch (ClassNotFoundException e) {
63        // win!
64      }
65    }
66  
67    @Test
68    public void testWithConfig() {
69      Configuration conf = new Configuration();
70      conf.set("B", "2b");
71      conf.set("C", "33");
72      conf.set("D", "4");
73  
74      CompoundConfiguration compoundConf = new CompoundConfiguration()
75          .add(baseConf)
76          .add(conf);
77      assertEquals("1", compoundConf.get("A"));
78      assertEquals("2b", compoundConf.get("B"));
79      assertEquals(33, compoundConf.getInt("C", 0));
80      assertEquals("4", compoundConf.get("D"));
81      assertEquals(4, compoundConf.getInt("D", 0));
82      assertNull(compoundConf.get("E"));
83      assertEquals(6, compoundConf.getInt("F", 6));
84    }
85  
86    private ImmutableBytesWritable strToIbw(String s) {
87      return new ImmutableBytesWritable(Bytes.toBytes(s));
88    }
89  
90    @Test
91    public void testWithIbwMap() {
92      Map<ImmutableBytesWritable, ImmutableBytesWritable> map =
93        new HashMap<ImmutableBytesWritable, ImmutableBytesWritable>();
94      map.put(strToIbw("B"), strToIbw("2b"));
95      map.put(strToIbw("C"), strToIbw("33"));
96      map.put(strToIbw("D"), strToIbw("4"));
97      // unlike config, note that IBW Maps can accept null values
98      map.put(strToIbw("G"), null);
99  
100     CompoundConfiguration compoundConf = new CompoundConfiguration()
101       .add(baseConf)
102       .add(map);
103     assertEquals("1", compoundConf.get("A"));
104     assertEquals("2b", compoundConf.get("B"));
105     assertEquals(33, compoundConf.getInt("C", 0));
106     assertEquals("4", compoundConf.get("D"));
107     assertEquals(4, compoundConf.getInt("D", 0));
108     assertNull(compoundConf.get("E"));
109     assertEquals(6, compoundConf.getInt("F", 6));
110     assertNull(compoundConf.get("G"));
111   }
112 
113   @org.junit.Rule
114   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
115     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
116 }