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.util.HBaseFsck.HbckInfo;
27  import org.apache.hadoop.hbase.util.HBaseFsck.MetaEntry;
28  import org.junit.Test;
29  
30  /**
31   * Test the comparator used by Hbck.
32   */
33  public class TestHBaseFsckComparator {
34  
35    byte[] table = Bytes.toBytes("table1");
36    byte[] table2 = Bytes.toBytes("table2");
37    byte[] keyStart = Bytes.toBytes("");
38    byte[] keyA = Bytes.toBytes("A");
39    byte[] keyB = Bytes.toBytes("B");
40    byte[] keyC = Bytes.toBytes("C");
41    byte[] keyEnd = Bytes.toBytes("");
42  
43    static HbckInfo genHbckInfo(byte[] table, byte[] start, byte[] end, int time) {
44      return new HbckInfo(new MetaEntry(new HRegionInfo(table, start, end), null,
45          time));
46    }
47  
48    @Test
49    public void testEquals() {
50      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
51      HbckInfo hi2 = genHbckInfo(table, keyA, keyB, 0);
52      assertEquals(0, HBaseFsck.cmp.compare(hi1, hi2));
53      assertEquals(0, HBaseFsck.cmp.compare(hi2, hi1));
54    }
55  
56    @Test
57    public void testEqualsInstance() {
58      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
59      HbckInfo hi2 = hi1;
60      assertEquals(0, HBaseFsck.cmp.compare(hi1, hi2));
61      assertEquals(0, HBaseFsck.cmp.compare(hi2, hi1));
62    }
63  
64    @Test
65    public void testDiffTable() {
66      HbckInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
67      HbckInfo hi2 = genHbckInfo(table2, keyA, keyC, 0);
68      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
69      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
70    }
71  
72    @Test
73    public void testDiffStartKey() {
74      HbckInfo hi1 = genHbckInfo(table, keyStart, keyC, 0);
75      HbckInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
76      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
77      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
78    }
79  
80    @Test
81    public void testDiffEndKey() {
82      HbckInfo hi1 = genHbckInfo(table, keyA, keyB, 0);
83      HbckInfo hi2 = genHbckInfo(table, keyA, keyC, 0);
84      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
85      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
86    }
87  
88    @Test
89    public void testAbsEndKey() {
90      HbckInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
91      HbckInfo hi2 = genHbckInfo(table, keyA, keyEnd, 0);
92      assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
93      assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
94    }
95  
96    @Test
97    public void testTiebreaker() {
98      HbckInfo hi1 = genHbckInfo(table, keyA, keyC, 0);
99      HbckInfo hi2 = genHbckInfo(table, keyA, keyC, 1);
100     assertTrue(HBaseFsck.cmp.compare(hi1, hi2) < 0);
101     assertTrue(HBaseFsck.cmp.compare(hi2, hi1) > 0);
102   }
103 }