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  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 = new ServerName("localhost", 1234, -1L);
40      HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO,
41        hsa1.getHostname(), hsa1.getPort());
42      HRegionLocation hrl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO,
43        hsa1.getHostname(), hsa1.getPort());
44      assertEquals(hrl1.hashCode(), hrl2.hashCode());
45      assertTrue(hrl1.equals(hrl2));
46      HRegionLocation hrl3 = new HRegionLocation(HRegionInfo.ROOT_REGIONINFO,
47        hsa1.getHostname(), hsa1.getPort());
48      assertNotSame(hrl1, hrl3);
49      // They are equal because they have same location even though they are
50      // carrying different regions.
51      assertTrue(hrl1.equals(hrl3));
52      ServerName hsa2 = new ServerName("localhost", 12345, -1L);
53      HRegionLocation hrl4 = new HRegionLocation(HRegionInfo.ROOT_REGIONINFO,
54          hsa2.getHostname(), hsa2.getPort());
55      // These have same HRI but different locations so should be different.
56      assertFalse(hrl3.equals(hrl4));
57    }
58  
59    @Test
60    public void testToString() {
61      ServerName hsa1 = new ServerName("localhost", 1234, -1L);
62      HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO,
63        hsa1.getHostname(), hsa1.getPort());
64      System.out.println(hrl1.toString());
65    }
66  
67    @Test
68    public void testCompareTo() {
69      ServerName hsa1 = new ServerName("localhost", 1234, -1L);
70      HRegionLocation hsl1 =
71        new HRegionLocation(HRegionInfo.ROOT_REGIONINFO, hsa1.getHostname(), hsa1.getPort());
72      ServerName hsa2 = new ServerName("localhost", 1235, -1L);
73      HRegionLocation hsl2 =
74        new HRegionLocation(HRegionInfo.ROOT_REGIONINFO, hsa2.getHostname(), hsa2.getPort());
75      assertTrue(hsl1.compareTo(hsl1) == 0);
76      assertTrue(hsl2.compareTo(hsl2) == 0);
77      int compare1 = hsl1.compareTo(hsl2);
78      int compare2 = hsl2.compareTo(hsl1);
79      assertTrue((compare1 > 0)? compare2 < 0: compare2 > 0);
80    }
81  
82    @org.junit.Rule
83    public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
84      new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
85  }
86