@Test
  public void testClusterRegionLocations() {
    // tests whether region locations are handled correctly in Cluster
    List<ServerName> servers = getListOfServerNames(randomServers(10, 10));
    List<RegionInfo> regions = randomRegions(101);
    Map<ServerName, List<RegionInfo>> clusterState = new HashMap<>();

    assignRegions(regions, servers, clusterState);

    // mock block locality for some regions
    RegionHDFSBlockLocationFinder locationFinder = mock(RegionHDFSBlockLocationFinder.class);
    // block locality: region:0 => {server:0}
    // region:1 => {server:0, server:1}
    // region:42 => {server:4, server:9, server:5}
    when(locationFinder.getTopBlockLocations(regions.get(0)))
      .thenReturn(Lists.newArrayList(servers.get(0)));
    when(locationFinder.getTopBlockLocations(regions.get(1)))
      .thenReturn(Lists.newArrayList(servers.get(0), servers.get(1)));
    when(locationFinder.getTopBlockLocations(regions.get(42)))
      .thenReturn(Lists.newArrayList(servers.get(4), servers.get(9), servers.get(5)));
    // this server does not exists in clusterStatus
    when(locationFinder.getTopBlockLocations(regions.get(43)))
      .thenReturn(Lists.newArrayList(ServerName.valueOf("foo", 0, 0)));

    BalancerClusterState cluster =
      new BalancerClusterState(clusterState, null, locationFinder, null);

    // this is ok, it is just a test
    int r0 = ArrayUtils.indexOf(cluster.regions, regions.get(0));
    int r1 = ArrayUtils.indexOf(cluster.regions, regions.get(1));
    int r10 = ArrayUtils.indexOf(cluster.regions, regions.get(10));
    int r42 = ArrayUtils.indexOf(cluster.regions, regions.get(42));
    int r43 = ArrayUtils.indexOf(cluster.regions, regions.get(43));

    int s0 = cluster.serversToIndex.get(servers.get(0).getAddress());
    int s1 = cluster.serversToIndex.get(servers.get(1).getAddress());
    int s4 = cluster.serversToIndex.get(servers.get(4).getAddress());
    int s5 = cluster.serversToIndex.get(servers.get(5).getAddress());
    int s9 = cluster.serversToIndex.get(servers.get(9).getAddress());

    // region 0 locations
    assertEquals(1, cluster.regionLocations[r0].length);
    assertEquals(s0, cluster.regionLocations[r0][0]);

    // region 1 locations
    assertEquals(2, cluster.regionLocations[r1].length);
    assertEquals(s0, cluster.regionLocations[r1][0]);
    assertEquals(s1, cluster.regionLocations[r1][1]);

    // region 10 locations
    assertEquals(0, cluster.regionLocations[r10].length);

    // region 42 locations
    assertEquals(3, cluster.regionLocations[r42].length);
    assertEquals(s4, cluster.regionLocations[r42][0]);
    assertEquals(s9, cluster.regionLocations[r42][1]);
    assertEquals(s5, cluster.regionLocations[r42][2]);

    // region 43 locations
    assertEquals(1, cluster.regionLocations[r43].length);
    assertEquals(-1, cluster.regionLocations[r43][0]);
  }
Asserts a valid retained assignment plan.
<p>
Must meet the following conditions:
<ul>
<li>Every input region has an assignment, and to an online server
<li>If a region had an existing assignment to a server with the same address a a currently
online server, it will be assigned to it
</ul>
hbase-balancer/src/test/java/org/apache/hadoop/hbase/master/balancer/TestBaseLoadBalancer.java