View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapred;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Assert;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestSplitTable {
34  
35    @Test
36    @SuppressWarnings("deprecation")
37    public void testSplitTableCompareTo() {
38      TableSplit aTableSplit = new TableSplit(Bytes.toBytes("tableA"),
39          Bytes.toBytes("aaa"), Bytes.toBytes("ddd"), "locationA");
40  
41      TableSplit bTableSplit = new TableSplit(Bytes.toBytes("tableA"),
42          Bytes.toBytes("iii"), Bytes.toBytes("kkk"), "locationA");
43  
44      TableSplit cTableSplit = new TableSplit(Bytes.toBytes("tableA"),
45          Bytes.toBytes("lll"), Bytes.toBytes("zzz"), "locationA");
46  
47      assertTrue(aTableSplit.compareTo(aTableSplit) == 0);
48      assertTrue(bTableSplit.compareTo(bTableSplit) == 0);
49      assertTrue(cTableSplit.compareTo(cTableSplit) == 0);
50  
51      assertTrue(aTableSplit.compareTo(bTableSplit) < 0);
52      assertTrue(bTableSplit.compareTo(aTableSplit) > 0);
53  
54      assertTrue(aTableSplit.compareTo(cTableSplit) < 0);
55      assertTrue(cTableSplit.compareTo(aTableSplit) > 0);
56  
57      assertTrue(bTableSplit.compareTo(cTableSplit) < 0);
58      assertTrue(cTableSplit.compareTo(bTableSplit) > 0);
59  
60      assertTrue(cTableSplit.compareTo(aTableSplit) > 0);
61    }
62  
63    @Test
64    @SuppressWarnings("deprecation")
65    public void testSplitTableEquals() {
66      byte[] tableA = Bytes.toBytes("tableA");
67      byte[] aaa = Bytes.toBytes("aaa");
68      byte[] ddd = Bytes.toBytes("ddd");
69      String locationA = "locationA";
70  
71      TableSplit tablesplit = new TableSplit(tableA, aaa, ddd, locationA);
72  
73      TableSplit tableB = new TableSplit(Bytes.toBytes("tableB"), aaa, ddd, locationA);
74      assertNotEquals(tablesplit.hashCode(), tableB.hashCode());
75      assertNotEquals(tablesplit, tableB);
76  
77      TableSplit startBbb = new TableSplit(tableA, Bytes.toBytes("bbb"), ddd, locationA);
78      assertNotEquals(tablesplit.hashCode(), startBbb.hashCode());
79      assertNotEquals(tablesplit, startBbb);
80  
81      TableSplit endEee = new TableSplit(tableA, aaa, Bytes.toBytes("eee"), locationA);
82      assertNotEquals(tablesplit.hashCode(), endEee.hashCode());
83      assertNotEquals(tablesplit, endEee);
84  
85      TableSplit locationB = new TableSplit(tableA, aaa, ddd, "locationB");
86      assertNotEquals(tablesplit.hashCode(), locationB.hashCode());
87      assertNotEquals(tablesplit, locationB);
88  
89      TableSplit same = new TableSplit(tableA, aaa, ddd, locationA);
90      assertEquals(tablesplit.hashCode(), same.hashCode());
91      assertEquals(tablesplit, same);
92    }
93  
94    @Test
95    @SuppressWarnings("deprecation")
96    public void testToString() {
97      TableSplit split =
98          new TableSplit(TableName.valueOf("table"), "row-start".getBytes(), "row-end".getBytes(),
99              "location");
100     String str =
101         "HBase table split(table name: table, start row: row-start, "
102             + "end row: row-end, region location: location)";
103     Assert.assertEquals(str, split.toString());
104 
105     split = new TableSplit((TableName) null, null, null, null);
106     str =
107         "HBase table split(table name: null, start row: null, "
108             + "end row: null, region location: null)";
109     Assert.assertEquals(str, split.toString());
110   }
111 }