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