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