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 java.nio.ByteBuffer;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import static org.junit.Assert.assertArrayEquals;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotEquals;
27  import static org.junit.Assert.assertSame;
28  import static org.junit.Assert.fail;
29  
30  import org.apache.hadoop.hbase.MediumTests;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  import org.junit.rules.TestWatcher;
36  import org.junit.runner.Description;
37  
38  /**
39   * Returns a {@code byte[]} containing the name of the currently running test method.
40   */
41  @Category(MediumTests.class)
42  public class TestTableName extends TestWatcher {
43    private TableName tableName;
44  
45    /**
46     * Invoked when a test is about to start
47     */
48    @Override
49    protected void starting(Description description) {
50      tableName = TableName.valueOf(description.getMethodName());
51    }
52  
53    public TableName getTableName() {
54      return tableName;
55    }
56    
57    String emptyTableNames[] ={"", " "};
58    String invalidNamespace[] = {":a", "%:a"};
59    String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
60        "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
61        , "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
62        "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
63    String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
64        "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
65        "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
66  
67  
68    @Test(expected = IllegalArgumentException.class)
69    public void testInvalidNamespace() {
70      for (String tn : invalidNamespace) {
71        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
72        fail("invalid namespace " + tn + " should have failed with IllegalArgumentException for namespace");
73      }
74    }
75  
76    @Test(expected = IllegalArgumentException.class)
77    public void testEmptyTableName() {
78      for (String tn : emptyTableNames) {
79        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
80        fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
81      }
82    }
83  
84    @Test
85    public void testLegalHTableNames() {
86      for (String tn : legalTableNames) {
87        TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
88      }
89    }
90  
91    @Test
92    public void testIllegalHTableNames() {
93      for (String tn : illegalTableNames) {
94        try {
95          TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
96          fail("invalid tablename " + tn + " should have failed");
97        } catch (Exception e) {
98          // expected
99        }
100     }
101   }
102 
103   class Names {
104     String ns;
105     byte[] nsb;
106     String tn;
107     byte[] tnb;
108     String nn;
109     byte[] nnb;
110 
111     Names(String ns, String tn) {
112       this.ns = ns;
113       nsb = ns.getBytes();
114       this.tn = tn;
115       tnb = tn.getBytes();
116       nn = this.ns + ":" + this.tn;
117       nnb = nn.getBytes();
118     }
119 
120     @Override
121     public boolean equals(Object o) {
122       if (this == o) return true;
123       if (o == null || getClass() != o.getClass()) return false;
124 
125       Names names = (Names) o;
126 
127       if (!ns.equals(names.ns)) return false;
128       if (!tn.equals(names.tn)) return false;
129 
130       return true;
131     }
132 
133     @Override
134     public int hashCode() {
135       int result = ns.hashCode();
136       result = 31 * result + tn.hashCode();
137       return result;
138     }
139   }
140 
141   Names[] names = new Names[] {
142       new Names("n1", "n1"),
143       new Names("n2", "n2"),
144       new Names("table1", "table1"),
145       new Names("table2", "table2"),
146       new Names("table2", "table1"),
147       new Names("table1", "table2"),
148       new Names("n1", "table1"),
149       new Names("n1", "table1"),
150       new Names("n2", "table2"),
151       new Names("n2", "table2")
152   };
153 
154   @Test
155   public void testValueOf() {
156 
157     Map<String, TableName> inCache = new HashMap<String, TableName>();
158     // fill cache
159     for (Names name : names) {
160       inCache.put(name.nn, TableName.valueOf(name.ns, name.tn));
161     }
162     for (Names name : names) {
163       assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.ns, name.tn), name));
164       assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nsb, name.tnb), name));
165       assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nn), name));
166       assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nnb), name));
167       assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(
168           ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name));
169     }
170 
171   }
172 
173   private TableName validateNames(TableName expected, Names names) {
174     assertEquals(expected.getNameAsString(), names.nn);
175     assertArrayEquals(expected.getName(), names.nnb);
176     assertEquals(expected.getQualifierAsString(), names.tn);
177     assertArrayEquals(expected.getQualifier(), names.tnb);
178     assertEquals(expected.getNamespaceAsString(), names.ns);
179     assertArrayEquals(expected.getNamespace(), names.nsb);
180     return expected;
181   }
182 
183 }