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