001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.HashSet;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.testclassification.MapReduceTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.util.ReflectionUtils;
030import org.junit.Assert;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036
037@Category({MapReduceTests.class, SmallTests.class})
038public class TestTableSplit {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestTableSplit.class);
043
044  @Rule
045  public TestName name = new TestName();
046
047  @Test
048  public void testHashCode() {
049    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
050        "row-start".getBytes(),
051        "row-end".getBytes(), "location");
052    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
053        "row-start".getBytes(),
054        "row-end".getBytes(), "location");
055    assertEquals (split1, split2);
056    assertTrue   (split1.hashCode() == split2.hashCode());
057    HashSet<TableSplit> set = new HashSet<>(2);
058    set.add(split1);
059    set.add(split2);
060    assertTrue(set.size() == 1);
061  }
062
063  /**
064   * length of region should not influence hashcode
065   * */
066  @Test
067  public void testHashCode_length() {
068    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
069            "row-start".getBytes(),
070            "row-end".getBytes(), "location", 1984);
071    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
072            "row-start".getBytes(),
073            "row-end".getBytes(), "location", 1982);
074
075    assertEquals (split1, split2);
076    assertTrue   (split1.hashCode() == split2.hashCode());
077    HashSet<TableSplit> set = new HashSet<>(2);
078    set.add(split1);
079    set.add(split2);
080    assertTrue(set.size() == 1);
081  }
082
083  /**
084   * Length of region need to be properly serialized.
085   * */
086  @Test
087  public void testLengthIsSerialized() throws Exception {
088    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
089            "row-start".getBytes(),
090            "row-end".getBytes(), "location", 666);
091
092    TableSplit deserialized = new TableSplit(TableName.valueOf(name.getMethodName()),
093            "row-start2".getBytes(),
094            "row-end2".getBytes(), "location1");
095    ReflectionUtils.copy(new Configuration(), split1, deserialized);
096
097    Assert.assertEquals(666, deserialized.getLength());
098  }
099
100  @Test
101  public void testToString() {
102    TableSplit split =
103        new TableSplit(TableName.valueOf(name.getMethodName()), "row-start".getBytes(), "row-end".getBytes(),
104            "location");
105    String str =
106        "HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, "
107            + "end row: row-end, region location: location, "
108            + "encoded region name: )";
109    Assert.assertEquals(str, split.toString());
110
111    split =
112        new TableSplit(TableName.valueOf(name.getMethodName()), null, "row-start".getBytes(),
113            "row-end".getBytes(), "location", "encoded-region-name", 1000L);
114    str =
115        "HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, "
116            + "end row: row-end, region location: location, "
117            + "encoded region name: encoded-region-name)";
118    Assert.assertEquals(str, split.toString());
119
120    split = new TableSplit((TableName) null, null, null, null);
121    str =
122        "HBase table split(table name: null, scan: , start row: null, "
123            + "end row: null, region location: null, "
124            + "encoded region name: )";
125    Assert.assertEquals(str, split.toString());
126
127    split = new TableSplit((TableName) null, null, null, null, null, null, 1000L);
128    str =
129        "HBase table split(table name: null, scan: , start row: null, "
130            + "end row: null, region location: null, "
131            + "encoded region name: null)";
132    Assert.assertEquals(str, split.toString());
133  }
134}
135