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.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HRegionInfo;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.HbckRegionInfo.MetaEntry;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033/**
034 * Test the comparator used by Hbck.
035 */
036@Category({ MiscTests.class, SmallTests.class })
037public class TestHBaseFsckComparator {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestHBaseFsckComparator.class);
042
043  TableName table = TableName.valueOf("table1");
044  TableName table2 = TableName.valueOf("table2");
045  byte[] keyStart = Bytes.toBytes("");
046  byte[] keyA = Bytes.toBytes("A");
047  byte[] keyB = Bytes.toBytes("B");
048  byte[] keyC = Bytes.toBytes("C");
049  byte[] keyEnd = Bytes.toBytes("");
050
051  static HbckRegionInfo genHbckInfo(TableName table, byte[] start, byte[] end, int time) {
052    return new HbckRegionInfo(new MetaEntry(new HRegionInfo(table, start, end), null, time));
053  }
054
055  @Test
056  public void testEquals() {
057    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
058    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyB, 0);
059    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2));
060    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1));
061  }
062
063  @Test
064  public void testEqualsInstance() {
065    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
066    HbckRegionInfo hi2 = hi1;
067    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2));
068    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1));
069  }
070
071  @Test
072  public void testDiffTable() {
073    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
074    HbckRegionInfo hi2 = genHbckInfo(table2, keyA, keyC, 0);
075    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
076    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
077  }
078
079  @Test
080  public void testDiffStartKey() {
081    HbckRegionInfo hi1 = genHbckInfo(table, keyStart, keyC, 0);
082    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
083    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
084    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
085  }
086
087  @Test
088  public void testDiffEndKey() {
089    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
090    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
091    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
092    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
093  }
094
095  @Test
096  public void testAbsEndKey() {
097    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
098    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyEnd, 0);
099    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
100    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
101  }
102}