View Javadoc

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.util;
19  
20  import static org.junit.Assert.fail;
21  
22  import org.apache.hadoop.hbase.SmallTests;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  import org.junit.rules.TestWatcher;
28  import org.junit.runner.Description;
29  
30  /**
31   * Returns a {@code byte[]} containing the name of the currently running test method.
32   */
33  @Category(SmallTests.class)
34  public class TestTableName extends TestWatcher {
35    private TableName tableName;
36  
37    /**
38     * Invoked when a test is about to start
39     */
40    @Override
41    protected void starting(Description description) {
42      tableName = TableName.valueOf(description.getMethodName());
43    }
44  
45    public TableName getTableName() {
46      return tableName;
47    }
48    
49    String emptyTableNames[] ={"", " "};
50    String invalidNamespace[] = {":a", "%:a"};
51    String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
52        "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
53        , "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
54        "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
55    String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
56        "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
57        "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
58  
59  
60    @Test(expected = IllegalArgumentException.class)
61    public void testInvalidNamespace() {
62      for (String tn : invalidNamespace) {
63        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
64        fail("invalid namespace " + tn + " should have failed with IllegalArgumentException for namespace");
65      }
66    }
67  
68    @Test(expected = IllegalArgumentException.class)
69    public void testEmptyTableName() {
70      for (String tn : emptyTableNames) {
71        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
72        fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
73      }
74    }
75  
76    @Test
77    public void testLegalHTableNames() {
78      for (String tn : legalTableNames) {
79        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
80      }
81    }
82  
83    @Test
84    public void testIllegalHTableNames() {
85      for (String tn : illegalTableNames) {
86        try {
87          TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
88          fail("invalid tablename " + tn + " should have failed");
89        } catch (Exception e) {
90          // expected
91        }
92      }
93    }
94    
95  }