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