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;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotSame;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestHRegionLocation {
32    /**
33     * HRegionLocations are equal if they have the same 'location' -- i.e. host and
34     * port -- even if they are carrying different regions.  Verify that is indeed
35     * the case.
36     */
37    @Test
38    public void testHashAndEqualsCode() {
39      ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
40      HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
41      HRegionLocation hrl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
42      assertEquals(hrl1.hashCode(), hrl2.hashCode());
43      assertTrue(hrl1.equals(hrl2));
44      HRegionLocation hrl3 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
45      assertNotSame(hrl1, hrl3);
46      // They are equal because they have same location even though they are
47      // carrying different regions or timestamp.
48      assertTrue(hrl1.equals(hrl3));
49      ServerName hsa2 = ServerName.valueOf("localhost", 12345, -1L);
50      HRegionLocation hrl4 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
51      // These have same HRI but different locations so should be different.
52      assertFalse(hrl3.equals(hrl4));
53      HRegionLocation hrl5 = new HRegionLocation(hrl4.getRegionInfo(),
54          hrl4.getServerName(), hrl4.getSeqNum() + 1);
55      assertTrue(hrl4.equals(hrl5));
56    }
57  
58    @Test
59    public void testToString() {
60      ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
61      HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
62      System.out.println(hrl1.toString());
63    }
64  
65    @Test
66    public void testCompareTo() {
67      ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
68      HRegionLocation hsl1 =
69        new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
70      ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L);
71      HRegionLocation hsl2 =
72        new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
73      assertTrue(hsl1.compareTo(hsl1) == 0);
74      assertTrue(hsl2.compareTo(hsl2) == 0);
75      int compare1 = hsl1.compareTo(hsl2);
76      int compare2 = hsl2.compareTo(hsl1);
77      assertTrue((compare1 > 0)? compare2 < 0: compare2 > 0);
78    }
79  
80  }
81