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.mapreduce;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.testclassification.SmallTests;
23  import org.apache.hadoop.util.ReflectionUtils;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  import java.util.HashSet;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  @Category(SmallTests.class)
34  public class TestTableSplit {
35    @Test
36    public void testHashCode() {
37      TableSplit split1 = new TableSplit(TableName.valueOf("table"),
38          "row-start".getBytes(),
39          "row-end".getBytes(), "location");
40      TableSplit split2 = new TableSplit(TableName.valueOf("table"),
41          "row-start".getBytes(),
42          "row-end".getBytes(), "location");
43      assertEquals (split1, split2);
44      assertTrue   (split1.hashCode() == split2.hashCode());
45      HashSet<TableSplit> set = new HashSet<TableSplit>(2);
46      set.add(split1);
47      set.add(split2);
48      assertTrue(set.size() == 1);
49    }
50  
51    /**
52     * length of region should not influence hashcode
53     * */
54    @Test
55    public void testHashCode_length() {
56      TableSplit split1 = new TableSplit(TableName.valueOf("table"),
57              "row-start".getBytes(),
58              "row-end".getBytes(), "location", 1984);
59      TableSplit split2 = new TableSplit(TableName.valueOf("table"),
60              "row-start".getBytes(),
61              "row-end".getBytes(), "location", 1982);
62  
63      assertEquals (split1, split2);
64      assertTrue   (split1.hashCode() == split2.hashCode());
65      HashSet<TableSplit> set = new HashSet<TableSplit>(2);
66      set.add(split1);
67      set.add(split2);
68      assertTrue(set.size() == 1);
69    }
70  
71    /**
72     * Length of region need to be properly serialized.
73     * */
74    @Test
75    public void testLengthIsSerialized() throws Exception {
76      TableSplit split1 = new TableSplit(TableName.valueOf("table"),
77              "row-start".getBytes(),
78              "row-end".getBytes(), "location", 666);
79  
80      TableSplit deserialized = new TableSplit(TableName.valueOf("table"),
81              "row-start2".getBytes(),
82              "row-end2".getBytes(), "location1");
83      ReflectionUtils.copy(new Configuration(), split1, deserialized);
84  
85      Assert.assertEquals(666, deserialized.getLength());
86    }
87  
88    @Test
89    public void testToString() {
90      TableSplit split =
91          new TableSplit(TableName.valueOf("table"), "row-start".getBytes(), "row-end".getBytes(),
92              "location");
93      String str =
94          "HBase table split(table name: table, scan: , start row: row-start, "
95              + "end row: row-end, region location: location)";
96      Assert.assertEquals(str, split.toString());
97  
98      split = new TableSplit((TableName) null, null, null, null);
99      str =
100         "HBase table split(table name: null, scan: , start row: null, "
101             + "end row: null, region location: null)";
102     Assert.assertEquals(str, split.toString());
103   }
104 }
105