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 port -- even if
040   * they are carrying different regions. Verify that is indeed the case.
041   */
042  @Test
043  public void testHashAndEqualsCode() {
044    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
045    HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
046    HRegionLocation hrl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
047    assertEquals(hrl1.hashCode(), hrl2.hashCode());
048    assertTrue(hrl1.equals(hrl2));
049    HRegionLocation hrl3 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
050    assertNotSame(hrl1, hrl3);
051    // They are equal because they have same location even though they are
052    // carrying different regions or timestamp.
053    assertTrue(hrl1.equals(hrl3));
054    ServerName hsa2 = ServerName.valueOf("localhost", 12345, -1L);
055    HRegionLocation hrl4 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
056    // These have same HRI but different locations so should be different.
057    assertFalse(hrl3.equals(hrl4));
058    HRegionLocation hrl5 =
059      new HRegionLocation(hrl4.getRegionInfo(), hrl4.getServerName(), hrl4.getSeqNum() + 1);
060    assertTrue(hrl4.equals(hrl5));
061  }
062
063  @Test
064  public void testToString() {
065    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
066    HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
067    System.out.println(hrl1.toString());
068  }
069
070  @SuppressWarnings("SelfComparison")
071  @Test
072  public void testCompareTo() {
073    ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L);
074    HRegionLocation hsl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1);
075    ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L);
076    HRegionLocation hsl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2);
077    assertEquals(0, hsl1.compareTo(hsl1));
078    assertEquals(0, hsl2.compareTo(hsl2));
079    int compare1 = hsl1.compareTo(hsl2);
080    int compare2 = hsl2.compareTo(hsl1);
081    assertTrue((compare1 > 0) ? compare2 < 0 : compare2 > 0);
082  }
083}