001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotSame;
023import static org.junit.Assert.assertTrue;
024
025import org.apache.hadoop.hbase.testclassification.MiscTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031@Category({MiscTests.class, SmallTests.class})
032public class TestHRegionLocation {
033
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036      HBaseClassTestRule.forClass(TestHRegionLocation.class);
037
038  /**
039   * HRegionLocations are equal if they have the same 'location' -- i.e. host and
040   * port -- even if they are carrying different regions.  Verify that is indeed
041   * the case.
042   */
043  @Test
044  public void testHashAndEqualsCode() {
045    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
046    HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
047    HRegionLocation hrl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
048    assertEquals(hrl1.hashCode(), hrl2.hashCode());
049    assertTrue(hrl1.equals(hrl2));
050    HRegionLocation hrl3 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
051    assertNotSame(hrl1, hrl3);
052    // They are equal because they have same location even though they are
053    // carrying different regions or timestamp.
054    assertTrue(hrl1.equals(hrl3));
055    ServerName hsa2 = ServerName.valueOf("localhost", 12345, -1L);
056    HRegionLocation hrl4 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
057    // These have same HRI but different locations so should be different.
058    assertFalse(hrl3.equals(hrl4));
059    HRegionLocation hrl5 = new HRegionLocation(hrl4.getRegionInfo(),
060        hrl4.getServerName(), hrl4.getSeqNum() + 1);
061    assertTrue(hrl4.equals(hrl5));
062  }
063
064  @Test
065  public void testToString() {
066    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
067    HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
068    System.out.println(hrl1.toString());
069  }
070
071  @SuppressWarnings("SelfComparison")
072  @Test
073  public void testCompareTo() {
074    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
075    HRegionLocation hsl1 =
076      new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
077    ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L);
078    HRegionLocation hsl2 =
079      new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
080    assertEquals(0, hsl1.compareTo(hsl1));
081    assertEquals(0, hsl2.compareTo(hsl2));
082    int compare1 = hsl1.compareTo(hsl2);
083    int compare2 = hsl2.compareTo(hsl1);
084    assertTrue((compare1 > 0)? compare2 < 0: compare2 > 0);
085  }
086}
087