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  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestTableSplit.class);
042
043  @Rule
044  public TestName name = new TestName();
045
046  @Test
047  public void testHashCode() {
048    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
049        "row-start".getBytes(),
050        "row-end".getBytes(), "location");
051    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
052        "row-start".getBytes(),
053        "row-end".getBytes(), "location");
054    assertEquals(split1, split2);
055    assertTrue(split1.hashCode() == split2.hashCode());
056    HashSet<TableSplit> set = new HashSet<>(2);
057    set.add(split1);
058    set.add(split2);
059    assertEquals(1, set.size());
060  }
061
062  /**
063   * length of region should not influence hashcode
064   * */
065  @Test
066  public void testHashCode_length() {
067    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
068            "row-start".getBytes(),
069            "row-end".getBytes(), "location", 1984);
070    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
071            "row-start".getBytes(),
072            "row-end".getBytes(), "location", 1982);
073
074    assertEquals(split1, split2);
075    assertTrue(split1.hashCode() == split2.hashCode());
076    HashSet<TableSplit> set = new HashSet<>(2);
077    set.add(split1);
078    set.add(split2);
079    assertEquals(1, set.size());
080  }
081
082  /**
083   * Length of region need to be properly serialized.
084   * */
085  @Test
086  public void testLengthIsSerialized() throws Exception {
087    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
088            "row-start".getBytes(),
089            "row-end".getBytes(), "location", 666);
090
091    TableSplit deserialized = new TableSplit(TableName.valueOf(name.getMethodName()),
092            "row-start2".getBytes(),
093            "row-end2".getBytes(), "location1");
094    ReflectionUtils.copy(new Configuration(), split1, deserialized);
095
096    Assert.assertEquals(666, deserialized.getLength());
097  }
098
099  @Test
100  public void testToString() {
101    TableSplit split =
102        new TableSplit(TableName.valueOf(name.getMethodName()), "row-start".getBytes(), "row-end".getBytes(),
103            "location");
104    String str =
105        "HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, "
106            + "end row: row-end, region location: location, "
107            + "encoded region name: )";
108    Assert.assertEquals(str, split.toString());
109
110    split =
111        new TableSplit(TableName.valueOf(name.getMethodName()), null, "row-start".getBytes(),
112            "row-end".getBytes(), "location", "encoded-region-name", 1000L);
113    str =
114        "HBase table split(table name: " + name.getMethodName() + ", scan: , start row: row-start, "
115            + "end row: row-end, region location: location, "
116            + "encoded region name: encoded-region-name)";
117    Assert.assertEquals(str, split.toString());
118
119    split = new TableSplit(null, null, null, null);
120    str =
121        "HBase table split(table name: null, scan: , start row: null, "
122            + "end row: null, region location: null, "
123            + "encoded region name: )";
124    Assert.assertEquals(str, split.toString());
125
126    split = new TableSplit(null, null, null, null, null, null, 1000L);
127    str =
128        "HBase table split(table name: null, scan: , start row: null, "
129            + "end row: null, region location: null, "
130            + "encoded region name: null)";
131    Assert.assertEquals(str, split.toString());
132  }
133}