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