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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.HashSet;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.MapReduceTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.util.ReflectionUtils;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.junit.jupiter.api.TestInfo;
033
034@Tag(MapReduceTests.TAG)
035@Tag(SmallTests.TAG)
036public class TestTableSplit {
037
038  @Test
039  public void testHashCode(TestInfo testInfo) {
040    TableSplit split1 = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
041      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
042    TableSplit split2 = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
043      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
044    assertEquals(split1, split2);
045    assertTrue(split1.hashCode() == split2.hashCode());
046    HashSet<TableSplit> set = new HashSet<>(2);
047    set.add(split1);
048    set.add(split2);
049    assertEquals(1, set.size());
050  }
051
052  /**
053   * length of region should not influence hashcode
054   */
055  @Test
056  public void testHashCode_length(TestInfo testInfo) {
057    TableSplit split1 = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
058      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 1984);
059    TableSplit split2 = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
060      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 1982);
061
062    assertEquals(split1, split2);
063    assertTrue(split1.hashCode() == split2.hashCode());
064    HashSet<TableSplit> set = new HashSet<>(2);
065    set.add(split1);
066    set.add(split2);
067    assertEquals(1, set.size());
068  }
069
070  /**
071   * Length of region need to be properly serialized.
072   */
073  @Test
074  public void testLengthIsSerialized(TestInfo testInfo) throws Exception {
075    TableSplit split1 = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
076      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", 666);
077
078    TableSplit deserialized =
079      new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
080        Bytes.toBytes("row-start2"), Bytes.toBytes("row-end2"), "location1");
081    ReflectionUtils.copy(new Configuration(), split1, deserialized);
082
083    assertEquals(666, deserialized.getLength());
084  }
085
086  @Test
087  public void testToString(TestInfo testInfo) {
088    TableSplit split = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()),
089      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location");
090    String str = "Split(tablename=" + testInfo.getTestMethod().get().getName()
091      + ", startrow=row-start, " + "endrow=row-end, regionLocation=location, " + "regionname=)";
092    assertEquals(str, split.toString());
093
094    split = new TableSplit(TableName.valueOf(testInfo.getTestMethod().get().getName()), null,
095      Bytes.toBytes("row-start"), Bytes.toBytes("row-end"), "location", "encoded-region-name",
096      1000L);
097    str = "Split(tablename=" + testInfo.getTestMethod().get().getName() + ", startrow=row-start, "
098      + "endrow=row-end, regionLocation=location, " + "regionname=encoded-region-name)";
099    assertEquals(str, split.toString());
100
101    split = new TableSplit(null, null, null, null);
102    str = "Split(tablename=null, startrow=null, " + "endrow=null, regionLocation=null, "
103      + "regionname=)";
104    assertEquals(str, split.toString());
105
106    split = new TableSplit(null, null, null, null, null, null, 1000L);
107    str = "Split(tablename=null, startrow=null, " + "endrow=null, regionLocation=null, "
108      + "regionname=null)";
109    assertEquals(str, split.toString());
110  }
111}