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