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