View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static junit.framework.TestCase.assertFalse;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.testclassification.MasterTests;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.TableName;
31  import org.junit.rules.TestName;
32  import org.junit.Rule;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(SmallTests.class)
37  public class TestRegionPlan {
38    private final ServerName SRC = ServerName.valueOf("source", 1234, 2345);
39    private final ServerName DEST = ServerName.valueOf("dest", 1234, 2345);
40    @Rule
41    public TestName name = new TestName();
42  
43    @Test
44    public void testCompareTo() {
45      HRegionInfo hri = new HRegionInfo(TableName.valueOf(name.getMethodName()));
46      RegionPlan a = new RegionPlan(hri, null, null);
47      RegionPlan b = new RegionPlan(hri, null, null);
48      assertEquals(0, a.compareTo(b));
49      a = new RegionPlan(hri, SRC, null);
50      b = new RegionPlan(hri, null, null);
51      assertEquals(1, a.compareTo(b));
52      a = new RegionPlan(hri, null, null);
53      b = new RegionPlan(hri, SRC, null);
54      assertEquals(-1, a.compareTo(b));
55      a = new RegionPlan(hri, SRC, null);
56      b = new RegionPlan(hri, SRC, null);
57      assertEquals(0, a.compareTo(b));
58      a = new RegionPlan(hri, SRC, null);
59      b = new RegionPlan(hri, SRC, DEST);
60      assertEquals(-1, a.compareTo(b));
61      a = new RegionPlan(hri, SRC, DEST);
62      b = new RegionPlan(hri, SRC, DEST);
63      assertEquals(0, a.compareTo(b));
64    }
65  
66    @Test
67    public void testEqualsWithNulls() {
68      HRegionInfo hri = new HRegionInfo(TableName.valueOf(name.getMethodName()));
69      RegionPlan a = new RegionPlan(hri, null, null);
70      RegionPlan b = new RegionPlan(hri, null, null);
71      assertTrue(a.equals(b));
72      a = new RegionPlan(hri, SRC, null);
73      b = new RegionPlan(hri, null, null);
74      assertFalse(a.equals(b));
75      a = new RegionPlan(hri, SRC, null);
76      b = new RegionPlan(hri, SRC, null);
77      assertTrue(a.equals(b));
78      a = new RegionPlan(hri, SRC, null);
79      b = new RegionPlan(hri, SRC, DEST);
80      assertFalse(a.equals(b));
81    }
82  
83    @Test
84    public void testEquals() {
85      HRegionInfo hri = new HRegionInfo(TableName.valueOf(name.getMethodName()));
86  
87      // Identity equality
88      RegionPlan plan = new RegionPlan(hri, SRC, DEST);
89      assertEquals(plan.hashCode(), new RegionPlan(hri, SRC, DEST).hashCode());
90      assertEquals(plan, new RegionPlan(hri, SRC, DEST));
91  
92      // HRI is used for equality
93      HRegionInfo other =
94          new HRegionInfo(TableName.valueOf(name.getMethodName() + "other"));
95      assertNotEquals(plan.hashCode(), new RegionPlan(other, SRC, DEST).hashCode());
96      assertNotEquals(plan, new RegionPlan(other, SRC, DEST));
97    }
98  }