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.client.RegionInfoBuilder;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ MiscTests.class, SmallTests.class })
033public class TestHRegionLocation {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestHRegionLocation.class);
038
039  /**
040   * HRegionLocations are equal if they have the same 'location' -- i.e. host and port -- even if
041   * they are carrying different regions. Verify that is indeed the case.
042   */
043  @Test
044  public void testHashAndEqualsCode() {
045    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
046    HRegionLocation hrl1 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
047    HRegionLocation hrl2 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
048    assertEquals(hrl1.hashCode(), hrl2.hashCode());
049    assertTrue(hrl1.equals(hrl2));
050    HRegionLocation hrl3 = new HRegionLocation(RegionInfoBuilder.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(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa2);
057    // These have same HRI but different locations so should be different.
058    assertFalse(hrl3.equals(hrl4));
059    HRegionLocation hrl5 =
060      new HRegionLocation(hrl4.getRegion(), 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(RegionInfoBuilder.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 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
076    ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L);
077    HRegionLocation hsl2 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa2);
078    assertEquals(0, hsl1.compareTo(hsl1));
079    assertEquals(0, hsl2.compareTo(hsl2));
080    int compare1 = hsl1.compareTo(hsl2);
081    int compare2 = hsl2.compareTo(hsl1);
082    assertTrue((compare1 > 0) ? compare2 < 0 : compare2 > 0);
083  }
084}