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.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HRegionInfo;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.testclassification.MasterTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.Rule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031import org.junit.rules.TestName;
032
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
034
035@Category({ MasterTests.class, SmallTests.class })
036public class TestRegionState {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestRegionState.class);
041
042  @Rule
043  public TestName name = new TestName();
044
045  @Test
046  public void testSerializeDeserialize() {
047    final TableName tableName = TableName.valueOf("testtb");
048    for (RegionState.State state : RegionState.State.values()) {
049      testSerializeDeserialize(tableName, state);
050    }
051  }
052
053  private void testSerializeDeserialize(final TableName tableName, final RegionState.State state) {
054    RegionState state1 = RegionState.createForTesting(new HRegionInfo(tableName), state);
055    ClusterStatusProtos.RegionState protobuf1 = state1.convert();
056    RegionState state2 = RegionState.convert(protobuf1);
057    ClusterStatusProtos.RegionState protobuf2 = state1.convert();
058    assertEquals("RegionState does not match " + state, state1, state2);
059    assertEquals("Protobuf does not match " + state, protobuf1, protobuf2);
060  }
061}