1
- /**
1
+ /*
2
2
* Licensed to the Apache Software Foundation (ASF) under one
3
3
* or more contributor license agreements. See the NOTICE file
4
4
* distributed with this work for additional information
20
20
import static org .junit .Assert .assertArrayEquals ;
21
21
import static org .junit .Assert .assertEquals ;
22
22
import static org .junit .Assert .assertSame ;
23
- import static org .junit .Assert .fail ;
24
-
23
+ import static org .junit .Assert .assertThrows ;
25
24
import java .nio .ByteBuffer ;
26
25
import java .util .HashMap ;
27
26
import java .util .Map ;
28
- import org .apache .hadoop .hbase .testclassification .MediumTests ;
29
27
import org .apache .hadoop .hbase .testclassification .MiscTests ;
28
+ import org .apache .hadoop .hbase .testclassification .SmallTests ;
30
29
import org .apache .hadoop .hbase .util .Bytes ;
31
30
import org .junit .ClassRule ;
32
31
import org .junit .Test ;
33
32
import org .junit .experimental .categories .Category ;
34
- import org .junit .rules .TestWatcher ;
35
- import org .junit .runner .Description ;
36
33
37
34
/**
38
- * Returns a {@code byte[]} containing the name of the currently running test method .
35
+ * Tests for various kinds of TableNames .
39
36
*/
40
- @ Category ({MiscTests .class , MediumTests .class })
41
- public class TestTableName extends TestWatcher {
42
-
37
+ @ Category ({MiscTests .class , SmallTests .class })
38
+ public class TestTableName {
43
39
@ ClassRule
44
40
public static final HBaseClassTestRule CLASS_RULE =
45
41
HBaseClassTestRule .forClass (TestTableName .class );
46
42
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" ,
64
46
"with-dash.with_underscore" , "02-01-2012.my_table_01-02" , "xyz._mytable_" , "9_9_0.table_02" ,
65
47
"dot1.dot2.table" , "new.-mytable" , "with-dash.with.dot" , "legal..t2" , "legal..legal.t2" ,
66
48
"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" ,
69
51
"new:.illegaldot" , "new:illegalcolon1:" , "new:illegalcolon1:2" };
70
52
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
-
116
53
static class Names {
117
54
String ns ;
118
55
byte [] nsb ;
@@ -147,7 +84,6 @@ public boolean equals(Object o) {
147
84
if (!tn .equals (names .tn )) {
148
85
return false ;
149
86
}
150
-
151
87
return true ;
152
88
}
153
89
@@ -159,7 +95,7 @@ public int hashCode() {
159
95
}
160
96
}
161
97
162
- Names [] names = new Names [] {
98
+ private static Names [] names = new Names [] {
163
99
new Names ("n1" , "n1" ),
164
100
new Names ("n2" , "n2" ),
165
101
new Names ("table1" , "table1" ),
@@ -172,9 +108,41 @@ public int hashCode() {
172
108
new Names ("n2" , "table2" )
173
109
};
174
110
175
- @ Test
176
- public void testValueOf () {
111
+ @ Test public void testInvalidNamespace () {
112
+ for (String tn : invalidNamespace ) {
113
+ assertThrows (IllegalArgumentException .class ,
114
+ () -> TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn )));
115
+ }
116
+ }
117
+
118
+ @ Test public void testEmptyNamespaceName () {
119
+ for (String nn : emptyNames ) {
120
+ assertThrows (IllegalArgumentException .class ,
121
+ () -> TableName .isLegalNamespaceName (Bytes .toBytes (nn )));
122
+ }
123
+ }
124
+
125
+ @ Test public void testEmptyTableName () {
126
+ for (String tn : emptyNames ) {
127
+ assertThrows (IllegalArgumentException .class ,
128
+ () -> TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn )));
129
+ }
130
+ }
131
+
132
+ @ Test public void testLegalHTableNames () {
133
+ for (String tn : legalTableNames ) {
134
+ TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn ));
135
+ }
136
+ }
177
137
138
+ @ Test public void testIllegalHTableNames () {
139
+ for (String tn : illegalTableNames ) {
140
+ assertThrows (Exception .class ,
141
+ () -> TableName .isLegalFullyQualifiedTableName (Bytes .toBytes (tn )));
142
+ }
143
+ }
144
+
145
+ @ Test public void testValueOf () {
178
146
Map <String , TableName > inCache = new HashMap <>();
179
147
// fill cache
180
148
for (Names name : names ) {
@@ -188,7 +156,6 @@ public void testValueOf() {
188
156
assertSame (inCache .get (name .nn ), validateNames (TableName .valueOf (
189
157
ByteBuffer .wrap (name .nsb ), ByteBuffer .wrap (name .tnb )), name ));
190
158
}
191
-
192
159
}
193
160
194
161
private TableName validateNames (TableName expected , Names names ) {
@@ -200,5 +167,4 @@ private TableName validateNames(TableName expected, Names names) {
200
167
assertArrayEquals (expected .getNamespace (), names .nsb );
201
168
return expected ;
202
169
}
203
-
204
170
}
0 commit comments