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