1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.util;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.apache.hadoop.hbase.util.HBaseFsck.HbckInfo;
28  import org.apache.hadoop.hbase.util.HBaseFsck.MetaEntry;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  /**
33   * Test the comparator used by Hbck.
34   */
35  @Category(SmallTests.class)
36  public class TestHBaseFsckComparator {
37  
38    byte[] table = Bytes.toBytes("table1");
39    byte[] table2 = Bytes.toBytes("table2");
40    byte[] keyStart = Bytes.toBytes("");
41    byte[] keyA = Bytes.toBytes("A");
42    byte[] keyB = Bytes.toBytes("B");
43    byte[] keyC = Bytes.toBytes("C");
44    byte[] keyEnd = Bytes.toBytes("");
45  
46    static HbckInfo genHbckInfo(byte[] table, byte[] start, byte[] end, int time) {
47      return new HbckInfo(new MetaEntry(new HRegionInfo(table, start, end), null,
48          time));
49    }
50  
51    @Test
52    public void testEquals() {
53      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
54      HbckInfo hi2 = genHbckInfo(table, keyA, keyB, 0);
55      assertEquals(0, HBaseFsck.cmp.compare(hi1, hi2));
56      assertEquals(0, HBaseFsck.cmp.compare(hi2, hi1));
57    }
58  
59    @Test
60    public void testEqualsInstance() {
61      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
62      HbckInfo hi2 = hi1;
63      assertEquals(0, HBaseFsck.cmp.compare(hi1, hi2));
64      assertEquals(0, HBaseFsck.cmp.compare(hi2, hi1));
65    }
66  
67    @Test
68    public void testDiffTable() {
69      HbckInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
70      HbckInfo hi2 = genHbckInfo(table2, keyA, keyC, 0);
71      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
72      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
73    }
74  
75    @Test
76    public void testDiffStartKey() {
77      HbckInfo hi1 = genHbckInfo(table, keyStart, keyC, 0);
78      HbckInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
79      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
80      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
81    }
82  
83    @Test
84    public void testDiffEndKey() {
85      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
86      HbckInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
87      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
88      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
89    }
90  
91    @Test
92    public void testAbsEndKey() {
93      HbckInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
94      HbckInfo hi2 = genHbckInfo(table, keyA, keyEnd, 0);
95      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
96      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
97    }
98  
99    @org.junit.Rule
100   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
101     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
102 }
103