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.master;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotEquals;
022
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfoBuilder;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.jupiter.api.BeforeEach;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.junit.jupiter.api.TestInfo;
033
034@Tag(MasterTests.TAG)
035@Tag(SmallTests.TAG)
036public class TestRegionPlan {
037
038  private final ServerName SRC = ServerName.valueOf("source", 1234, 2345);
039  private final ServerName DEST = ServerName.valueOf("dest", 1234, 2345);
040
041  private String methodName;
042
043  @BeforeEach
044  public void setUp(TestInfo testInfo) {
045    methodName = testInfo.getTestMethod().get().getName();
046  }
047
048  @Test
049  public void testCompareTo() {
050    RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(methodName)).build();
051    RegionPlan a = new RegionPlan(hri, null, null);
052    RegionPlan b = new RegionPlan(hri, null, null);
053    assertEquals(0, a.compareTo(b));
054    a = new RegionPlan(hri, SRC, null);
055    b = new RegionPlan(hri, null, null);
056    assertEquals(1, a.compareTo(b));
057    a = new RegionPlan(hri, null, null);
058    b = new RegionPlan(hri, SRC, null);
059    assertEquals(-1, a.compareTo(b));
060    a = new RegionPlan(hri, SRC, null);
061    b = new RegionPlan(hri, SRC, null);
062    assertEquals(0, a.compareTo(b));
063    a = new RegionPlan(hri, SRC, null);
064    b = new RegionPlan(hri, SRC, DEST);
065    assertEquals(-1, a.compareTo(b));
066    a = new RegionPlan(hri, SRC, DEST);
067    b = new RegionPlan(hri, SRC, DEST);
068    assertEquals(0, a.compareTo(b));
069  }
070
071  @Test
072  public void testEqualsWithNulls() {
073    RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(methodName)).build();
074    RegionPlan a = new RegionPlan(hri, null, null);
075    RegionPlan b = new RegionPlan(hri, null, null);
076    assertEquals(a, b);
077    a = new RegionPlan(hri, SRC, null);
078    b = new RegionPlan(hri, null, null);
079    assertNotEquals(a, b);
080    a = new RegionPlan(hri, SRC, null);
081    b = new RegionPlan(hri, SRC, null);
082    assertEquals(a, b);
083    a = new RegionPlan(hri, SRC, null);
084    b = new RegionPlan(hri, SRC, DEST);
085    assertNotEquals(a, b);
086  }
087
088  @Test
089  public void testEquals() {
090    RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(methodName)).build();
091
092    // Identity equality
093    RegionPlan plan = new RegionPlan(hri, SRC, DEST);
094    assertEquals(plan.hashCode(), new RegionPlan(hri, SRC, DEST).hashCode());
095    assertEquals(plan, new RegionPlan(hri, SRC, DEST));
096
097    // HRI is used for equality
098    RegionInfo other =
099      RegionInfoBuilder.newBuilder(TableName.valueOf(methodName + "other")).build();
100    assertNotEquals(plan.hashCode(), new RegionPlan(other, SRC, DEST).hashCode());
101    assertNotEquals(plan, new RegionPlan(other, SRC, DEST));
102  }
103}