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.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfoBuilder;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.Rule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.junit.rules.TestName;
035
036@Category({ ClientTests.class, SmallTests.class })
037public class TestCatalogFamilyFormat {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestCatalogFamilyFormat.class);
042
043  @Rule
044  public TestName name = new TestName();
045
046  @Test
047  public void testParseReplicaIdFromServerColumn() {
048    String column1 = HConstants.SERVER_QUALIFIER_STR;
049    assertEquals(0, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column1)));
050    String column2 = column1 + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER;
051    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column2)));
052    String column3 = column2 + "00";
053    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column3)));
054    String column4 = column3 + "2A";
055    assertEquals(42, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column4)));
056    String column5 = column4 + "2A";
057    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column5)));
058    String column6 = HConstants.STARTCODE_QUALIFIER_STR;
059    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column6)));
060  }
061
062  @Test
063  public void testMetaReaderGetColumnMethods() {
064    assertArrayEquals(HConstants.SERVER_QUALIFIER, CatalogFamilyFormat.getServerColumn(0));
065    assertArrayEquals(
066      Bytes.toBytes(
067        HConstants.SERVER_QUALIFIER_STR + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
068      CatalogFamilyFormat.getServerColumn(42));
069
070    assertArrayEquals(HConstants.STARTCODE_QUALIFIER, CatalogFamilyFormat.getStartCodeColumn(0));
071    assertArrayEquals(
072      Bytes.toBytes(HConstants.STARTCODE_QUALIFIER_STR +
073        CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
074      CatalogFamilyFormat.getStartCodeColumn(42));
075
076    assertArrayEquals(HConstants.SEQNUM_QUALIFIER, CatalogFamilyFormat.getSeqNumColumn(0));
077    assertArrayEquals(
078      Bytes.toBytes(
079        HConstants.SEQNUM_QUALIFIER_STR + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
080      CatalogFamilyFormat.getSeqNumColumn(42));
081  }
082
083  /**
084   * The info we can get from the regionName is: table name, start key, regionId, replicaId.
085   */
086  @Test
087  public void testParseRegionInfoFromRegionName() throws IOException  {
088    RegionInfo originalRegionInfo = RegionInfoBuilder.newBuilder(
089        TableName.valueOf(name.getMethodName())).setRegionId(999999L)
090      .setStartKey(Bytes.toBytes("2")).setEndKey(Bytes.toBytes("3"))
091      .setReplicaId(1).build();
092    RegionInfo newParsedRegionInfo = CatalogFamilyFormat
093      .parseRegionInfoFromRegionName(originalRegionInfo.getRegionName());
094    assertEquals("Parse TableName error", originalRegionInfo.getTable(),
095      newParsedRegionInfo.getTable());
096    assertEquals("Parse regionId error", originalRegionInfo.getRegionId(),
097      newParsedRegionInfo.getRegionId());
098    assertTrue("Parse startKey error", Bytes.equals(originalRegionInfo.getStartKey(),
099      newParsedRegionInfo.getStartKey()));
100    assertEquals("Parse replicaId error", originalRegionInfo.getReplicaId(),
101      newParsedRegionInfo.getReplicaId());
102    assertTrue("We can't parse endKey from regionName only",
103      Bytes.equals(HConstants.EMPTY_END_ROW, newParsedRegionInfo.getEndKey()));
104  }
105}