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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotSame;
023import static org.junit.jupiter.api.Assertions.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.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031@Tag(MiscTests.TAG)
032@Tag(SmallTests.TAG)
033public class TestHRegionLocation {
034
035  /**
036   * HRegionLocations are equal if they have the same 'location' -- i.e. host and port -- even if
037   * they are carrying different regions. Verify that is indeed the case.
038   */
039  @Test
040  public void testHashAndEqualsCode() {
041    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
042    HRegionLocation hrl1 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
043    HRegionLocation hrl2 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
044    assertEquals(hrl1.hashCode(), hrl2.hashCode());
045    assertTrue(hrl1.equals(hrl2));
046    HRegionLocation hrl3 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
047    assertNotSame(hrl1, hrl3);
048    // They are equal because they have same location even though they are
049    // carrying different regions or timestamp.
050    assertTrue(hrl1.equals(hrl3));
051    ServerName hsa2 = ServerName.valueOf("localhost", 12345, -1L);
052    HRegionLocation hrl4 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa2);
053    // These have same HRI but different locations so should be different.
054    assertFalse(hrl3.equals(hrl4));
055    HRegionLocation hrl5 =
056      new HRegionLocation(hrl4.getRegion(), hrl4.getServerName(), hrl4.getSeqNum() + 1);
057    assertTrue(hrl4.equals(hrl5));
058  }
059
060  @Test
061  public void testToString() {
062    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
063    HRegionLocation hrl1 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
064    System.out.println(hrl1.toString());
065  }
066
067  @SuppressWarnings("SelfComparison")
068  @Test
069  public void testCompareTo() {
070    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
071    HRegionLocation hsl1 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa1);
072    ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L);
073    HRegionLocation hsl2 = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, hsa2);
074    assertEquals(0, hsl1.compareTo(hsl1));
075    assertEquals(0, hsl2.compareTo(hsl2));
076    int compare1 = hsl1.compareTo(hsl2);
077    int compare2 = hsl2.compareTo(hsl1);
078    assertTrue((compare1 > 0) ? compare2 < 0 : compare2 > 0);
079  }
080}