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.procedure2;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.MasterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.ServerCrashState;
031import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
032
033@Category({MasterTests.class, SmallTests.class})
034public class TestProcedureToString {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestProcedureToString.class);
039
040  /**
041   * A do-nothing environment for BasicProcedure.
042   */
043  static class BasicProcedureEnv {};
044
045  /**
046   * A do-nothing basic procedure just for testing toString.
047   */
048  static class BasicProcedure extends Procedure<BasicProcedureEnv> {
049    @Override
050    protected Procedure<BasicProcedureEnv>[] execute(BasicProcedureEnv env)
051        throws ProcedureYieldException, InterruptedException {
052      return new Procedure [] {this};
053    }
054
055    @Override
056    protected void rollback(BasicProcedureEnv env) throws IOException, InterruptedException {
057    }
058
059    @Override
060    protected boolean abort(BasicProcedureEnv env) {
061      return false;
062    }
063
064    @Override
065    protected void serializeStateData(ProcedureStateSerializer serializer)
066        throws IOException {
067    }
068
069    @Override
070    protected void deserializeStateData(ProcedureStateSerializer serializer)
071        throws IOException {
072    }
073  }
074
075  /**
076   * A do-nothing basic procedure that overrides the toStringState method. It just doubles the
077   * current state string.
078   */
079  static class DoublingStateStringBasicProcedure extends BasicProcedure {
080    @Override
081    protected void toStringState(StringBuilder builder) {
082      // Call twice to get the state string twice as our state value.
083      super.toStringState(builder);
084      super.toStringState(builder);
085    }
086  }
087
088  /**
089   * Test that I can override the toString for its state value.
090   */
091  @Test
092  public void testBasicToString() {
093    BasicProcedure p = new BasicProcedure();
094    ProcedureState state = ProcedureState.RUNNABLE;
095    p.setState(state);
096    // Just assert that the toString basically works and has state in it.
097    assertTrue(p.toString().contains(state.toString()));
098    p = new DoublingStateStringBasicProcedure();
099    p.setState(state);
100    // Assert our override works and that we get double the state...
101    String testStr = state.toString() + state.toString();
102    assertTrue(p.toString().contains(testStr));
103  }
104
105  /**
106   * Do-nothing SimpleMachineProcedure for checking its toString.
107   */
108  static class SimpleStateMachineProcedure
109          extends StateMachineProcedure<BasicProcedureEnv, ServerCrashState> {
110    @Override
111    protected org.apache.hadoop.hbase.procedure2.StateMachineProcedure.Flow executeFromState(
112            BasicProcedureEnv env, ServerCrashState state)
113            throws ProcedureYieldException, InterruptedException {
114      return null;
115    }
116
117    @Override
118    protected void rollbackState(BasicProcedureEnv env, ServerCrashState state) throws IOException,
119        InterruptedException {
120    }
121
122    @Override
123    protected ServerCrashState getState(int stateId) {
124      return ServerCrashState.valueOf(stateId);
125    }
126
127    @Override
128    protected int getStateId(ServerCrashState state) {
129      return state.getNumber();
130    }
131
132    @Override
133    protected ServerCrashState getInitialState() {
134      return null;
135    }
136
137    @Override
138    protected boolean abort(BasicProcedureEnv env) {
139      return false;
140    }
141  }
142
143  @Test
144  public void testStateMachineProcedure() {
145    SimpleStateMachineProcedure p = new SimpleStateMachineProcedure();
146    ProcedureState state = ProcedureState.RUNNABLE;
147    p.setState(state);
148    p.setNextState(ServerCrashState.SERVER_CRASH_ASSIGN);
149    // Just assert that the toString basically works and has state in it.
150    assertTrue(p.toString().contains(state.toString()));
151    assertTrue(p.toString().contains(ServerCrashState.SERVER_CRASH_ASSIGN.toString()));
152  }
153}