1- /**
1+ /*
22 * Licensed to the Apache Software Foundation (ASF) under one
33 * or more contributor license agreements. See the NOTICE file
44 * distributed with this work for additional information
2121import static org .junit .Assert .assertEquals ;
2222import static org .junit .Assert .assertSame ;
2323import static org .junit .Assert .fail ;
24-
2524import java .nio .ByteBuffer ;
2625import java .util .HashMap ;
2726import java .util .Map ;
28- import org .apache .hadoop .hbase .testclassification .MediumTests ;
2927import org .apache .hadoop .hbase .testclassification .MiscTests ;
28+ import org .apache .hadoop .hbase .testclassification .SmallTests ;
3029import org .apache .hadoop .hbase .util .Bytes ;
3130import org .junit .ClassRule ;
3231import org .junit .Test ;
3332import org .junit .experimental .categories .Category ;
34- import org .junit .rules .TestWatcher ;
35- import org .junit .runner .Description ;
3633
3734/**
38- * Returns a {@code byte[]} containing the name of the currently running test method .
35+ * Tests for various kinds of TableNames .
3936 */
40- @ Category ({MiscTests .class , MediumTests .class })
41- public class TestTableName extends TestWatcher {
42-
37+ @ Category ({MiscTests .class , SmallTests .class })
38+ public class TestTableName {
4339 @ ClassRule
4440 public static final HBaseClassTestRule CLASS_RULE =
4541 HBaseClassTestRule .forClass (TestTableName .class );
4642
47- private TableName tableName ;
48-
49- /**
50- * Invoked when a test is about to start
51- */
52- @ Override
53- protected void starting (Description description ) {
54- tableName = TableName .valueOf (description .getMethodName ());
55- }
56-
57- public TableName getTableName () {
58- return tableName ;
59- }
60-
61- String [] emptyNames = {"" , " " };
62- String [] invalidNamespace = {":a" , "%:a" };
63- String [] legalTableNames = {"foo" , "with-dash_under.dot" , "_under_start_ok" ,
43+ private static String [] emptyNames = {"" , " " };
44+ private static String [] invalidNamespace = {":a" , "%:a" };
45+ private static String [] legalTableNames = {"foo" , "with-dash_under.dot" , "_under_start_ok" ,
6446 "with-dash.with_underscore" , "02-01-2012.my_table_01-02" , "xyz._mytable_" , "9_9_0.table_02" ,
6547 "dot1.dot2.table" , "new.-mytable" , "with-dash.with.dot" , "legal..t2" , "legal..legal.t2" ,
6648 "trailingdots.." , "trailing.dots..." , "ns:mytable" , "ns:_mytable_" , "ns:my_table_01-02" };
67- String [] illegalTableNames = {".dot_start_illegal" , "-dash_start_illegal" , "spaces not ok " ,
68- "-dash-.start_illegal" , "new.table with space" , "01 .table" , "ns:-illegaldash" ,
49+ private static String [] illegalTableNames = {".dot_start_illegal" , "-dash_start_illegal" ,
50+ "spaces not ok" , " -dash-.start_illegal" , "new.table with space" , "01 .table" , "ns:-illegaldash" ,
6951 "new:.illegaldot" , "new:illegalcolon1:" , "new:illegalcolon1:2" };
7052
71-
72- @ Test (expected = IllegalArgumentException .class )
73- public void testInvalidNamespace () {
74- for (String tn : invalidNamespace ) {
75- TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
76- fail ("invalid namespace " + tn
77- + " should have failed with IllegalArgumentException for namespace" );
78- }
79- }
80-
81- @ Test (expected = IllegalArgumentException .class )
82- public void testEmptyNamespaceName () {
83- for (String nn : emptyNames ) {
84- TableName .isLegalNamespaceName (Bytes .toBytes (nn ));
85- fail ("invalid Namespace name " + nn + " should have failed with IllegalArgumentException" );
86- }
87- }
88-
89- @ Test (expected = IllegalArgumentException .class )
90- public void testEmptyTableName () {
91- for (String tn : emptyNames ) {
92- TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
93- fail ("invalid tablename " + tn + " should have failed with IllegalArgumentException" );
94- }
95- }
96-
97- @ Test
98- public void testLegalHTableNames () {
99- for (String tn : legalTableNames ) {
100- TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
101- }
102- }
103-
104- @ Test
105- public void testIllegalHTableNames () {
106- for (String tn : illegalTableNames ) {
107- try {
108- TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
109- fail ("invalid tablename " + tn + " should have failed" );
110- } catch (Exception e ) {
111- // expected
112- }
113- }
114- }
115-
11653 static class Names {
11754 String ns ;
11855 byte [] nsb ;
@@ -147,7 +84,6 @@ public boolean equals(Object o) {
14784 if (!tn .equals (names .tn )) {
14885 return false ;
14986 }
150-
15187 return true ;
15288 }
15389
@@ -159,7 +95,7 @@ public int hashCode() {
15995 }
16096 }
16197
162- Names [] names = new Names [] {
98+ private static Names [] names = new Names [] {
16399 new Names ("n1" , "n1" ),
164100 new Names ("n2" , "n2" ),
165101 new Names ("table1" , "table1" ),
@@ -172,9 +108,52 @@ public int hashCode() {
172108 new Names ("n2" , "table2" )
173109 };
174110
111+ @ Test (expected = IllegalArgumentException .class )
112+ public void testInvalidNamespace () {
113+ for (String tn : invalidNamespace ) {
114+ TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
115+ fail ("invalid namespace " + tn
116+ + " should have failed with IllegalArgumentException for namespace" );
117+ }
118+ }
119+
120+ @ Test (expected = IllegalArgumentException .class )
121+ public void testEmptyNamespaceName () {
122+ for (String nn : emptyNames ) {
123+ TableName .isLegalNamespaceName (Bytes .toBytes (nn ));
124+ fail ("invalid Namespace name " + nn + " should have failed with IllegalArgumentException" );
125+ }
126+ }
127+
128+ @ Test (expected = IllegalArgumentException .class )
129+ public void testEmptyTableName () {
130+ for (String tn : emptyNames ) {
131+ TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
132+ fail ("invalid tablename " + tn + " should have failed with IllegalArgumentException" );
133+ }
134+ }
135+
175136 @ Test
176- public void testValueOf () {
137+ public void testLegalHTableNames () {
138+ for (String tn : legalTableNames ) {
139+ TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
140+ }
141+ }
177142
143+ @ Test
144+ public void testIllegalHTableNames () {
145+ for (String tn : illegalTableNames ) {
146+ try {
147+ TableName .isLegalFullyQualifiedTableName (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 testValueOf () {
178157 Map <String , TableName > inCache = new HashMap <>();
179158 // fill cache
180159 for (Names name : names ) {
@@ -188,7 +167,6 @@ public void testValueOf() {
188167 assertSame (inCache .get (name .nn ), validateNames (TableName .valueOf (
189168 ByteBuffer .wrap (name .nsb ), ByteBuffer .wrap (name .tnb )), name ));
190169 }
191-
192170 }
193171
194172 private TableName validateNames (TableName expected , Names names ) {
@@ -200,5 +178,4 @@ private TableName validateNames(TableName expected, Names names) {
200178 assertArrayEquals (expected .getNamespace (), names .nsb );
201179 return expected ;
202180 }
203-
204181}
0 commit comments