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 =
044      TableName.valueOf("table1");
045  TableName table2 =
046      TableName.valueOf("table2");
047  byte[] keyStart = Bytes.toBytes("");
048  byte[] keyA = Bytes.toBytes("A");
049  byte[] keyB = Bytes.toBytes("B");
050  byte[] keyC = Bytes.toBytes("C");
051  byte[] keyEnd = Bytes.toBytes("");
052
053  static HbckRegionInfo genHbckInfo(TableName table, byte[] start, byte[] end, int time) {
054    return new HbckRegionInfo(new MetaEntry(new HRegionInfo(table, start, end), null,
055        time));
056  }
057
058  @Test
059  public void testEquals() {
060    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
061    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyB, 0);
062    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2));
063    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1));
064  }
065
066  @Test
067  public void testEqualsInstance() {
068    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
069    HbckRegionInfo hi2 = hi1;
070    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2));
071    assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1));
072  }
073
074  @Test
075  public void testDiffTable() {
076    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
077    HbckRegionInfo hi2 = genHbckInfo(table2, keyA, keyC, 0);
078    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
079    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
080  }
081
082  @Test
083  public void testDiffStartKey() {
084    HbckRegionInfo hi1 = genHbckInfo(table, keyStart, keyC, 0);
085    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
086    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
087    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
088  }
089
090  @Test
091  public void testDiffEndKey() {
092    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
093    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
094    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
095    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
096  }
097
098  @Test
099  public void testAbsEndKey() {
100    HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
101    HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyEnd, 0);
102    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0);
103    assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0);
104  }
105}
106