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.TableName; 025import org.apache.hadoop.hbase.client.RegionInfoBuilder; 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( 053 RegionInfoBuilder.newBuilder(table).setStartKey(start).setEndKey(end).build(), null, time)); 054 } 055 056 @Test 057 public void testEquals() { 058 HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0); 059 HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyB, 0); 060 assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2)); 061 assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1)); 062 } 063 064 @Test 065 public void testEqualsInstance() { 066 HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0); 067 HbckRegionInfo hi2 = hi1; 068 assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi1, hi2)); 069 assertEquals(0, HbckRegionInfo.COMPARATOR.compare(hi2, hi1)); 070 } 071 072 @Test 073 public void testDiffTable() { 074 HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0); 075 HbckRegionInfo hi2 = genHbckInfo(table2, keyA, keyC, 0); 076 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0); 077 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0); 078 } 079 080 @Test 081 public void testDiffStartKey() { 082 HbckRegionInfo hi1 = genHbckInfo(table, keyStart, keyC, 0); 083 HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0); 084 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0); 085 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0); 086 } 087 088 @Test 089 public void testDiffEndKey() { 090 HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyB, 0); 091 HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyC, 0); 092 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0); 093 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0); 094 } 095 096 @Test 097 public void testAbsEndKey() { 098 HbckRegionInfo hi1 = genHbckInfo(table, keyA, keyC, 0); 099 HbckRegionInfo hi2 = genHbckInfo(table, keyA, keyEnd, 0); 100 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi1, hi2) < 0); 101 assertTrue(HbckRegionInfo.COMPARATOR.compare(hi2, hi1) > 0); 102 } 103}