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.hbase.util.Bytes;
030import org.apache.hadoop.util.ReflectionUtils;
031import org.junit.Assert;
032import org.junit.ClassRule;
033import org.junit.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestName;
037
038@Category({ MapReduceTests.class, SmallTests.class })
039public class TestTableSplit {
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      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
051    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
052      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
053    assertEquals(split1, split2);
054    assertTrue(split1.hashCode() == split2.hashCode());
055    HashSet<TableSplit> set = new HashSet<>(2);
056    set.add(split1);
057    set.add(split2);
058    assertEquals(1, set.size());
059  }
060
061  /**
062   * length of region should not influence hashcode
063   */
064  @Test
065  public void testHashCode_length() {
066    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
067      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 1984);
068    TableSplit split2 = new TableSplit(TableName.valueOf(name.getMethodName()),
069      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 1982);
070
071    assertEquals(split1, split2);
072    assertTrue(split1.hashCode() == split2.hashCode());
073    HashSet<TableSplit> set = new HashSet<>(2);
074    set.add(split1);
075    set.add(split2);
076    assertEquals(1, set.size());
077  }
078
079  /**
080   * Length of region need to be properly serialized.
081   */
082  @Test
083  public void testLengthIsSerialized() throws Exception {
084    TableSplit split1 = new TableSplit(TableName.valueOf(name.getMethodName()),
085      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 666);
086
087    TableSplit deserialized = new TableSplit(TableName.valueOf(name.getMethodName()),
088      Bytes.toBytes("row-start2"), Bytes.toBytes("row-end2"), "location1");
089    ReflectionUtils.copy(new Configuration(), split1, deserialized);
090
091    Assert.assertEquals(666, deserialized.getLength());
092  }
093
094  @Test
095  public void testToString() {
096    TableSplit split = new TableSplit(TableName.valueOf(name.getMethodName()),
097      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
098    String str = "Split(tablename=" + name.getMethodName() + ", startrow=row-start, "
099      + "endrow=row-end, regionLocation=location, " + "regionname=)";
100    Assert.assertEquals(str, split.toString());
101
102    split =
103      new TableSplit(TableName.valueOf(name.getMethodName()), null, Bytes.toBytes("row-start"),
104        Bytes.toBytes("row-end"), "location", "encoded-region-name", 1000L);
105    str = "Split(tablename=" + name.getMethodName() + ", startrow=row-start, "
106      + "endrow=row-end, regionLocation=location, " + "regionname=encoded-region-name)";
107    Assert.assertEquals(str, split.toString());
108
109    split = new TableSplit(null, null, null, null);
110    str = "Split(tablename=null, startrow=null, " + "endrow=null, regionLocation=null, "
111      + "regionname=)";
112    Assert.assertEquals(str, split.toString());
113
114    split = new TableSplit(null, null, null, null, null, null, 1000L);
115    str = "Split(tablename=null, startrow=null, " + "endrow=null, regionLocation=null, "
116      + "regionname=null)";
117    Assert.assertEquals(str, split.toString());
118  }
119}