1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24   
25  import java.util.regex.Pattern;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
30  import org.apache.hadoop.hbase.coprocessor.SampleRegionWALObserver;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  /**
36   * Test setting values in the descriptor
37   */
38  @Category(SmallTests.class)
39  public class TestHTableDescriptor {
40    final static Log LOG = LogFactory.getLog(TestHTableDescriptor.class);
41  
42    /**
43     * Test cps in the table description
44     * @throws Exception
45     */
46    @Test
47    public void testGetSetRemoveCP() throws Exception {
48      HTableDescriptor desc = new HTableDescriptor("table");
49      // simple CP
50      String className = BaseRegionObserver.class.getName();
51      // add and check that it is present
52      desc.addCoprocessor(className);
53      assertTrue(desc.hasCoprocessor(className));
54      // remove it and check that it is gone
55      desc.removeCoprocessor(className);
56      assertFalse(desc.hasCoprocessor(className));
57    }
58  
59    /**
60     * Test cps in the table description
61     * @throws Exception
62     */
63    @Test
64    public void testSetListRemoveCP() throws Exception {
65      HTableDescriptor desc = new HTableDescriptor("testGetSetRemoveCP");
66      // simple CP
67      String className1 = BaseRegionObserver.class.getName();
68      String className2 = SampleRegionWALObserver.class.getName();
69      // Check that any coprocessor is present.
70      assertTrue(desc.getCoprocessors().size() == 0);
71  
72      // Add the 1 coprocessor and check if present.
73      desc.addCoprocessor(className1);
74      assertTrue(desc.getCoprocessors().size() == 1);
75      assertTrue(desc.getCoprocessors().contains(className1));
76  
77      // Add the 2nd coprocessor and check if present.
78      // remove it and check that it is gone
79      desc.addCoprocessor(className2);
80      assertTrue(desc.getCoprocessors().size() == 2);
81      assertTrue(desc.getCoprocessors().contains(className2));
82  
83      // Remove one and check
84      desc.removeCoprocessor(className1);
85      assertTrue(desc.getCoprocessors().size() == 1);
86      assertFalse(desc.getCoprocessors().contains(className1));
87      assertTrue(desc.getCoprocessors().contains(className2));
88  
89      // Remove the last and check
90      desc.removeCoprocessor(className2);
91      assertTrue(desc.getCoprocessors().size() == 0);
92      assertFalse(desc.getCoprocessors().contains(className1));
93      assertFalse(desc.getCoprocessors().contains(className2));
94    }
95  
96    /**
97     * Test that we add and remove strings from settings properly.
98     * @throws Exception
99     */
100   @Test
101   public void testRemoveString() throws Exception {
102     HTableDescriptor desc = new HTableDescriptor("table");
103     String key = "Some";
104     String value = "value";
105     desc.setValue(key, value);
106     assertEquals(value, desc.getValue(key));
107     desc.remove(key);
108     assertEquals(null, desc.getValue(key));
109   }
110 
111   /**
112    * Test default value handling for maxFileSize
113    */
114   @Test
115   public void testGetMaxFileSize() {
116     HTableDescriptor desc = new HTableDescriptor("table");
117     assertEquals(-1, desc.getMaxFileSize());
118     desc.setMaxFileSize(1111L);
119     assertEquals(1111L, desc.getMaxFileSize());
120   }
121 
122   /**
123    * Test default value handling for memStoreFlushSize
124    */
125   @Test
126   public void testGetMemStoreFlushSize() {
127     HTableDescriptor desc = new HTableDescriptor("table");
128     assertEquals(-1, desc.getMemStoreFlushSize());
129     desc.setMemStoreFlushSize(1111L);
130     assertEquals(1111L, desc.getMemStoreFlushSize());
131   }
132 
133   String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",  };
134   String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok" };
135 
136   @Test
137   public void testLegalHTableNames() {
138     for (String tn : legalTableNames) {
139       HTableDescriptor.isLegalTableName(Bytes.toBytes(tn));
140     }
141   }
142 
143   @Test
144   public void testIllegalHTableNames() {
145     for (String tn : illegalTableNames) {
146       try {
147         HTableDescriptor.isLegalTableName(Bytes.toBytes(tn));
148         fail("invalid tablename " + tn + " should have failed");
149       } catch (Exception e) {
150         // expected
151       }
152     }
153   }
154 
155   @Test
156   public void testLegalHTableNamesRegex() {
157     for (String tn : legalTableNames) {
158       LOG.info("Testing: '" + tn + "'");
159       assertTrue(Pattern.matches(HTableDescriptor.VALID_USER_TABLE_REGEX, tn));
160     }
161   }
162 
163   @Test
164   public void testIllegalHTableNamesRegex() {
165     for (String tn : illegalTableNames) {
166       LOG.info("Testing: '" + tn + "'");
167       assertFalse(Pattern.matches(HTableDescriptor.VALID_USER_TABLE_REGEX, tn));
168     }
169   }
170 }