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