View Javadoc

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