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.mapred;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
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.junit.Assert;
030import org.junit.ClassRule;
031import org.junit.Rule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.junit.rules.TestName;
035
036@Category({MapReduceTests.class, SmallTests.class})
037public class TestSplitTable {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestSplitTable.class);
042
043  @Rule
044  public TestName name = new TestName();
045
046  @Test
047  @SuppressWarnings({"deprecation", "SelfComparison"})
048  public void testSplitTableCompareTo() {
049    TableSplit aTableSplit = new TableSplit(Bytes.toBytes("tableA"),
050        Bytes.toBytes("aaa"), Bytes.toBytes("ddd"), "locationA");
051
052    TableSplit bTableSplit = new TableSplit(Bytes.toBytes("tableA"),
053        Bytes.toBytes("iii"), Bytes.toBytes("kkk"), "locationA");
054
055    TableSplit cTableSplit = new TableSplit(Bytes.toBytes("tableA"),
056        Bytes.toBytes("lll"), Bytes.toBytes("zzz"), "locationA");
057
058    assertEquals(0, aTableSplit.compareTo(aTableSplit));
059    assertEquals(0, bTableSplit.compareTo(bTableSplit));
060    assertEquals(0, cTableSplit.compareTo(cTableSplit));
061
062    assertTrue(aTableSplit.compareTo(bTableSplit) < 0);
063    assertTrue(bTableSplit.compareTo(aTableSplit) > 0);
064
065    assertTrue(aTableSplit.compareTo(cTableSplit) < 0);
066    assertTrue(cTableSplit.compareTo(aTableSplit) > 0);
067
068    assertTrue(bTableSplit.compareTo(cTableSplit) < 0);
069    assertTrue(cTableSplit.compareTo(bTableSplit) > 0);
070
071    assertTrue(cTableSplit.compareTo(aTableSplit) > 0);
072  }
073
074  @Test
075  @SuppressWarnings("deprecation")
076  public void testSplitTableEquals() {
077    byte[] tableA = Bytes.toBytes("tableA");
078    byte[] aaa = Bytes.toBytes("aaa");
079    byte[] ddd = Bytes.toBytes("ddd");
080    String locationA = "locationA";
081
082    TableSplit tablesplit = new TableSplit(tableA, aaa, ddd, locationA);
083
084    TableSplit tableB = new TableSplit(Bytes.toBytes("tableB"), aaa, ddd, locationA);
085    assertNotEquals(tablesplit.hashCode(), tableB.hashCode());
086    assertNotEquals(tablesplit, tableB);
087
088    TableSplit startBbb = new TableSplit(tableA, Bytes.toBytes("bbb"), ddd, locationA);
089    assertNotEquals(tablesplit.hashCode(), startBbb.hashCode());
090    assertNotEquals(tablesplit, startBbb);
091
092    TableSplit endEee = new TableSplit(tableA, aaa, Bytes.toBytes("eee"), locationA);
093    assertNotEquals(tablesplit.hashCode(), endEee.hashCode());
094    assertNotEquals(tablesplit, endEee);
095
096    TableSplit locationB = new TableSplit(tableA, aaa, ddd, "locationB");
097    assertNotEquals(tablesplit.hashCode(), locationB.hashCode());
098    assertNotEquals(tablesplit, locationB);
099
100    TableSplit same = new TableSplit(tableA, aaa, ddd, locationA);
101    assertEquals(tablesplit.hashCode(), same.hashCode());
102    assertEquals(tablesplit, same);
103  }
104
105  @Test
106  @SuppressWarnings("deprecation")
107  public void testToString() {
108    TableSplit split =
109        new TableSplit(TableName.valueOf(name.getMethodName()), "row-start".getBytes(), "row-end".getBytes(),
110            "location");
111    String str =
112        "HBase table split(table name: " + name.getMethodName() + ", start row: row-start, "
113            + "end row: row-end, region location: location)";
114    Assert.assertEquals(str, split.toString());
115
116    split = new TableSplit((TableName) null, null, null, null);
117    str =
118        "HBase table split(table name: null, start row: null, "
119            + "end row: null, region location: null)";
120    Assert.assertEquals(str, split.toString());
121  }
122}